I'm in the process of building a Web API with AWS Lambda using .NET Core.
I have run into a problem, where the code piece below work as expected on my Windows machine (Echo the image back), but when deployed to AWS Lambda, the returned image is broken. After further investigation, the echoed back file's size is nearly double the size of the sending file when deployed on AWS?
[HttpPost]
public async Task<IActionResult> Post(IFormFile file)
{
using (var tmpStream = new MemoryStream())
{
await file.CopyToAsync(tmpStream);
var fileExtension = Path.GetExtension(file.FileName);
return File(tmpStream.ToArray(), file.ContentType);
}
}
Am I missing some configuration or overlooking something? AWS Gateway??
(I'm testing the issue via Postman)
Incase anyone is looking for a solution, in addition to adding "multipart/form-data" as binary media type in the API Gateway settings, you need to add a model in the method request body of the resource.
Details can be found at https://github.com/aws/aws-lambda-dotnet/issues/635#issuecomment-616226910
Steps:
public class LambdaEntryPoint : APIGatewayProxyFunction
{
/// <summary>
/// The builder has configuration, logging and Amazon API Gateway already configured. The startup class
/// needs to be configured in this method using the UseStartup<>() method.
/// </summary>
/// <param name="builder"></param>
protected override void Init(IWebHostBuilder builder)
{
RegisterResponseContentEncodingForContentType("multipart/form-data", ResponseContentEncoding.Base64);
builder.UseStartup<Startup>();
}
}
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "MediaFileUpload",
"type": "object",
"properties": {
"file": { "type": "string" }
}
}
EDIT: Added code, and additional steps for clarity, as comment by @JeremyCaney
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With