Skip to content

Instantly share code, notes, and snippets.

@secretshardul
Created February 4, 2020 11:14
Show Gist options
  • Save secretshardul/d7390ed120d54b75f152f07300aa72d2 to your computer and use it in GitHub Desktop.
Save secretshardul/d7390ed120d54b75f152f07300aa72d2 to your computer and use it in GitHub Desktop.
Lambda function reads a file from an S3 bucket. It converts it into a base64 encoded string and returns the value. API gateway converts this into binary and adds the appropriate header.
var AWS = require("aws-sdk");
var s3 = new AWS.S3();
exports.handler = (event, context, callback) => {
var params = {Bucket: 'myBucket', Key: 'myImg.png'};
s3.getObject(params, function(err, data) {
if (err) {
console.log(err, err.stack);
}
else{
var attachment = data.Body.toString('base64');
callback(null, attachment);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment