0 votes
1 view
in AWS by (18.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 (42.2k 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'

};

...