Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing data when using busboy library to upload file inside of a Lambda?

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.

like image 964
Max Crane Avatar asked Oct 16 '25 04:10

Max Crane


1 Answers

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!

like image 166
Miguel Angel López Arcos Avatar answered Oct 17 '25 20:10

Miguel Angel López Arcos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!