NodeJS流出AWS Lambda函数 [英] NodeJS stream out of AWS Lambda function

查看:171
本文介绍了NodeJS流出AWS Lambda函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试将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不支持流. (很难找到它在文档中明确提到的内容,除非注意在工作中的 解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆