Parsing multipart body in AWS Lambda function (serverless)

Summary

For those who’s worked with the express framework, there are tonnes of different libraries that we can use to organise file upload.
Whereas, most of them are not designed for lambda functions.
In this article, we are going to discuss how to parse multipart requests in AWS.
Lambda function, using one of those libraries, called busboy.Busboy is a Node js module for parsing incoming HTML form data.

Parser module

Let’s write whole parsing code in one function called multipartParser.js
At first, we need to install busboy, in order to do that please follow this link https://github.com/mscdex/busboy

Once installed we import it to our parser module in this form

The main function in our parser should receive an {event} object as an argument, the same event object from lambda function
arguments and return a promise.

Busboy needs content type header to start parsing.
We can get that header from event.headers, this part is little bit tricky if you are using the serverless offline plugin on your local content type will be “Content-Type” in case you deploy your function to AWS content type will be “content-type”, so in order to check which one is available we will create the separate function
to get the actual content type.

Then we need to initialize busboy instance and pass the content type to its constructor, so our code becomes like this.

Next, we define a variable to parsed data in it.

Next, we add event listener to our busboy instance which will fire once any file will be found in event.body

 

As you can see the file is a stream that’s why we set on data listener to get entire file data and at the end, we just grab filename and mime type and assign it to our result object.
It is known that multipart form data can contain files and fields.
So we need one more listener on busboy to grab fields as well.

At the end, we need to set error listener on busboy, so if something goes wrong we can reject the promise

And of course finish listener, so once everything is parsed we resolve a promise with event object by rewriting its body with
our result object which contains files and fields.

The last thing we need to do, we should pass event.body to busboy, in order to it to parse, as a second argument we pass
encoding if you are using API Gateway Proxy Integration you may have a param isBase64Encoded with tells integration request
to encode body with base64, if it is set to false, we just set encoding to binary.
To learn more about API Gateway Proxy Integration refer to this link:

http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html

At the end, we call

So the final module looks like this.

 

One more note, when you deploy your function, make sure to enable binary support in API gateway. Otherwise binary data will be broken, except those which has text content. To implement that, log in your AWS console and search for a service API Gateway, on the left side you should find all the available apps. Once you found yours, clicking on it and you will see the list of available configuration, then select the one called Binary support.
On the right side, you can specify all the binary content types you need.

  • Topics:
  • nodejs

Top Stories

High Five! You just read 2 awesome articles, in row. You may want to subscribe to our blog newsletter for new blog posts.