【问题标题】:Meteor S3 file upload with Slingshot使用 Slingshot 上传 Meteor S3 文件
【发布时间】:2015-06-16 10:27:32
【问题描述】:

所以.. 我是 Meteor 的新手,我正在尝试使用 edgee:slingshot 上传到 S3 存储桶。我在根目录中有一个包含以下信息的设置文件。

{

  "AWSAccessKeyId": "Key",
  "AWSSecretAccessKey": "Key"

}

在服务器端我有:

Slingshot.createDirective("Test", Slingshot.S3Storage, {
  bucket: "test",

  acl: "public-read",

  key: function (file) {

    return file.name;
  }
}); 

在客户端我有:

 var doc = document.implementation.createHTMLDocument("New Document");
  var p = doc.createElement("p");
  p.innerHTML = "This is a new paragraph.";

  try {
    doc.body.appendChild(p);
    console.log(doc);
  } catch(e) {
    console.log(e);
  }

  var uploader = new Slingshot.Upload("Test");

uploader.send(doc, function (error, downloadUrl) {
  if (error) {

    console.error('Error uploading', uploader.xhr.response);
    alert (error);
  }
  else{
    console.log("Worked!");
  }
});

我在 Windows 上使用 Meteor,错误是:

S3:AWS 密钥未定义

匹配错误:缺少“授权”密钥。

我不确定为什么会发生此错误,因此非常感谢您的帮助。

我正在使用meteor run --settings settings.json 运行我的settings.json,它运行良好。

【问题讨论】:

  • 你在settings.json 上有那个键吗?如果是,你应该运行meteor meteor --settings settings.json
  • 是的!我正在运行meteor run --settings settings.json
  • 似乎您缺少存储桶。 bucket String (required) - Name of bucket to use. The default is Meteor.settings.S3Bucket.
  • 如何在我的代码中实现它?对不起,我是这方面的初学者..
  • 我从不使用 slingshot,也找不到 setting.json 示例,但尝试使用 "bucket":"S3Bucket"Meteor.settings.S3Bucket

标签: node.js file-upload meteor amazon-s3 meteor-slingshot


【解决方案1】:

缺少的一件事是指令中的authorize 函数,这是必需的(请参阅API)所以添加

Slingshot.createDirective("Test", Slingshot.S3Storage, {
  bucket: "test",

  acl: "public-read",

  authorize: function () {
    // do some validation
    // e.g. deny uploads if user is not logged in.
    if (!this.userId) {
      throw new Meteor.Error(403, "Login Required");
     }

    return true;
  },

  key: function (file) {

    return file.name;
  }
}); 

请注意,maxSizeallowedFileTypes 也是必需的,因此您应该添加到客户端和服务器端代码(例如在 lib/common.js 中)

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

希望有帮助。

【讨论】:

    【解决方案2】:

    在服务器端像这样初始化指令

    Slingshot.createDirective('Test', Slingshot.S3Storage, {
      bucket: 'test',
      maxSize: 1024 * 1024 * 1,
      acl: 'public-read',
      region: AWS_REGION_OF_UR_BUCKET,
      AWSAccessKeyId: YOUR_AWS_ACCESS_KEY_ID,
      AWSSecretAccessKey: YOUR_AWS_SECRET_ACCESS_KEY,
      allowedFileTypes: ['image/png', 'image/jpeg', 'image/gif'],
      authorize: function() {
       var message;
       if (!this.userId) {
        message = 'Please login before posting files';
        throw new Meteor.Error('Login Required', message);
       }
       return true;
      },
      key: function(file) {
        // admin would be the folder and file would be saved with a timestamp
        return 'admin/' + Date.now() + file.name;
      }
     });
    

    其他一切似乎都很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-16
      • 2016-10-11
      • 2013-11-03
      • 2016-12-23
      • 1970-01-01
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多