question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Busboy with AWS Lambda function

See original GitHub issue

Hi Everyone,

I am trying to use busboy to parse multipart/form-data coming from AWS API Gateway to AWS Lambda. I have used LAMBDA_PROXY integration.

My parser is as follows:

export function parseMultipartFormData(event) {
  return new Promise((resolve, reject) => {
    const busboy = new Busboy({
      headers: {
        'content-type': event.headers['Content-Type'],
      },
    });

    const body = {};
    let buffer = '';

    busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
      body.filename = filename;
      body.encoding = encoding;
      body.mimetype = mimetype;

      file.setEncoding('base64');

      file.on('data', (chunk) => {
        buffer += chunk;
      });

      file.once('end', () => {
        body.documentContent = buffer;
      });
    })
      .on('field', (fieldname, val) => {
        body[fieldname] = val;
      })
      .on('finish', () => {
        resolve(body);
      })
      .on('error', (err) => {
        reject(err);
      });

    busboy.end(event.body);
  });
}

Parser is working fine for fields. When I upload a .pdf file it gets corrupted. So I checked the same file’s base_64 encoded string on my local machine using this block of code:

const fs = require('fs');
const { Buffer } = require('buffer');
const path = require('path');

const stream = fs.createReadStream(path.join(__dirname, 'file.pdf'));

let buffer = '';

stream.setEncoding('base64');

stream.on('data', (chunk) => {
  buffer += chunk;
});

stream.on('end', () => {
  console.log(buffer);
});

Then I compared the base_64 encoded string with the string generated by busboy parser and somehow that string is not matching for the same file and encoding.

Is there anything wrong with using busboy with lambda? Or something is wrong in my integration?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
mscdexcommented, Sep 7, 2021

Previously I was using Buffer.concat but it didn’t work

It is the correct way for buffering binary data though and will work for either text or non-text data.

1reaction
mscdexcommented, Sep 7, 2021

If event.body is a base64-encoded string, then you need to either convert it to a Buffer first (from base64) and pass that to busboy.end() instead or pass the encoding ('base64') as the second argument to busboy.end() so that Busboy can correctly deal with the actual raw, binary form data.

Also the file varying between S3 and your local machine sounds suspect. Maybe it did get corrupted during upload to S3 after all?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Missing data when using busboy library to upload file inside of ...
I am trying to use busboy inside a lambda function to process a post request which is supposed to upload an image file....
Read more >
Missing data when using busboy inside Lambda? #199 - GitHub
I am trying to use busboy on a lambda function to process a post request which is supposed to upload an image file....
Read more >
How to Build a serverless backend AWS Lambda function to ...
How to Build a serverless backend AWS Lambda function to upload a file using TypeScript ... Busboy is a streaming parser for HTML...
Read more >
Uploading Images to AWS S3 with Serverless | foqc
AWS S3 is one of the many services provided by Amazon Web Services (AWS), which allows you to store files, most of you...
Read more >
AWS Lambda function handler in Node.js
The Lambda function handler is the method in your function code that processes events. When your function is invoked, Lambda runs the handler...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found