8

I am trying to upload buffer data from an image into S3. It gets uploaded fine. But when I try to download/view the image in S3, it throws an error. I have tried the following -

  1. The image is available to me in Buffer format (JSON). I cannot change this

let image = { "type": "Buffer", "data": [45, 45, 45....]

let buf = new Buffer(image )

let params = {
  Bucket: "bucketName",
  Key: "TestImage123haha.PNG",
  Body: buf ,
  ACL: 'public-read',
  ContentType: 'image/jpeg'
};

s3.upload(params, function(err, data) {
  if (err) {
    console.log('ERROR MSG: ', err);
  } else {
    console.log('Successfully uploaded data' + data.Location);
  }
})
  1. After upoading the image if I try to visit the URL of the bucket where it is stored and view the image, this is what I get -

Response

  1. When I print the buffer data - buf in console , this is what I get -

Result of printing Buffer data in console

Boudhayan Dev
  • 970
  • 4
  • 14
  • 42
  • To be clear, are you doing `let buf = new Buffer(image )` or `let buf = new Buffer(image.data)`? – Joey Kilpatrick Sep 20 '19 at 03:25
  • I have tried both. image.data contain the binary content + the headers that you see in the image attached. – Boudhayan Dev Sep 22 '19 at 14:52
  • Did you find the solution for this issue? – itsHarshad Oct 05 '20 at 13:56
  • I faintly remember trying to remove the headers in the response (check image attached) and then uploading it. Sorry don't remember the details exactly as it is year-old, however, headers were the issue which I am pretty certain of. – Boudhayan Dev Oct 05 '20 at 16:45
  • 1
    @BoudhayanDev Could you please share what was the exact solution? I already spent two weeks. If you share this, it will be very much helpful for me. Thank you – KIRAN K J Nov 07 '21 at 06:15

1 Answers1

1

This problem took me many days but I was able to solve it:

In your API GATE WAY of funtion lambda go to Configuration > Binary types.

enter image description here

Add multipart/form-data

andrecoweb
  • 171
  • 1
  • 2
  • 6