Node.js 无服务器框架-将二进制文件上载到S3会损坏
我有一个端点,它接受表单数据,包括一个文件。此文件可以是文本文件、图像或pdf。我正在使用busboy(v0.2.14)解析表单数据。该代码如下所示:Node.js 无服务器框架-将二进制文件上载到S3会损坏,node.js,amazon-s3,serverless,busboy,Node.js,Amazon S3,Serverless,Busboy,我有一个端点,它接受表单数据,包括一个文件。此文件可以是文本文件、图像或pdf。我正在使用busboy(v0.2.14)解析表单数据。该代码如下所示: let buffers = []; file.on('data', data => buffers.push(data)); file.on('end', () => { result.filename = filename; resu
let buffers = [];
file.on('data', data => buffers.push(data));
file.on('end', () => {
result.filename = filename;
result.contentType = mimetype;
// Concat the chunks into a Buffer
result.file = new Buffer.concat(buffers);
});
// ...
busboy.write(event.body, event.isBase64Encoded ? 'base64' : 'binary');
busboy.end();
static myPutObject(bucketName, fileName, data, contentType, acl) {
const params = {
Bucket: bucketName,
Key: fileName,
Body: data,
ACL: acl,
ContentType: contentType,
ContentEncoding: 'base64'
};
return new AWS.S3().putObject(params).promise();
}
但是,当我使用AWS SDK(v2.97.0)将文件数据推送到S3时,当我去查看它们时,所有二进制文件都已损坏。这不会发生在文本文件上。S3上传代码如下所示:
let buffers = [];
file.on('data', data => buffers.push(data));
file.on('end', () => {
result.filename = filename;
result.contentType = mimetype;
// Concat the chunks into a Buffer
result.file = new Buffer.concat(buffers);
});
// ...
busboy.write(event.body, event.isBase64Encoded ? 'base64' : 'binary');
busboy.end();
static myPutObject(bucketName, fileName, data, contentType, acl) {
const params = {
Bucket: bucketName,
Key: fileName,
Body: data,
ACL: acl,
ContentType: contentType,
ContentEncoding: 'base64'
};
return new AWS.S3().putObject(params).promise();
}
我已经尝试了在Stack Overflow或GitHub上可以找到的所有东西,但没有成功 S3是一个“对象输入”和“对象输出”存储。它不知道您的内容是二进制还是文本或utf-16编码。它在接收时存储所有字节,并在请求时提供这些字节
下面是我们如何验证问题是在S3上还是在代码中
希望它有帮助。如果您在前面使用API网关。apiGateway将损坏传入的二进制文件,除非您专门启用了二进制媒体类型。 如果使用SLS进行部署,则只需添加:
apiGateway:
binaryMediaTypes:
- '*/*'
在“提供程序”部分中
请看这里: