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);
                }
            }

`

Atif
已提問 7 個月前檢視次數 650 次
1 個回答
0
已接受的答案

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
已回答 6 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南