如何使用Meteor将文件上传到Amazon

如何使用Meteor将文件上传到Amazon

本文介绍了如何使用Meteor将文件上传到Amazon S3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件上传到我的Amazon S3存储桶. S3和亚马逊成立.这是来自亚马逊的错误消息:

I'm trying to upload files to my Amazon S3 Bucket. S3 and amazon is set up.This is the error message from Amazon:

对策略和签名进行了编码,对于 Node.js

Policy and signature is encoded, with Crypto.js for Node.js

var crypto=Npm.require("crypto");

我正在尝试使用Meteor HTTP.post方法构建POST请求.这也可能是错误的.

I'm trying to build POST request with Meteor HTTP.post method. This could be wrong as well.

    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请求:

This is the POST request I'm using with Meteor:

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

她正在对政策和签名进行编码:

and her I'm encoding the policy and the signature:

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 ,因为我也不想强迫客户端在此注册.)

A can't figure out whats going on. There might already be a problem at the POST method, or the encryption, because I don't know these methods too well. If someone could point me to the right direction, to encode, or send POST request to AmazonS3 properly, it could help a lot.
(I don't like to use filepicker.io, because I don't want to force the client to sign up there as well.)

提前谢谢!

推荐答案

直接上传到S3,您可以使用 slingshot 包:

Direct uploads to S3 you can use the slingshot package:

meteor add edgee:slingshot

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

On the server side declare your directive:

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

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

This directive will generate policy and signature automatically.

他们只是像这样上传它:

And them just upload it like this:

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

这篇关于如何使用Meteor将文件上传到Amazon S3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:09