[SOLVED] Uploading an image to s3

Hello,

I am trying to upload an image directly into an s3 bucket. So I have a function on the client side that gets the file and then sends it to the server side to upload to s3. I cant send the file directly to the server for some reason because when Itry to do for instance Meteor.call(‘uploadFile’, somefile) some file will just be blank on the server side. So I am using filereader and i’ve tried to read it every way but after it uploads to s3 it says it isnt a valid image file. Can anyone tell me what I May be doing wrong?

Client Side

Template.ShowBucket.events({
  'click #addPhoto' : () => {
    thisFile = document.getElementById('pic').files[0];
    var reader = new FileReader();
    reader.readAsText(thisFile);

    reader.onload = () => {
      var params = {
        Bucket: 'datajournal',
        Key: thisFile.name,
        Body : reader.result,
        ContentType : thisFile.type,
        ACL: 'public-read'
      };
      Meteor.call('uploadToBucket', params, (err, data) => {
        BlazeLayout.reset()
        BlazeLayout.render('MainLayout', {main:'ShowBucket'});
      });
    };

  }
});

SERVER SIDE

Meteor.methods({
  async uploadToBucket(params){

    s3 = new AWS.S3();
    //const upload = await s3.upload(params);
    const test = await s3.upload(params, (err, data) => {
      if(err)
        console.log(err)
      else
        console.log('data' + data);
      });
  }
});

I solved this, Please see the below working code.

Template.ShowBucket.events({
  'click #addPhoto' : () => {
    thisFile = document.getElementById('pic').files[0];
    var reader = new FileReader();
    reader.readAsBinaryString(thisFile);

    reader.onload = () => {
      var params = {
        Bucket: 'datajournal',
        Key: thisFile.name,
        Body :  null,
        ContentType : thisFile.type,
        ACL: 'public-read'
      };
      Meteor.call('uploadToBucket', params, reader.result, (err, data) => {
        BlazeLayout.reset()
        BlazeLayout.render('MainLayout', {main:'ShowBucket'});
      });
    };

  }
});```

async uploadToBucket(params, data){

var base64data = new Buffer(data, 'binary');
params.Body = base64data;
s3 = new AWS.S3();
//const upload = await s3.upload(params);
const test = await s3.upload(params, (err, data) => {
  if(err)
    console.log(err)
  else
    console.log(data);
  });
1 Like