流星:无法使用 CollectionFS 将图像上传到 S3

Meteor: Not able to upload image to S3 using CollectionFS

我正在尝试使用 this guide with the only exception of using cfs-s3 包测试上传功能。这是非常基本的简单代码,但我在客户端控制台上收到错误 - Error: Access denied. No allow validators set on restricted collection for method 'insert'. [403]

尽管我已经以各种可能的方式设置了 allow insert,但还是出现了这个错误。

这是我的客户端代码:

// client/images.js
var imageStore = new FS.Store.S3("images");

Images = new FS.Collection("images", {
    stores: [imageStore],
    filter: {
        allow: {
            contentTypes: ['image/*']
        }
    }
});

Images.deny({
 insert: function(){
 return false;
 },
 update: function(){
 return false;
 },
 remove: function(){
 return false;
 },
 download: function(){
 return false;
 }
 });

Images.allow({
 insert: function(){
 return true;
 },
 update: function(){
 return true;
 },
 remove: function(){
 return true;
 },
 download: function(){
 return true;
 }
});

而且首页有一个简单的文件输入按钮-

// client/home.js
'change .myFileInput': function(e, t) {
    FS.Utility.eachFile(e, function(file) {
        Images.insert(file, function (err, fileObj) {
          if (err){
             console.log(err)  // --- THIS is the error
          } else {
             // handle success depending what you need to do

            console.log("fileObj id: " + fileObj._id)
            //Meteor.users.update(userId, {$set: imagesURL});
          }
        });
     });
}

我已经在 S3 上设置了正确的策略和所有内容,但我认为这个错误根本与 S3 无关。

// server/images.js
var imageStore = new FS.Store.S3("images", {
    accessKeyId: "xxxx",
    secretAccessKey: "xxxx",
    bucket: "www.mybucket.com"
});

Images = new FS.Collection("images", {
    stores: [imageStore],
    filter: {
        allow: {
            contentTypes: ['image/*']
        }
    }
});

我也适当地发布和订阅了这些合集。我已经挖了几个小时,但似乎无法弄清楚发生了什么。

编辑: 我刚刚重新添加了 insecure 包,现在一切正常。所以基本上,问题出在 allow/deny 规则上,但实际上我正在这样做。我不确定为什么它不承认规则。

您需要在纯服务器代码中定义 FS.Collection 的 allow/deny 规则。这些是应用于 FS.Collection 创建的基础 Mongo.Collection 的服务器端规则。

最好的方法是将 AWS 密钥导出为以下环境变量:AWS_ACCESS_KEY_ID、AWS_SECRET_ACCESS_KEY,从 FS.Store 中删除 accessKeyId 和 secretAccessKey 选项,然后移动FS.Collection 构造函数在客户端和服务器上调用 运行。 cfs:s3 页面

中提到了使用环境变量的便利性

除此之外,您还可以使用 Meteor.settings.public 控制存储桶名称,这在您希望根据环境使用不同的存储桶时非常方便。