使用Slingshot上传Meteor S3文件

时间:2021-12-16 16:24:12

So.. I am new to Meteor and I am trying to upload to a S3 bucket using edgee:slingshot. I have a settings file in the root with the following info.

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

{

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

}

On the server side I have:

在服务器端我有:

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

  acl: "public-read",

  key: function (file) {

    return file.name;
  }
}); 

On the client side I have:

在客户端我有:

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

I am using Meteor on Windows and the error is:

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

S3:AWS key is undefined

S3:AWS键未定义

Match error: Missing Key 'authorize'.

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

I am not exactly sure why this error is occurring, so help is much appreciated.

我不确定为什么会发生这种错误,所以非常感谢帮助。

Im running my settings.json with meteor run --settings settings.json and it works fine.

我运行我的settings.json与流星运行--settings settings.json,它工作正常。

2 个解决方案

#1


3  

one thing that is missing is the authorize function in the directive, which is required (see the API) so add

缺少的一件事是指令中的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;
  }
}); 

Please note that also maxSize and allowedFileTypes are required, so you should add to client and server side code (e.g. in lib/common.js)

请注意,还需要maxSize和allowedFileTypes,因此您应该添加到客户端和服务器端代码(例如,在lib / common.js中)

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

hope that helps.

希望有所帮助。

#2


0  

Initialize the directive like this on the server side

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

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

Everything else seems to be fine.

其他一切似乎都很好。

#1


3  

one thing that is missing is the authorize function in the directive, which is required (see the API) so add

缺少的一件事是指令中的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;
  }
}); 

Please note that also maxSize and allowedFileTypes are required, so you should add to client and server side code (e.g. in lib/common.js)

请注意,还需要maxSize和allowedFileTypes,因此您应该添加到客户端和服务器端代码(例如,在lib / common.js中)

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

hope that helps.

希望有所帮助。

#2


0  

Initialize the directive like this on the server side

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

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

Everything else seems to be fine.

其他一切似乎都很好。