Missing data when using busboy inside Lambda?
See original GitHub issueHi,
First off, thank you for writing this library. I’m hoping to use it in my current project. I am trying to use busboy on a lambda function to process a post request which is supposed to upload an image file. This is what I have in 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);
and in my lambda function I have this method to get the file contents:
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 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.
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. Thank you!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:13 (5 by maintainers)
Hey sorry for the late response, had the same issue and finally got it working. In my case i use AWS Lambdas so anyone still looking for a solution this is how i did it.
This is not a busboy issue, you just need to enable binary support in API Gateway (https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings-configure-with-console.html)
All your initial code is correct, this is my code:
If you use serverless framework to manage your lambdas you just need to add this to provider setting
Hope this helps.
Thank you @Smoodle, you saved me a lot of time. Here my version with little improvements.