javascript – 如何使用Meteor将文件上传到Amazon S3?

前端之家收集整理的这篇文章主要介绍了javascript – 如何使用Meteor将文件上传到Amazon S3?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将文件上传到我的Amazon S3 Bucket. S3和亚马逊成立.
这是亚马逊的错误消息:

Conflicting query string parameters: acl,policy

策略和签名被编码,对于Node.js使用Crypto.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'},"$Content-Type",""],["content-length-range",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,因为我不想强制客户端在那里注册.)

提前致谢!!!

解决方法

直接上传到S3可以使用 slingshot包:
meteor add edgee:slingshot

在服务器端声明您的指令:

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;
  }
});

该指令将自动生成策略和签名.

他们只是上传它:

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}});
});

猜你在找的JavaScript相关文章