0

I created a nodejs http server app to download a remote image, it functions fine as a standalone node js app, but the download file corrupts when using nodejs aws-lambda.

I have tried going into the aws gateway api settings and setitng the binary content type to allow all */*

request.get('https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png', {encoding:'binary'},function(error, response){ callback(null, { "statusCode" : 200, "headers": { 'Content-Type': 'application/octet-stream', 'Cache-Control': 'no-cache', 'Content-Disposition': 'attachment; filename="google.png"', 'Accept': 'application/octet-stream' }, "body" : response.body}); }); 

this code shows what is inside my lambda function. it seems that the received file has some sort of utf or encoding which corrupts it.

Question:

Has anyone experienced the same issue or can provide guidance on what could be causing the corrupt file?

3
  • Are you using API gateway to connect to lambda and getting the image as part of the response stream? Commented Jun 25, 2019 at 6:19
  • Using the get api (just opening in browser) here is the full handler pastebin.com/S4NDGhXW Commented Jun 25, 2019 at 7:22
  • To verify if the downloaded file is working properly within lambda, you can maybe upload to S3 and check the file integrity and as far as API Gateway is concerned, try this Commented Jun 25, 2019 at 7:29

1 Answer 1

1

Solved

It turned out the problem was that I needed to format the image as base64 and then set isBase64Encoded to true. I have provided het working code below.

Step 1. Go into the GUI for lambda api gateway settings and set the binary content type to allow all /

Step 2. In the response make sure you format image binary you retrieve as base64 and also pass the isBase64Encoded flag as true. This will allow you to output the file for download. If you do not format it as base64 and flip the flag you will get an encoded version of the file that looks and acts corrupt.

 responseType: 'arraybuffer'}).then(response => { callback(null, { statusCode: 200, headers: { 'Content-Type': 'image/png', 'Cache-Control': 'no-cache', 'Content-Disposition': 'attachment; filename="google.png"' }, body: Buffer.from(response.data, 'binary').toString('base64'), isBase64Encoded: true }) }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.