使用CollectionFS和cfs-s3流星包上传AWS S3桶



我使用Meteor.js与Amazon S3 Bucket上传和存储照片。我使用的是陨石包集合fs和laws -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'方法时出现异常

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

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

凭据应该通过环境变量设置。因此,您的密钥和秘密仅在服务器上可用。点击此链接进一步阅读

好的,首先,这里是一些示例代码,这是为我工作。检查你的差异

模板辅助:

'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: []
});

这里你会发现另一个例子点击

另一个错误点可能是您的aws配置。你按这里写的做了吗?

基于这个帖子点击,似乎这个错误发生时FS.File()没有正确构造。所以也许这应该是你开始的第一个方法。

很多阅读,所以我希望这对你有帮助:)

最新更新