'AWS Lambda image/pdf upload to S3 is corrupted (asp.net core)

I have written a function that uploads a file in an s3 bucket. It works fine when I run my application locally.

But when I deploy the application in AWS Lambda, file upload is working properly but the file is being corrupted. The uploaded file size is a little bit higher than the actual file size.

txt file upload is working fine.

Here is my code

                Guid guid = Guid.NewGuid();
                string extension = System.IO.Path.GetExtension(logo.FileName);
                var fileName = $"{guid}{extension}";
                using (var ms = new System.IO.MemoryStream())
                {
                    logo.CopyTo(ms);
                    ms.Position = 0;
                    System.IO.Stream stream = ms;
                    var client = new AmazonS3Client(AppConstants.S3AccessKey, AppConstants.S3SecretKey, Amazon.RegionEndpoint.USEast1);
                    PutObjectRequest putRequest = new PutObjectRequest
                    {
                        BucketName = AppConstants.S3Bucket,
                        Key = fileName,
                        InputStream = stream
                    };

                    PutObjectResponse response = await client.PutObjectAsync(putRequest);
                }

I have configure API Gateway for binary data as well as change the LambdaEntryPoint with following code

 RegisterResponseContentEncodingForContentType("multipart/form-data", ResponseContentEncoding.Base64);

Is there any other configuration that I missed?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source