我知道很多人问过这个问题我调查了他们的帖子,但我仍然没有得到正确的答案,所以这里就是。
我正在尝试使用Amazon dev引入的基于浏览器的上传技术将图像上传到s3。现在我能够计算政策和我的签名。但是当我尝试上传图片时,我总是得到“签名不匹配”(>。<)。我遇到的一个主要问题是我拥有的凭据只是暂时的:AWS Security Token Service,它由accessKEy,secretKey和安全令牌组成。我会发布我的代码,所以请任何人
这是我的policy_json转换
function setValues(accesskey, secretkey, token) {
var folder = 'avatars/email@domain.com/',
acl = 'public-read',
bucket = 'my-bucket';
var POLICY_JSON = { "expiration": "2013-12-03T12:29:27.000Z",
"conditions": [
{"bucket": bucket},
["starts-with", "$key", folder],
{"acl": acl},
{"success_action_redirect": "201"},
["starts-with", "$Content-Type", "image/png"]
]
};
var policy = Base64.encode(JSON.stringify(POLICY_JSON));
var signature = b64_hmac_sha1(secretkey, policy);
return {
"policy":policy,
"signature":signature,
"folder" : folder,
"acl" : acl,
"bucket" : bucket
}
}
我的上传
function upload(acl, accesskey, policy, signature, token) {
$(':button').click(function()
{
var file = document.getElementById('file').files[0];
// var key = "events/" + (new Date).getTime() + '-' + file.name;
var xdate = '20131016T105000Z';
// var formData = new FormData($('form')[0]); //$('form')[0]
var formData = new FormData(); //$('form')[0]
formData.append("key", "avatars/email@domain.com/${filename}");
formData.append("acl", acl);
formData.append("success_action_redirect", "201");
formData.append("Content-Type", file.type);
formData.append("AWSAccessKeyId", accesskey);
formData.append("policy", policy);
formData.append("signature", signature);
// formData.append("x-amz-security-token", token);
formData.append("file", file);
$.ajax({
url: 'https://mybucket.s3.amazonaws.com', //Server script to process data
type: 'POST',
xhr: function() { // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){ // Check if upload property exists
myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // For handling the progress of the upload
}
return myXhr;
},
//Ajax events
beforeSend: function(e) {
e.setRequestHeader("Authorization", "AWS "+accesskey+":"+signature);
e.setRequestHeader("x-amz-date", xdate);
e.setRequestHeader("x-amz-acl", acl);
e.setRequestHeader("x-amz-security-token", token);
// alert('Are you sure you want to upload document.');
},
success: function(e) { alert('Upload completed'); } ,
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus);
console.log(errorThrown);
} ,
// Form data
data: formData,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
});
}
我唯一的HTML
<form enctype="multipart/form-data">
<input id="file" name="file" type="file" />
<input type="button" value="Upload" />
</form>
这让我感到困惑,首先在mybucket内部,当我的同事使用他的email_address (say image1.jpg)
上传了一张图片(say other_email_Address@domain.com)
时,它有一个名为avatars的文件夹,当我们查看时,上传成功了它创建了一个文件夹,其名称为email_address,其中包含图像。那是为什么?
mybucket/
- avatars/
- my_email_address.@domain.com
- image1
- other_email_address@domain.com
- image1
so on ...
我使用的工具是:webtoolkit.base64.js,sha1.js,base64-binary.js。