node.js - 如何使用 meteor 弹弓正确上传到Amazon S3?

标签 node.js amazon-web-services meteor amazon-s3 meteor-slingshot

我正在尝试上传到 S3,但 Web 控制台中不断出现的错误是

Exception in delivering result of invoking 'slingshot/uploadRequest': TypeError: >Cannot read property 'response' of undefined

这是我的服务器端代码:

Slingshot.fileRestrictions("Test1", {
  allowedFileTypes: ["image/png", "image/jpeg", "image/gif"],
  maxSize: 10 * 1024 * 1024 // 10 MB (use null for unlimited)
});


Slingshot.createDirective("Test1", Slingshot.S3Storage, {

  AWSAccessKeyId: "Key",
  AWSSecretAccessKey: "Key",
  bucket: "bucketname",


  acl: "public-read",

  authorize: function () {
    //Deny uploads if user is not logged in.

    },

  key: function (file) {
    //Store file into a directory by the user's username.
    return file.name;
  }

客户端代码:

Template.first.events({
    'change .fileInput': function(event, template) {

      event.preventDefault();

var uploader = new Slingshot.Upload("Test1");
var docc = document.getElementById('fileup').files[0];
console.log(docc);

uploader.send(docc, function (error){
  if (error) {


    console.error('Error uploading', uploader.xhr.response);

    alert (error);
  }
  else{
    console.log("Worked!");
  }

  });
  }

非常感谢您的帮助!

最佳答案

在 createDirective 命令 block 中引用区域似乎也很重要,例如

Slingshot.createDirective("Test1", Slingshot.S3Storage, {

  bucket: "bucketname",
  region: "eu-west-1"
  ...

如果没有明确指定区域,我得到的只是“400 - 错误请求”。

AWSAccessKeyId 和 AWSSecretAccessKey 应放入单独的 settings.json 文件中,该文件仅限于您的服务器部署并从源代码管理 (github) 中排除。

设置.json:

{
  "AWSAccessKeyId":     "<insert key id here>",
  "AWSSecretAccessKey": "<insert access key here>"
}

通常,每个环境(dev、acpt、live)都有一个设置文件,所有这些都作为同一代码库的一部分,并在运行时为所需的环境进行选择。

关于node.js - 如何使用 meteor 弹弓正确上传到Amazon S3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29586029/

相关文章:

java - 通过 maven for Spring Boot 应用程序为 AWS Beanstalk 创建 .ebextensions 的正确方法是什么

Accounts.onCreateUser 中的 Meteor Promise

objective-c - 使用 NSTask 执行 ionic 构建命令 - 无法访问启动路径

node.js - 我如何让 D3 读取 Handlebars 上下文对象

amazon-web-services - 尝试创建一个 aws_route53_record 以指向负载均衡器,但使用 terraform 不断收到错误

javascript - DynamoDB 查询错误 : Multiple Validation Error

node.js - 将 Meteor 项目从 1.5.2.2 更新到 1.6 时出错

javascript - 如何注册 Meteor Spacebars 助手?

node.js - '错误: self signed certificate' with node+express application

node.js - 在 Mocha 测试中响应 NodeJS 提示