Search code examples
c#asp.netparsingaws-lambdamultipartform-data

Parse raw multipart/form-data in lambda annotation framework .NET


Give me code for ParseMultipartFormData function.

[LambdaFunction()]
[HttpApi(LambdaHttpMethod.Post, "/create")]
public async Task<IActionResult> AddEntity([FromBody] APIGatewayHttpApiV2ProxyRequest request)
{
    var contentType = request.Headers["content-type"];

    if (contentType.Contains("multipart/form-data"))
    {
        var formData = ParseMultipartFormData(request.Body, contentType);
        var fileContent = formData["file"]; // Process file here
    }

    return Ok(new { message = "Data added successfully" });
}

Solution

  • This is not a recommend approach to post files on AWS Lambda. As AWS Lambda has a payload limit of 6MB.

    What instead you should do is - create an S3 pre-signed URL, and return that URL from the API. Use that pre-signed URL to upload file directly to S3, and then invoke another API just to save the updates.

    This way, your API will be lightweight, no need to deal with the file objects in C# code. Just make sure, you keep the timeout of pre-signed URL minimal.