Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse multipart/form-data from body as string on AWS Lambda

I'm glad to see AWS now supports multipart/form-data on AWS Lambda, but now that the raw data is in my lambda function how do I process it?

I see multiparty is a good multipart library in Node for multipart processing, but its constructor expects a request, not a raw string.

The input message I am receiving on my Lambda function (after the body mapping template has been applied) is:

{ "rawBody": "--ce0741b2-93d4-4865-a7d6-20ca51fe2689\r\nContent-Disposition: form-data; name=\"Content-Type\"\r\n\r\nmultipart/mixed; boundary=\"------------020601070403020003080006\"\r\n--ce0741b2-93d4-4865-a7d6-20ca51fe2689\r\nContent-Disposition: form-data; name=\"Date\"\r\n\r\nFri, 26 Apr 2013 11:50:29 -0700\r\n--ce0741b2-93d4-4865-a7d6-20ca51fe2689\r\nContent-Disposition: form-data; name=\"From\"\r\n\r\nBob <[email protected]>\r\n--ce0741b2-93d4-4865-a7d6-20ca51fe2689\r\nContent-Disposition: form-data; name=\"In-Reply-To\"\r...  

etc and some file data.

The body mapping template I'm using is

{   "rawBody" : "$util.escapeJavaScript($input.body).replaceAll("\\'", "'")" } 

How can I parse this data to acecss the fields and files posted to my Lambda function?

like image 596
Diederik Avatar asked Jul 26 '16 20:07

Diederik


People also ask

Can lambda function have multiple files?

At the end you want to send multiple files to a lambda function as a zip and use these files inside the lambda_handler() function . You need to create all your files inside a directory in such a way that they can be imported in other files easily and zip your lambda function and use it.

How do you use multipart form data?

Multipart form data: The ENCTYPE attribute of <form> tag specifies the method of encoding for the form data. It is one of the two ways of encoding the HTML form. It is specifically used when file uploading is required in HTML form. It sends the form data to server in multiple parts because of large size of file.

Can we clone lambda function in AWS?

There is no provided function to copy/clone Lambda Functions and API Gateway configurations. You will need to create new a new function from scratch. If you envision having to duplicate functions in the future, it may be worthwhile to use AWS CloudFormation to create your Lambda Functions.


1 Answers

busboy doesn't work for me in the "file" case. It didn't throw an exception so I couldn't handle exception in lambda at all.

I'm using aws-lambda-multipart-parser lib wasn't hard like so. It just parses data from event.body and returns data as Buffer or text.

Usage:

const multipart = require('aws-lambda-multipart-parser');  const result = multipart.parse(event, spotText) // spotText === true response file will be Buffer and spotText === false: String 

Response data:

{     "file": {         "type": "file",         "filename": "lorem.txt",         "contentType": "text/plain",         "content": {             "type": "Buffer",             "data": [ ... byte array ... ]         } or String     },     "field": "value" } 
like image 59
Long Nguyen Avatar answered Sep 16 '22 11:09

Long Nguyen