Image getting corrupted while uploading to S3 via REST API deployed to AWS Lambda

0

I have an endpoint in my REST API developed in ASP.NET Core Web API and deployed to AWS Lambda which is responsible to upload image in AWS S3.

When I run the API locally it's able to upload the image successfully and when I downloading the same image from S3 it shows correctly in image viewer app.

But when I deploy the REST API to AWS Lambda and use the same API endpoint to upload the image we get 200 OK response from the API but when I download the same image from S3 it says the image is corrupted and gives below error.

What could be the possible reason that my API returns 200 OK for image upload but actually it doesn't upload successfully.

Image_error

C# code to upload image.

` var file = Request.Form.Files[0];

var BucketName = bucketName;

            using (var fileTransferUtility = new TransferUtility(_s3Client))
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadRequest = new TransferUtilityUploadRequest
                    {
                        InputStream = stream,
                        BucketName = BucketName,
                        Key = file.FileName,
                        ContentType = 
                        GetFileExtension(file.FileName) == "jpg" ? "image/jpeg" 
                                    : GetFileExtension(file.FileName) == "png" ? "image/png" : "image/jpeg" // Set the correct Content-Type here
                    };
                    await fileTransferUtility.UploadAsync(uploadRequest).ConfigureAwait(false);
                }
            }

`

1 Answer
0
Accepted Answer

This was resolved by doing the change in AWS API Gateway which was invoking AWS Lambda.

api_gateway_changes

Once changes we have deploy the API Gateway once again.

Atif
answered 6 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions