Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

I upload an image file using the following format:

var body = fs.createReadStream(tempPath).pipe(zlib.createGzip());

var s3obj = new AWS.S3({params: {Bucket: myBucket, Key: myKey}});

var params = {

  Body: body,

  ACL: 'public-read',

  ContentType: 'image/png'

};

s3obj.upload(params, function(err, data) {

  if (err) console.log("An error occurred with S3 fig upload: ", err);

  console.log("Uploaded the image file at: ", data.Location);

});

The image successfully uploads to my S3 bucket (there are no error messages and I see it in the S3-console), but when I try to display it on my website, it returns a broken img icon. When I download the image using the S3-console file downloader I am unable to open it with the error that the file is "damaged or corrupted".

If I upload a file manually using the S3-console, I can correctly display it on my website, so I'm pretty sure there's something wrong with how I'm uploading.

What is going wrong?

1 Answer

0 votes
by (44.4k points)

post one more parameter to the file because it was zipped fixed the problem:

var params = {

  Body: body,

  ACL: 'public-read',

  ContentType: 'image/png',

  ContentEncoding: 'gzip'

};

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...