【问题标题】:How to upload files to Amazon S3 with Meteor?如何使用 Meteor 将文件上传到 Amazon S3?
【发布时间】:2013-11-03 10:18:57
【问题描述】:

我正在尝试将文件上传到我的 Amazon 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 方法或加密可能已经存在问题,因为我不太了解这些方法。如果有人能指出我正确的方向、正确编码或向 AmazonS3 发送 POST 请求,那将有很大帮助。
(我不喜欢使用 filepicker.io,因为我不想强迫客户也在那里注册。)

提前致谢!!!

【问题讨论】:

  • queryString 和 POST 参数不一样,也许你需要在 GET url 上复制两个被抱怨的参数...

标签: javascript amazon-web-services amazon-s3 meteor


【解决方案1】:

为什么不使用aws-sdk 包?它为您打包了所有需要的方法。例如,下面是添加文件到存储桶的简单函数:

s3.putObject({
  Bucket: ...,
  ACL: ...,
  Key: ...,
  Metadata: ...,
  ContentType: ...,
  Body: ...,
}, function(err, data) {
  ...
});

【讨论】:

  • 谢谢,这本来可以很明显的。我什至不知道我是怎么错过这个包裹的。我去试试就知道了。
  • 你发送正确吗?这可能有点棘手。如果您在配置方面遇到问题,最好打开一个新问题,因为它有点不同。
  • 我确实提出了一个新问题:stackoverflow.com/questions/19593313/…
【解决方案2】:

直接上传到 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}});
});

【讨论】:

  • 这是一个很老的问题,当时还没有 Slingshot 包。现在有了,而且还不错。直接从浏览器上传文件,所以我接受这个答案,而不是在服务器上工作的休伯特。感谢分享!
【解决方案3】:

查看S3meteor 包。自述文件对如何开始进行了非常全面的演练

【讨论】:

    【解决方案4】:

    首先是添加s3文件上传的包。

    对于安装:添加(AWS SDK 智能包) $ meteor add peerlibrary: aws-sdk

    1.创建指令upload.js并粘贴此代码。

    angular.module('techno')
    .directive("fileupload", [function () {
        return {
            scope: {
                fileupload: "="
            },
            link: function(scope,element, attributes){
                $('.button-collapse').sideNav();
                element.bind("change", function (event) {
                    scope.$apply(function () {
                     scope.fileupload = event.target.files[0];
                 });
               })
           }};
    }]);
    

    2.获取访问密钥并将其粘贴到您的fileUpload.js 文件中。

    AWS.config.update({
    accessKeyId: ' AKIAJ2TLJBEUO6IJLKMN ',
    secretAccessKey: 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'
    });
    
    AWS.config.region = 'us-east-1';
    let bucket = new AWS.S3();
    

    3.现在将此上传代码放入您的指令fileUpload.js

    vm.upload = (Obj) =>{
    vm.loadingButton = true;
    let name = Obj.name;
    let params = {
        Bucket: 'technodheeraj',
        Key: name,
        ContentType: 'application/pdf',
        Body: Obj,
        ServerSideEncryption: 'AES256'
    };
    
    bucket.putObject(params, (err, data) => {
        if (err) {
            console.log('---err------->', err);
        }
        else {
            vm.fileObject = {
                userId: Meteor.userId(),
                eventId: id,
                fileName: name,
                fileSize: fileObj.size,
            };
       vm.call("saveFile", vm.fileObject, (error, result) => {
                if (!error){
                    console.log('File saved successfully');
    
                }
            })
        }
    })
    
    };
    

    4.现在在“saveFile”方法中粘贴这段代码

    saveFile: function(file){
    if(file){
        return Files.insert(file);
    }
    
    };
    

    5.在 HTML 中粘贴此代码

    <input type="file" name="file" fileupload="file">
    <button type="button" class="btn btn-info " ng-click="vm.upload(file)"> Upload File</button>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      相关资源
      最近更新 更多