Reputation: 79
I have project .net core serverless Application
public APIGatewayProxyResponse UploadFile(APIGatewayProxyRequest request, ILambdaContext context)
{
FileModel model = JsonConvert.DeserializeObject<FileModel>(request.Body);
return new APIGatewayProxyResponse
{
StatusCode = (int)HttpStatusCode.OK,
Body = JsonConvert.SerializeObject(new Dictionary<string, string>
{
{ "Code",$"{model.filename}"},
{ "Description",$"File Recieved"}
})
};
}
I have the Lambda function which should receive file using ApiGatewayProxyRequest I am sending file through postman using formdata to upload the file. I receive nothing inside lambda function How can I receive File using this approach? Thank you in advance
Upvotes: 1
Views: 2187
Reputation: 1078
I was struggling with the same issue where I need to process an audio file in lambda function and need to send that to an external API. I used a package HttpMultiParser in the NuGet package manager to parse the file along with other form data parameters.
Below is a sample function to parse the file as well as other parameters. The input for this function is the request.body
of the APIGatewayHttpApiV2ProxyRequest
object.
private MyModel ExtractFormData(string requestBody)
{
try
{
var parser = HttpMultipartParser.MultipartFormDataParser.Parse(new MemoryStream(Convert.FromBase64String(requestBody)));
var file = parser.Files.First();
Stream audioFile = file.Data;
MyModel extractedData = new MyModel
{
FirstName = Convert.ToString(parser.GetParameterValue("FirstName")), //STRING
LastName = Convert.ToString(parser.GetParameterValue("LastName")), //STRING
VoiceFile = audioFile //byte[]
};
return extractedData;
}
catch (Exception e)
{
LambdaLogger.Log("CUSTOM LOG : Exception e: " + JsonConvert.SerializeObject(e));
return new MyModel();
}
}
By the way, it's ok to send small files to Lambda but is not meant to handle large files. You could upload the file to S3 bucket and process it with the Lambda function.
Upvotes: 1