无法打开到S3的无服务器上载文件



我正在尝试使用无服务器(Node.js(上传文件

const contentType = event.headers['Content-Type'] || event.headers['content-type'];
const bb = new busboy({ headers: { 'content-type': contentType }});
// When file load
bb.on('file', function (fieldname, file, filename, encoding, mimetype) {
console.log(fieldname, filename, encoding, mimetype);
console.log(file);
const key = 'upload/' + filename;
var s3obj = new AWS.S3({
params: {
Bucket: 'fileupload',
Key: key,
ACL: 'public-read',
ContentEncoding: encoding,
ContentType: mimetype,
}
});
s3obj.upload({ Body: file })
.on('httpUploadProgress', function(evt) { console.log(evt); })
.send(function(err, data) { console.log(err, data) });
})
bb.end(event.body);
callback(null, response({ status: 'success' }));

运行此代码后,S3成功创建了文件,但如果我上传了图像或其他非文本文件(不是.txt、.csv(,文件大小将不同,文件无法打开。

我可以知道我的代码哪一部分出错了吗?

发现需要

Add multipart/form-data binary media type

在API网关下获取文件的正确编码。

我已经关注了这个插件

https://github.com/myshenin/aws-lambda-multipart-parser

来解决这个问题。

我在上传图像时遇到了同样的问题。解决方案是在存储桶设置中启用二进制媒体类型。

我将介质类型设置为*/*

最新更新