AWS S3 Bucket Upload using CollectionFS and cfs-s3 meteor package

zakir2k

I am using Meteor.js with Amazon S3 Bucket for uploading and storing photos. I am using the meteorite packges collectionFS and aws-s3. I have setup my aws-s3 connection correctly and the images collection is working fine.

Client side event handler:

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

And my server side method:

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

The file that is selected is a .jpg image file but upon upload I get this error on the server method:

Exception while invoking method 'uploadPhoto' Error: DataMan constructor received data that it doesn't support

And no matter whether I directly pass the image file, or attach it as data or use the fileReader to read as text/binary/string. I still get that error. Please advise.

chaosbohne

Ok, maybe some thoughts. I have done things with collectionFS some months ago, so take care to the docs, because my examples maybe not 100% correct.

Credentials should be set via environment variables. So your key and secret is available on server only. Check this link for further reading.

Ok first, here is some example code which is working for me. Check yours for differences.

Template helper:

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

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

Ok, your next point is the validation. The validation can be done with allow/deny rules and with a filter on the FS.Collection. This way you can do all your validation AND insert via client.

Example:

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

Here you will find another example click

Another point of error is maybe your aws configuration. Have you done everything like it is written here?

Based on this post click it seems that this error occures when FS.File() is not constructed correctly. So maybe this should be you first way to start.

A lot for reading so i hope this helps you :)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

CollectionFS 및 cfs-s3 meteor 패키지를 사용한 AWS S3 버킷 업로드

분류에서Dev

how to upload larger file( greater than 12 MB) to aws s3 bucket in using salesforce apex

분류에서Dev

aws s3 php: fails to upload directory using UploadSyncBuilder

분류에서Dev

upload file using flask to amazon s3

분류에서Dev

Grant access to AWS S3 bucket/folder to users without AWS account

분류에서Dev

AWS 리소스를 사용하여 'aws s3 sync s3 : // bucket1 s3 : // bucket2'를 예약하는 방법은 무엇입니까?

분류에서Dev

cfs : gridfs에서 cfs : s3으로 마이그레이션

분류에서Dev

Is it possible to access a public AWS S3 bucket without providing keys?

분류에서Dev

AWS S3 권한-put-bucket-acl 오류

분류에서Dev

uploading to S3 bucket from Codeigniter

분류에서Dev

How to Give Access to non-public Amazon S3 bucket folders using Parse authenticated user

분류에서Dev

Upload Files directly to S3 chunk-by-chunk using Play Scala using Iteratees

분류에서Dev

AWS S3 Download Link Issues

분류에서Dev

nodejs, multer, aws S3

분류에서Dev

AWS S3 putBucketLifecycleConfiguration overwiting

분류에서Dev

AWS :: S3 :: Errors :: InvalidAccessKeyId

분류에서Dev

Druid not storing to AWS S3

분류에서Dev

Rails AWS assets on Cloudfront & s3

분류에서Dev

AWS S3 Java API

분류에서Dev

AWS S3, CloudFront 및 SSL

분류에서Dev

CORS AWS S3 및 Cloudfront

분류에서Dev

Deploy Maven to s3 bucket from jenkins

분류에서Dev

Cost efficient way of changing storage class on an S3 bucket

분류에서Dev

Run image processing algorithn in python on S3 bucket

분류에서Dev

Amazon S3 managing files in versioned bucket

분류에서Dev

Downloading S3 files(objects) using aws-sdk for ruby in rails

분류에서Dev

AWS S3 client side encryption using KMS - Region being ignored

분류에서Dev

`aws s3 cp` vs`aws s3 sync` 동작 및 비용

분류에서Dev

예외 해결 방법 ** System.MissingMethodException : ** 'Method not found : void Amazon.S3.Transfer.TransferUtility.Upload ()'(Xamarin AWS S3)

Related 관련 기사

  1. 1

    CollectionFS 및 cfs-s3 meteor 패키지를 사용한 AWS S3 버킷 업로드

  2. 2

    how to upload larger file( greater than 12 MB) to aws s3 bucket in using salesforce apex

  3. 3

    aws s3 php: fails to upload directory using UploadSyncBuilder

  4. 4

    upload file using flask to amazon s3

  5. 5

    Grant access to AWS S3 bucket/folder to users without AWS account

  6. 6

    AWS 리소스를 사용하여 'aws s3 sync s3 : // bucket1 s3 : // bucket2'를 예약하는 방법은 무엇입니까?

  7. 7

    cfs : gridfs에서 cfs : s3으로 마이그레이션

  8. 8

    Is it possible to access a public AWS S3 bucket without providing keys?

  9. 9

    AWS S3 권한-put-bucket-acl 오류

  10. 10

    uploading to S3 bucket from Codeigniter

  11. 11

    How to Give Access to non-public Amazon S3 bucket folders using Parse authenticated user

  12. 12

    Upload Files directly to S3 chunk-by-chunk using Play Scala using Iteratees

  13. 13

    AWS S3 Download Link Issues

  14. 14

    nodejs, multer, aws S3

  15. 15

    AWS S3 putBucketLifecycleConfiguration overwiting

  16. 16

    AWS :: S3 :: Errors :: InvalidAccessKeyId

  17. 17

    Druid not storing to AWS S3

  18. 18

    Rails AWS assets on Cloudfront & s3

  19. 19

    AWS S3 Java API

  20. 20

    AWS S3, CloudFront 및 SSL

  21. 21

    CORS AWS S3 및 Cloudfront

  22. 22

    Deploy Maven to s3 bucket from jenkins

  23. 23

    Cost efficient way of changing storage class on an S3 bucket

  24. 24

    Run image processing algorithn in python on S3 bucket

  25. 25

    Amazon S3 managing files in versioned bucket

  26. 26

    Downloading S3 files(objects) using aws-sdk for ruby in rails

  27. 27

    AWS S3 client side encryption using KMS - Region being ignored

  28. 28

    `aws s3 cp` vs`aws s3 sync` 동작 및 비용

  29. 29

    예외 해결 방법 ** System.MissingMethodException : ** 'Method not found : void Amazon.S3.Transfer.TransferUtility.Upload ()'(Xamarin AWS S3)

뜨겁다태그

보관