【问题标题】:Meteor: Not able to upload image to S3 using CollectionFSMeteor:无法使用 CollectionFS 将图像上传到 S3
【发布时间】:2015-05-17 20:41:15
【问题描述】:

我正在尝试使用this 指南测试上传功能,唯一的例外是使用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 包,现在一切正常。所以基本上,问题在于允许/拒绝规则,但我实际上正在这样做。我不知道为什么它不承认规则。

【问题讨论】:

    标签: javascript file-upload meteor amazon-s3


    【解决方案1】:

    您需要在仅服务器代码中定义 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 页面上提到了使用 env vars 的便利性

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

    【讨论】:

    • 有关 CollectionFS 安全配置的更多详细信息,请参阅Security 上的 wiki 页面
    猜你喜欢
    • 2018-02-18
    • 2016-03-21
    • 1970-01-01
    • 2020-07-07
    • 2017-01-29
    • 2015-09-03
    • 2017-07-29
    • 1970-01-01
    • 2015-06-01
    相关资源
    最近更新 更多