[英]Missing data when using busboy library to upload file inside of a Lambda?
我正在尝试在 lambda 函数中使用 busboy 来处理应该上传图像文件的发布请求。 我注意到整个文件内容并没有让它被 busboy 解析。
我尝试将调用 busboy.write 更改为仅使用“base64”,因为看起来文件以二进制形式到达,但这也不起作用。
我的客户代码
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);
我的 lambda 代码
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();
});
}
尝试使用示例照片时,我注意到“获得数据”控制台日志显示我没有收到整个文件。 我使用的文件是 229707 字节,但控制台日志说它收到了 217351 字节。
我想知道我是否使用了 busboy 错误,或者这是否是 lambda + api 网关的一些怪癖。 非常感谢任何想法或帮助故障排除。
我也在为这个问题苦苦挣扎,但最终是API Gateway的问题。
我能够通过在API Gateway的设置中添加multipart/form-data作为二进制媒体类型来解决这个问题。
为此,请转到API Gateway > "Your API" > Settings > Add Binary Media Type 并添加multipart/form-data 。
之后,再次部署您的API ,它应该可以工作。
我希望这可以帮助任何人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.