Parsing multipart body in AWS Lambda function (serverless)

Summary

For those who have worked with the express framework, there are many different libraries that we can use to organize file uploads.
However, most of them are not designed for lambda functions.
In this article we are going to discuss how to parse multipart requests in AWS.
A lambda function, using one of those libraries is 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
First, we will 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 below.

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

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

Then we need to initialize the busboy instance and pass the content type to its constructor. Our code will look like this.

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

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

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

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

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

The last thing we need to do is pass event.body to busboy, in order to parse it. 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 the 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, click 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:
  • DevOps

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.