Search code examples
amazon-web-services.net-coreaws-lambdaimagesharp

Uploading image file to AWS Lambda .Net Core application from browser results in unusable file - how to fix?


I was uploading a jpg image from my browser, as a part of a form, to my .Net Core web api which resides on AWS Lambda. It uploaded but resulted in errors when I tried to process it using ImageSharp. Also I noticed that the file size of the uploaded version was almost double the size of the original. Note that there was no a problem when uploaded locally in my Windows environment running from Visual Studio 2019 Preview. It is the version that was published to AWS Lambda from Visual Studio that was having issues.

Further info: Image arrived into my .NET Controller as an IFormFile within an IForm.


Solution

  • AWS does something to the file before it gets to AWS Lambda. The fix is simple to implement.

    In the Amazon Console, Go to Services, then to the API Gateway page. (Now refer to image below!) Select your API and under your API go to settings. Add these binary types:

    1. image/jpeg
    2. multipart/form-data

    And click Save.

    Image showing AWS Console

    Now click on Resources, then click on Actions and select Deploy API.

    Now select a stage. (I set the stage to Prod in the box that comes up.) Then click on the Deploy button.

    Image showing part of AWS Console

    At this point you should be ready to go. If not, maybe it is a cache issue that has not caught up. If that is the case, perhaps redo the deploy api procedure above. Perhaps that kills time in which the cache refreshes or pushes a cache refresh, but soon it should work one way or another.