我是S3的新手。试图捕捉S3 http://docs.aws.amazon.com/AmazonS3/2006-03-01/API/ErrorResponses.html
中的错误我的代码示例:
{"expiration": "2007-12-01T12:00:00.000Z",
"conditions": [
{"bucket": "johnsmith"},
["starts-with", "$key", ""],
{"acl": "private"},
{"success_action_redirect": "http://johnsmith.s3.amazonaws.com/successful_upload.html"},
["eq", "$Content-Type", "application/msword,application/pdf"],
["content-length-range", 2048, 20971520 ]
]
}';
<form action="http://johnsmith.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="${filename}" /><br />
<input type="hidden" name="acl" value="private" />
<input type="hidden" name="success_action_redirect" value="http://johnsmith.s3.amazonaws.com/successful_upload.html" >
<input type="hidden" name="AWSAccessKeyId " value="15B4D3461F177624206A" />
<input type="hidden" name="Policy" value="eyAiZXhwaXJhdGlvbiI6ICIyMDA3LTEyLTAxVDEyOjAwOjAwLjAwMFoiLAogICJjb25kaXRpb25zIjogWwogICAgeyJidWNrZXQiOiAiam9obnNtaXRoIiB9LAogICAgWyJzdGFydHMtd2l0aCIsICIka2V5IiwgInVzZXIvZXJpYy8iXSwKICAgIHsiYWNsIjogInB1YmxpYy1yZWFkIiB9LAogICAgeyJyZWRpcmVjdCI6ICJodHRwOi8vam9obnNtaXRoLnMzLmFtYXpvbmF3cy5jb20vc3VjY2Vzc2Z1bF91cGxvYWQuaHRtbCIgfSwKICAgIFsic3RhcnRzLXdpdGgiLCAiJENvbnRlbnQtVHlwZSIsICJpbWFnZS8iXSwKICAgIHsieC1hbXotbWV0YS11dWlkIjogIjE0MzY1MTIzNjUxMjc0In0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiR4LWFtei1tZXRhLXRhZyIsICIiXSwKICBdCn0K" />
<input type="hidden" name="Signature" value="2qCp0odXe7A9IYyUVqn0w2adtCA=" />
File: <input type="file" name="file" /> <br />
<input type="submit" name="submit" value="Upload to Amazon S3" />
</form>
如果文件上传或过期失败,想要捕获错误。检查内容类型,内容长度范围。
获得正确成功的回调变量,如bucket,key,etag。
由于
答案 0 :(得分:0)
编辑:我的原始答案使用了iframe但我终于让XMLHttpRequest工作了,我认为这是一种更清洁的方法。
我遇到了同样的问题。我的要求是:
互联网上散布着大量解决方案。我让它在两个版本中工作:
使用iFrame呈现POST结果并尝试访问上传的文件(在我的情况下是图片)以检查上传是否有效:https://gist.github.com/joaoffcosta/5728483
使用XMLHttpRequest发出请求并处理进度,完成和过早失败:https://gist.github.com/joaoffcosta/5729398(最佳解决方案IMO )
在这两种解决方案中,您的S3 Bucket应该允许“Everyone”进行“上传/删除”。 此外,XMLHttpRequest解决方案要求您在存储桶上配置CORS。要做到这一点,你应该去你的桶“权限”,按“添加CORS配置”并添加如下内容:
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>http://your_website_or_localhost_server/</AllowedOrigin>
<AllowedMethod>POST</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<ExposeHeader>x-amz-server-side-encryption</ExposeHeader>
<ExposeHeader>x-amz-request-id</ExposeHeader>
<ExposeHeader>x-amz-id-2</ExposeHeader>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
答案 1 :(得分:0)
使用简单的ajax表单帖子......
<script>
function uploadFile() {
var file_name = $('#form1 :input[name="file"]').val().split('\\').pop();
if(!file_name || file_name.length === 0) {
alert('please choose file')
return false;
}
$.ajax({
url: "/getS3FormData.php",
cache: false,
type: 'GET',
data: {file_name : file_name} ,
async: false,
success: function (response) {
var s3 = JSON.parse(response);
handleFileUpload(s3);
},
error: function (error) {
alert("Upload FAILED :-( Retry.");
}
});
}
function handleFileUpload(s3) {
var fd = new FormData();
fd.append('key', s3.key);
fd.append('AWSAccessKeyId', s3.AWSAccessKeyId);
fd.append('acl', s3.acl);
fd.append('policy', s3.policy);
fd.append('signature', s3.signature);
fd.append('Content-Type', s3.content_type);
var file = $('#form1').find('input[name=file]')[0].files[0];
fd.append("file", file);
window.onbeforeunload = function () {
return "Upload in progress";
}
$.ajax({
url: s3.url,
data: fd,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
window.onbeforeunload = null;
window.location.href = s3.success_action_redirect;
},
error: function(error) {
window.onbeforeunload = null;
alert('Upload Failed, Please try again');
}
});
}
html表单
<form id="form1" method="post" enctype="multipart/form-data">
<input type="file" name="file"></input>
<input type="submit" onclick="uploadFile(); return false;" value="Upload">
</form>