简体   繁体   English

NodeJS流出AWS Lambda函数

[英]NodeJS stream out of AWS Lambda function

We are trying to migrate our zip microservice from regular application in nodejs Express to AWS API Gateway integrated with AWS Lambda. 我们正在尝试将我们的zip微服务从nodejs Express中的常规应用程序迁移到与AWS Lambda集成的AWS API Gateway。
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. 我们当前的应用程序向我们的API发送请求,获取附件列表,然后访问这些附件并以zip存档的形式将其内容发送回用户。 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. 除了我必须从Lambda函数中流式传输内容之外,我已经设法做了所有事情。
I think one of possible solutions is to use aws-serverless-express, but I'd like a more elegant solution. 我认为可能的解决方案之一是使用aws-serverless-express,但我想要一个更优雅的解决方案。

Anyone has any ideas? 有人有什么想法吗? Is it even possible to stream out of Lambda? 甚至可以流出Lambda?

Unfortunately lambda does not support streams as events or return values. 不幸的是,lambda不支持流作为事件返回值。 (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. 对于您的示例,您将不得不等待streamFiles ,然后返回完成的结果。

( 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 ) aws-serverless-express在这里没有帮助,如果你在返回之前检查他们等待管道完成的代码: https//github.com/awslabs/aws-serverless-express/blob/master/src/index。 js#L68

nb 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, eg the stream downloading the complete response from the lambda, not listening to a stream emitted from the lambda. nb这里有一个细微差别,很多语言SDK支持请求/响应流,但这意味着连接到流传输,例如从lambda下载完整响应的流,而不是从lambda发出的流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM