Reputation: 31
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?
Upvotes: 0
Views: 495
Reputation: 31
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
})
});
Upvotes: 1