我正在尝试将文件上传到我的亚马逊S3存储桶。S3和亚马逊已经设置好了。 这是来自亚马逊的错误消息:
冲突的查询字符串参数: acl, policy
策略和签名已使用Crypto.js编码,用于Node.js
var crypto=Npm.require("crypto");
我正在尝试使用Meteor的HTTP.post方法构建POST请求。这可能也是错误的。
var BucketName="mybucket";
var AWSAccessKeyId="MY_ACCES_KEY";
var AWSSecretKey="MY_SECRET_KEY";
//create policy
var POLICY_JSON={
"expiration": "2009-01-01T00:00:00Z",
"conditions": [
{"bucket": BucketName},
["starts-with", "$key", "uploads/"],
{"acl": 'public-read'},
["starts-with", "$Content-Type", ""],
["content-length-range", 0, 1048576],
]
}
var policyBase64=encodePolicy(POLICY_JSON);
//create signature
var SIGNATURE = encodeSignature(policyBase64,AWSSecretKey);
console.log('signature: ', SIGNATURE);
这是我在Meteor中使用的POST请求:
//Send data----------
var options={
"params":{
"key":file.name,
'AWSAccessKeyId':AWSAccessKeyId,
'acl':'public-read',
'policy':policyBase64,
'signature':SIGNATURE,
'Content-Type':file.type,
'file':file,
"enctype":"multipart/form-data",
}
}
HTTP.call('POST','https://'+BucketName+'.s3.amazonaws.com/',options,function(error,result){
if(error){
console.log("and HTTP ERROR:",error);
}else{
console.log("result:",result);
}
});
现在我正在对政策和签名进行编码:
encodePolicy=function(jsonPolicy){
// stringify the policy, store it in a NodeJS Buffer object
var buffer=new Buffer(JSON.stringify(jsonPolicy));
// convert it to base64
var policy=buffer.toString("base64");
// replace "/" and "+" so that it is URL-safe.
return policy.replace(/\//g,"_").replace(/\+/g,"-");
}
encodeSignature=function(policy,secret){
var hmac=crypto.createHmac("sha256",secret);
hmac.update(policy);
return hmac.digest("hex");
}
A无法确定发生了什么问题。可能在POST方法或加密时已经存在问题,因为我对这些方法不太熟悉。如果有人能引导我正确地进行编码或将POST请求正确发送到AmazonS3,那将会非常有帮助。
(我不喜欢使用filepicker.io,因为我不想强迫客户在那里注册。)
提前感谢了!