【问题标题】:AWS S3 Bucket Upload using CollectionFS and cfs-s3 meteor package使用 CollectionFS 和 cfs-s3 流星包上传 AWS S3 存储桶
【发布时间】:2014-08-05 09:19:09
【问题描述】:

我正在使用 Meteor.js 和 Amazon S3 Bucket 来上传和存储照片。我正在使用陨石包 collectionFS 和 aws-s3。我已经正确设置了我的 aws-s3 连接,并且图像收集工作正常。

客户端事件处理程序:

     'click .submit': function(evt, templ) {
        var user = Meteor.user();
        var photoFile = $('#photoInput').get(0).files[0];

        if(photoFile){

        var readPhoto = new FileReader();

        readPhoto.onload = function(event) {
            photodata = event.target.result;
            console.log("calling method");
            Meteor.call('uploadPhoto', photodata, user);
        };
      }

还有我的服务器端方法:

 'uploadPhoto': function uploadPhoto(photodata, user) {

      var tag = Random.id([10] + "jpg");
      var photoObj = new FS.File({name: tag});
      photoObj.attachData(photodata);
      console.log("s3 method called");

      Images.insert(photoObj, function (err, fileObj) {
        if(err){
          console.log(err, err.stack)
        }else{
          console.log(fileObj._id);
        }
      });

选择的文件是 .jpg 图像文件,但在上传时我在服务器方法上收到此错误:

调用方法 'uploadPhoto' 时出现异常错误:DataMan 构造函数接收到它不支持的数据

无论我是直接传递图像文件,还是将其作为数据附加或使用fileReader读取为文本/二进制/字符串。我仍然得到那个错误。请指教。

【问题讨论】:

  • 您是否有理由不想在客户端进行插入?
  • 也许这是错误的方法 - 但要确保我的 AWS 凭证(密钥 + 机密)安全,而不是将其提供给客户端。在允许上传到存储桶之前,还希望在服务器端对数据进行某些验证。

标签: amazon-web-services amazon-s3 meteor


【解决方案1】:

好吧,也许有些想法。几个月前我已经使用 collectionFS 做了一些事情,所以请注意文档,因为我的示例可能不是 100% 正确。

应通过环境变量设置凭据。因此,您的密钥和秘密仅在服务器上可用。查看此link 以进一步阅读。

好的,首先,这里有一些对我有用的示例代码。检查你的差异。

模板助手:

'dropped #dropzone': function(event, template) {
  addImage(event);
}

函数addImage:

function addImagePreview(event) {
  //Go throw each file,
  FS.Utility.eachFile(event, function(file) {    

    //Some Validationchecks
    var reader = new FileReader();

    reader.onload = (function(theFile) {
      return function(e) {

        var fsFile = new FS.File(image.src);

        //setMetadata, that is validated in collection
        //just own user can update/remove fsFile
        fsFile.metadata = {owner: Meteor.userId()};           

        PostImages.insert(fsFile, function (err, fileObj) {
          if(err) {
            console.log(err);
          }
        });         
      };
    })(file);

    // Read in the image file as a data URL.
    reader.readAsDataURL(file);          
  });   
}

好的,你的下一点是验证。可以使用允许/拒绝规则和 FS.Collection 上的过滤器来完成验证。这样您就可以通过客户端进行所有验证和插入。

例子:

PostImages = new FS.Collection('profileImages', {
  stores: [profileImagesStore],
  filter: {
    maxSize: 3145728,
    allow: {
      contentTypes: ['image/*'],
      extensions: ['png', 'PNG', 'jpg', 'JPG', 'jpeg', 'JPEG']
    }
  },
  onInvalid: function(message) {
    console.log(message);
  }
});


PostImages.allow({
  insert: function(userId, doc) {
    return (userId && doc.metadata.owner === userId);
  },
  update: function(userId, doc, fieldNames, modifier) {
    return (userId === doc.metadata.owner);
  },
  remove: function(userId, doc) {
    return false;
  },
  download: function(userId) {
    return true;
  },
  fetch: []
});

在这里你会发现另一个例子click

另一个错误点可能是您的 aws 配置。你有没有像here写的那样做所有事情?

根据这篇文章click,似乎当 FS.File() 构造不正确时会发生此错误。所以也许这应该是你开始的第一个方法。

阅读量很大,希望对你有帮助:)

【讨论】:

  • 感谢您。只是通过这个工作。环境变量确实有效。
  • 这确实有效 - 唯一的问题是 contentType 在上传到 S3 时似乎默认返回到 application/octet-stream。虽然 FS.File 中的类型明显是 image/jpeg 或 image/png。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-07
  • 1970-01-01
  • 2018-07-11
  • 1970-01-01
  • 2018-04-25
  • 2017-03-13
  • 1970-01-01
相关资源
最近更新 更多