Search code examples
amazon-web-servicesamazon-s3aws-lambdaaws-api-gatewaysharp

Lambda resizing image in S3 bucket with sharp corrupting image


I have followed this AWS tutorial to add an S3 trigger that resizes an image using sharp and puts it in a different bucket. This all seems to work fine. The problem I have is with fetching the new image using a GET endpoint through API Gateway.

The file returned seems larger than it should be and is not a valid image. If I download the file directly from the S3 UI, the image is fine, which suggests there's something going on with the API Gateway (I followed this tutorial to set that up)

Now here's the strange part...

If I manually upload an image to the bucket that stores the resized images, then download the image through API Gateway, IT WORKS! So the gateway appears fine.

Why would the download seem corrupted only when the image has been resized and downloaded through the API Gateway?

EDIT: -

If I change my GET gateway to use the original bucket that images are uploaded to, images are downloaded fine.


Solution

  • The problem seemed to lie with the resizing script setting the Content-Type to image. I changed it to image/jpeg and it worked.

    This might actually be related to the S3 setting of allowable data type. Maybe if I'd just added image instead of image/jpeg it would work, but I didn't test that.