AWS Lambda和AWS API Gateway:如何发送二进制文件? [英] AWS Lambda and AWS API Gateway: How to send a binary file?

查看:112
本文介绍了AWS Lambda和AWS API Gateway:如何发送二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个lambda函数,该函数会在事件中使用输入键从s3中获取文件,并且需要将其发送给客户端.我正在使用以下功能从s3获取文件

I have a lambda function which fetches a file from s3 using the input key in event and needs to send the same to client. I am using the following function to get the file from s3

function getObject(key){
  var params = {
    Bucket: "my_bucket",
    Key: key   
  }
  return new Promise(function (resolve, reject){
    s3.getObject(params, function (err, data){
      if(err){
        reject(err);
      }
      resolve(data.Body)
    })
  })
}

如果我将此承诺(缓冲区)的响应发送到 context.succeed ,它将在前端显示为JSON数组.如何将其作为文件发送?这些文件可以是ZIP或HTTP存档(HAR)文件.s3键包含适当的扩展名.我猜想它与API网关中的集成响应"有关.但无法找出要更改的地方

If I send the response of this promise (buffer) to context.succeed, it is displayed as a JSON array on front end. How can I send it as a file ? The files can be either ZIP or HTTP Archive (HAR) files. The s3 keys contain the appropriate extension. I am guessing it has got something to do with the "Integration Response" in API Gateway. But not able to figure out where to change

推荐答案

好消息,您现在可以处理API网关的二进制输入和输出(

Good news, you can now handle binary input and output for API Gateway (announcement and documentation).

基本上,Lambda函数没有任何变化,但是现在您可以将 contentHandling API网关集成属性设置为 CONVERT_TO_BINARY .

Basically, nothing changes in your Lambda Function, but you can now set the contentHandling API Gateway Integration property to CONVERT_TO_BINARY.

不幸的是,AWS官方示例仅展示了HTTP API Gateway后端,因为AWS Lambda支持似乎尚未完成.例如,尽管新的二进制支持和 $ util.base64Decode()映射实用程序可以实现,但我还没有设法从AWS Lambda返回压缩后的内容.

Unfortunately, the official AWS examples showcase only the HTTP API Gateway backend, as the AWS Lambda support seems not complete yet. For example, I haven't managed to return gzipped content from AWS Lambda yet, although it should be possible thanks to the new binary support and the $util.base64Decode() mapping utility.

这篇关于AWS Lambda和AWS API Gateway:如何发送二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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