如何使用流星弹弓正确上传到亚马逊 S3



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

调用"弹弓/上传请求"的传递结果时出现异常:类型错误:>无法读取未定义的属性'响应'

这是我的服务器端代码:

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 命令块中引用区域似乎也很重要,例如

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)都有一个设置文件,这些文件都作为同一代码库的一部分,并在运行时针对所需的环境进行选择。

在下面行失败。 xhr 为空

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

记录如下所示的错误并从那里进行调试

console.error('Error uploading', error);

最新更新