Amazon Lambda - return docx 文件 - Node.js
Amazon Lambda - return docx file - Node.js
我正在尝试将我的 Node.js 应用程序从 Google Cloud Functions 迁移到 Amazon Lambda。应用程序从 Amazon S3 加载 docx 文件,处理它并 return 该 docx 文件。但是我卡在了文件 return 的进程中。在 Google Cloud Platform 中我可以这样做:
module.exports = function(customENV){ return function(req, res) {
new AWS.S3().getObject({ Bucket: aws_bucket, Key: aws_file }, function(err, data) {
if(!err) {
res.set('Access-Control-Allow-Origin', "*");
res.set('Access-Control-Allow-Methods', 'GET, POST');
res.set('Content-Disposition', `inline; filename="rename.docx"`);
res.type('docx');
res.status(200);
res.end(data.Body, 'binary');
}
});
}};
在 Amazon Lambda 中,我复制了这样的解决方案:
exports.handler = function(event, context, callback) {
new AWS.S3().getObject({ Bucket: aws_bucket, Key: aws_file }, function(err, data) {
if(!err) {
var response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': "*",
'Access-Control-Allow-Methods': 'GET, POST',
'Content-type' : 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'Content-Disposition': 'inline; filename="rename.docx"'
},
isBase64Encoded: true,
body: data.Body,
};
callback(null, response);
}
});
};
作为 API 网关,我使用 LAMBDA_PROXY 和任何方法。 Models/responses/mappings 均为“默认”。但是,我得到的唯一响应是“内部服务器错误”。在 CloudWatch 日志中,我还看到“由于配置错误导致执行失败:无法对正文进行 base64 解码”。
我尝试复制各种解决方案 and/or 不同配置的 API 网关,但是没有成功。可能我只是不了解亚马逊的 API 网关,因此我不知道如何正确配置它。
也许像日志说的那样也可能是数据转换,但我试过像toString("base64")这样的转换,也没有成功。
有什么想法可以让这个最小的解决方案发挥作用吗?谢谢!
如果您将回复更改为:
,您的解决方案应该会起作用
var response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': "*",
'Access-Control-Allow-Methods': 'GET, POST',
'Content-type' : 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'Content-Disposition': 'inline; filename="rename.docx"'
},
isBase64Encoded: true,
body: Buffer.from(data.Body).toString('base64'),
};
这应该可以解决您的 Unable to base64 decode the body
错误(如果没有,请您记录 response
对象以确认您的 lambda 已成功返回并且正文 是一个base64字符串)。
但是,您还需要将二进制媒体类型添加到您的网关部署中,否则当您发出请求时,您的响应将是 base64 字符串而不是二进制。涉及几次点击 (full documentation):
- Under the selected API in the primary navigation panel, choose Settings.
- In the Settings pane, choose Add Binary Media Type in the Binary Media Types section.
- Type a required media type, for example, image/png, in the input text field. If needed, repeat this step to add more media types.
我正在尝试将我的 Node.js 应用程序从 Google Cloud Functions 迁移到 Amazon Lambda。应用程序从 Amazon S3 加载 docx 文件,处理它并 return 该 docx 文件。但是我卡在了文件 return 的进程中。在 Google Cloud Platform 中我可以这样做:
module.exports = function(customENV){ return function(req, res) {
new AWS.S3().getObject({ Bucket: aws_bucket, Key: aws_file }, function(err, data) {
if(!err) {
res.set('Access-Control-Allow-Origin', "*");
res.set('Access-Control-Allow-Methods', 'GET, POST');
res.set('Content-Disposition', `inline; filename="rename.docx"`);
res.type('docx');
res.status(200);
res.end(data.Body, 'binary');
}
});
}};
在 Amazon Lambda 中,我复制了这样的解决方案:
exports.handler = function(event, context, callback) {
new AWS.S3().getObject({ Bucket: aws_bucket, Key: aws_file }, function(err, data) {
if(!err) {
var response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': "*",
'Access-Control-Allow-Methods': 'GET, POST',
'Content-type' : 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'Content-Disposition': 'inline; filename="rename.docx"'
},
isBase64Encoded: true,
body: data.Body,
};
callback(null, response);
}
});
};
作为 API 网关,我使用 LAMBDA_PROXY 和任何方法。 Models/responses/mappings 均为“默认”。但是,我得到的唯一响应是“内部服务器错误”。在 CloudWatch 日志中,我还看到“由于配置错误导致执行失败:无法对正文进行 base64 解码”。
我尝试复制各种解决方案 and/or 不同配置的 API 网关,但是没有成功。可能我只是不了解亚马逊的 API 网关,因此我不知道如何正确配置它。
也许像日志说的那样也可能是数据转换,但我试过像toString("base64")这样的转换,也没有成功。
有什么想法可以让这个最小的解决方案发挥作用吗?谢谢!
如果您将回复更改为:
,您的解决方案应该会起作用 var response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': "*",
'Access-Control-Allow-Methods': 'GET, POST',
'Content-type' : 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'Content-Disposition': 'inline; filename="rename.docx"'
},
isBase64Encoded: true,
body: Buffer.from(data.Body).toString('base64'),
};
这应该可以解决您的 Unable to base64 decode the body
错误(如果没有,请您记录 response
对象以确认您的 lambda 已成功返回并且正文 是一个base64字符串)。
但是,您还需要将二进制媒体类型添加到您的网关部署中,否则当您发出请求时,您的响应将是 base64 字符串而不是二进制。涉及几次点击 (full documentation):
- Under the selected API in the primary navigation panel, choose Settings.
- In the Settings pane, choose Add Binary Media Type in the Binary Media Types section.
- Type a required media type, for example, image/png, in the input text field. If needed, repeat this step to add more media types.