0

I'm having an issue with .NET 6 framework regarding the uploading of image files to S3 using the AWS SDK.

When I POST to my endpoint running on a local IIES it works perfectly and I can see the generated file in S3 without any issues.

The problem is the following: After a serverless deployment to AWS Lambda, the same .NET Core endpoint that produced a perfect result in my local environment behaves way different when it's running on a lambda. when I try to open the image it shows a square dot at the center but no image.

I am using IFormFile and here is my code

 public async Task<string> Upload(IFormFile formfile, string name)
        {
            var xbuilder = WebApplication.CreateBuilder();
            var _AwsSetting = xbuilder.Configuration.GetSection("AwsCredentials").Get<AWSCredentials>();

            var accessKey = _AwsSetting.AWSAccessKey;
            var secretKey = _AwsSetting.AWSSecretAccessKey;
            RegionEndpoint bucketRegion = RegionEndpoint.APSouth1;
            var bucketName = _AwsSetting.AWSS3BucketName;
            var location = $"{name + Path.GetExtension(formfile.FileName)}";
            var contentType = formfile.ContentType;
            var client = new AmazonS3Client(accessKey, secretKey, bucketRegion);
           
            try
            {
                using (var stream = new MemoryStream())
                {
                   await formfile.CopyToAsync(stream);

                        var putRequest = new PutObjectRequest()
                        {

                            Key = location,
                            BucketName = bucketName,
                            InputStream = stream,
                            CannedACL = S3CannedACL.PublicRead,
                            ContentType=contentType

                        };
                        await client.PutObjectAsync(putRequest);
                        string publicUrl = string.Empty;
                        publicUrl = $"https://{bucketName}.s3.{bucketRegion.SystemName}.amazonaws.com/{location}";
                        return publicUrl;
                }
            }
            catch (Exception e)
            {
                throw e;
            }

        }
  • _".NET 6 framework"_ - there is _no such thing_ as ".NET 6 Framework" - you're confusing .NET Framework 4.x with ".NET 6". – Dai Sep 03 '22 at 08:29
  • 1
    `throw e;` <-- **don't do this**, because it resets the captured stack-trace. [Instead either re-throw using the `throw;` keyword by itself or re-throw `e` as an `InnerException`](https://stackoverflow.com/questions/22623/best-practices-for-catching-and-re-throwing-net-exceptions). – Dai Sep 03 '22 at 08:30
  • Your `MemoryStream` is redundant. You should be able to pass `formFile`s inner `Stream` directly to `PutObjectRequest.InputStream` without intermediate buffering (which is basically wasting memory for no good reason) – Dai Sep 03 '22 at 08:31
  • `await client.PutObjectAsync(putRequest);` <-- Your code is ignoring the `PutObjectResponse` return-value from `PutObjectAsync`. You **should always** inspect return-values as they'll indicate error conditions and other problems that aren't bad enough to throw an exception). – Dai Sep 03 '22 at 08:33

0 Answers0