How to upload Excel to Amazon S3 using AWS Lamda and dotnet Core 5?

Viewed 476

So i am using AWS Lamda project to upload excel file to S3. It got upload on S3 but issue is it got corrupted once uploaded. It is working fine on local but when i deploy it to AWS it does not work.

Below is my code.

Controller.cs

IFormFile file = Request.Form.Files[0];
UploadtoS3("Test", file, file.FileName);

UploadtoS3 Method

UploadtoS3(string bucketName, IFormFile file, string fileName)
{
    System.IO.Stream MyStream = file.OpenReadStream();
            
    PutObjectRequest request = new PutObjectRequest
    {
       BucketName = bucketName,
       Key = fileName,
       InputStream = MyStream,
       ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"                    
    };

    PutObjectResponse response = await _client.PutObjectAsync(request);
}

AWS API Gateway

I have also added possible binary types.

enter image description here

Please help. Thanks in Advance

1 Answers

Faced with a similar problem. In my case, the redeployment stage for the api helped

Also updated LambdaProxyFunction

  protected override void Init(IWebHostBuilder builder)
{
  builder
      .UseStartup<Startup>();
        
  RegisterResponseContentEncodingForContentType("multipart/form-data", ResponseContentEncoding.Base64);
  RegisterResponseContentEncodingForContentType("application/vnd.ms-excel", ResponseContentEncoding.Base64);
  RegisterResponseContentEncodingForContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ResponseContentEncoding.Base64);
  RegisterResponseContentEncodingForContentType("application/vnd.ms-excel.sheet.macroEnabled.12", ResponseContentEncoding.Base64);

    }
Related