Skip to content Skip to sidebar Skip to footer

Uploading Image To Amazon S3 With Node.js Results In A Small Transparent Square

I am uploading a image to Amazon S3 bucket but when it arrives there, it's a small transparent square, in the future I will use a Front-end application and the file will be uploade

Solution 1:

use putObject rather than upload

await s3.putObject(params, (error, data) => {
  if (error) {
    console.log(error);
  } else {
    console.log(data);
  }
});

also change the contentType & Encoding as:

letparams= {
  Bucket:bucket,
  Key:filename,
  Body:rawdata,
  ContentEncoding:'base64',
  ContentType:'image/jpeg'ACL:'public-read'
};

Solution 2:

Had the same issues and tried to solve it for hours. Seems like the problem is coming from the API Gateway configuration. If you are using the console,go to the settings and add to the Binary Media Types */* or using open api add x-amazon-apigateway-binary-media-types: [ "/" ]` at the top of the file

Solution 3:

req.files[0] will have following data

{ fieldname: 'img', originalname: 'Annotation 2021-01-11 095535.png', encoding: '7bit', mimetype: 'image/png', buffer: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 06 2d 00 00 01 a2 08 06 00 00 00 9b 3e 85 60 00 00 00 01 73 52 47 42 00 ae ce 1c e9 00 00 00 04 ... 38012 more bytes>, size: 38062 }

now try this -

const { originalname, buffer, encoding, mimetype } = req.files[0]
s3.upload ({Bucket: 'somebucket',
        Key: originalname, 
        Body: buffer,
        ContentEncoding: encoding,
        ContentType: mimetype,
    },function (err, data) {
    if (err) {
       console.log("Error", err);
     } if (data) {
       console.log("Upload Success", data.Location);
     }
   })

Post a Comment for "Uploading Image To Amazon S3 With Node.js Results In A Small Transparent Square"