4

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

4
  • Read the official AWS response in this thread: forums.aws.amazon.com/thread.jspa?threadID=195218 Commented Apr 6, 2016 at 18:14
  • 1
    API Gateway team - Mark B is right, that is the official status. We are working to support binary passthrough in the future, but no ETA. Commented Apr 6, 2016 at 18:26
  • is there any workaround for this. Our complete application is serverless. We would want to avoid creating a separate EC2 instance for just download purpose Commented Apr 6, 2016 at 19:54
  • You could generate a pre-signed S3 URL in your Lambda which your client uses to download. Commented Apr 16, 2016 at 18:47

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Could You please provide some examples? I am absolute beginner with AWS and I'm currently trying to upload zip file to Lambda and I would like also to do it locally using serverless and serverless-offline frameworks (for development)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.