简体   繁体   中英

AWS Lambda and AWS API Gateway: How to send a binary file?

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)
    })
  })
}

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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