I've got a Lambda function in AWS that is hooked up to a POST method which basically takes an image URL and uploads the image to an S3 bucket.
My Lambda function looks like this:
var s3 = new AWS.S3();
exports.handler = (event, context, callback) => {
let encodedImage = JSON.parse(event.body).user_avatar;
let decodedImage = Buffer.from(encodedImage, 'base64');
var filePath = "avatars/" + event.queryStringParameters.username + ".jpg"
var params = {
"Body": decodedImage,
"Bucket": "MYBUCKETNAME.com",
"Key": filePath
};
s3.upload(params, function (err, data) {
if (err) {
callback(err, null);
} else {
let response = {
"statusCode": 200,
"headers": {
"my_header": "my_value"
},
"body": JSON.stringify(data),
"isBase64Encoded": false
};
callback(null, response);
}
});
};
It seems to upload the image to the bucket specified, but when I view the image it seems to be broken.
I'm very inexperienced with using AWS for anything outside of the GUI they provide. If anyone has any idea of what might be going on I'd appreciate any help. Thanks in advance!