NodeJS流出AWS Lambda函数 [英] NodeJS stream out of AWS Lambda function
问题描述
我们正在尝试将zip微服务从nodejs Express中的常规应用程序迁移到与AWS Lambda集成的AWS API Gateway.
我们当前的应用程序将请求发送到我们的API,获取附件列表,然后访问这些附件并将其内容以zip存档的形式发送回用户.看起来像这样:
module.exports = function requestHandler(req, res) {
//...
//irrelevant code
//...
return getFileList(params, token).then(function(fileList) {
const filename = `attachments_${params.id}`;
res.set('Content-Disposition', `attachment; filename=${filename}.zip`);
streamFiles(fileList, filename).pipe(res); <-- here magic happens
}, function(error) {
errors[error](req, res);
});
};
除了要从Lambda函数中流式传输内容的那一部分之外,我已经设法完成了所有操作.
我认为可能的解决方案之一是使用aws-serverless-express,但我想要一个更优雅的解决方案.
有人有什么想法吗?甚至有可能从Lambda中流出来吗?
不幸的是,由于事件或返回值,lambda不支持流. (很难找到它在文档中明确提到的内容,除非注意在工作中的 https://github.com/awslabs/aws-serverless-express/blob/master/src/index.js#L68 )>
n.b.这里有一个细微差别,很多语言SDK支持请求/响应流式传输,但是这意味着要连接到流式传输,例如流从lambda下载完整的响应,而不是听从lambda发出的流.
We are trying to migrate our zip microservice from regular application in nodejs Express to AWS API Gateway integrated with AWS Lambda.
Our current application sends request to our API, gets list of attachments and then visits those attachments and pipes their content back to user in form of zip archive. It looks something like this:
module.exports = function requestHandler(req, res) {
//...
//irrelevant code
//...
return getFileList(params, token).then(function(fileList) {
const filename = `attachments_${params.id}`;
res.set('Content-Disposition', `attachment; filename=${filename}.zip`);
streamFiles(fileList, filename).pipe(res); <-- here magic happens
}, function(error) {
errors[error](req, res);
});
};
I have managed to do everything except the part where I have to stream content out of Lambda function.
I think one of possible solutions is to use aws-serverless-express, but I'd like a more elegant solution.
Anyone has any ideas? Is it even possible to stream out of Lambda?
Unfortunately lambda does not support streams as events or return values. (It's hard to find it mentioned explicitly in the documentation, except by noting how invocation and contexts/callbacks are described in the working documentation).
In the case of your example, you will have to await streamFiles
and then return the completed result.
(aws-serverless-express
would not help here, if you check the code they wait for your pipe to finish before returning: https://github.com/awslabs/aws-serverless-express/blob/master/src/index.js#L68)
n.b. There's a nuance here that a lot of the language SDK's support streaming for requests/responses, however this means connecting to the stream transport, e.g. the stream downloading the complete response from the lambda, not listening to a stream emitted from the lambda.
这篇关于NodeJS流出AWS Lambda函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!