4

I am trying to use busboy inside a lambda function to process a post request which is supposed to upload an image file. I notice that the whole file content is not making it to be parsed by busboy.

I tried changing the call to busboy.write to just use 'base64' since it looks like the file arrives in binary, but that didn't work either.

my client code

const formData = new FormData();
formData.append("file", params.file, params.file.name);

const request = new XMLHttpRequest();
request.open("POST", "https://myapi/uploadphoto");
request.setRequestHeader('Authorization', this.props.idToken);
request.send(formData);

my lambda code

function getFile(event) {
   const busboy = new Busboy({headers: event.headers});
   const result = {};

   return new Promise((resolve, reject) => {
      busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
         file.on('data', data => {
            result.content = data;
            console.log("got data... " + data.length + ' bytes');
         });

         file.on('end', () => {
            result.filename = filename;
            result.contentType = mimetype;
            resolve(result);
         });
      });

      busboy.on('error', error => reject(error));
      busboy.write(event.body, event.isBase64Encoded ? 'base64' : 'binary');
      busboy.end();
   });
}

When trying with an example photo, I notice that the "got data" console log is showing me that I am not receiving the whole file. The file I am using is 229707 bytes but the console log says that it received 217351 bytes.

I am wondering if I am using busboy wrong or if this is some quirk of lambda + api gateway. Any ideas or help troubleshooting is much appreciated.

Max Crane
  • 137
  • 1
  • 3
  • 7

1 Answers1

2

I was struggling with this issue too, but in the end it was a problem with API Gateway.

I was able to solve the problem by adding multipart/form-data as a binary media type inside the settings of API Gateway.

To do it go to API Gateway > "Your API" > Settings > Add Binary Media Type and add multipart/form-data.

After that, deploy again your API and it should work.

I hope this helps anyone!