zal*_*ari 12 javascript amazon-s3 amazon-web-services meteor
我正在尝试将文件上传到我的Amazon S3 Bucket.S3和亚马逊成立.这是来自亚马逊的错误消息:
冲突的查询字符串参数:acl,policy
使用Node.js的Crypto.js对策略和签名进行编码
var crypto=Npm.require("crypto");
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用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);
Run Code Online (Sandbox Code Playgroud)
这是我在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);
}
});
Run Code Online (Sandbox Code Playgroud)
她正在对政策和签名进行编码:
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");
}
Run Code Online (Sandbox Code Playgroud)
A无法弄清楚最近发生了什么.POST方法或加密可能已经存在问题,因为我不太了解这些方法.如果有人可以指出我正确的方向,编码或正确地向AmazonS3发送POST请求,它可能会有很大帮助.
(我不喜欢使用filepicker.io,因为我不想强迫客户端在那里注册.)
提前致谢!!!
为什么不使用aws-sdk包?它为您提供了所有需要的方法。例如,以下是将文件添加到存储桶的简单函数:
s3.putObject({
Bucket: ...,
ACL: ...,
Key: ...,
Metadata: ...,
ContentType: ...,
Body: ...,
}, function(err, data) {
...
});
Run Code Online (Sandbox Code Playgroud)
直接上传到S3你可以使用弹弓包:
meteor add edgee:slingshot
Run Code Online (Sandbox Code Playgroud)
在服务器端声明您的指令:
Slingshot.createDirective("myFileUploads", Slingshot.S3Storage, {
bucket: "mybucket",
allowedFileTypes: ["image/png", "image/jpeg", "image/gif"],
acl: "public-read",
authorize: function () {
//You can add user restrictions here
return true;
},
key: function (file) {
return file.name;
}
});
Run Code Online (Sandbox Code Playgroud)
该指令将自动生成策略和签名.
他们只是像这样上传它:
var uploader = new Slingshot.Upload("myFileUploads");
uploader.send(document.getElementById('input').files[0], function (error, url) {
Meteor.users.update(Meteor.userId(), {$push: {"profile.files": url}});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5899 次 |
| 最近记录: |