Attempting to upload an image from a lambda into an S3 bucket, and every time I run a test, the image in the S3 bucket is empty, only being a couple kb large.
const AWS = require('aws-sdk');
var s3 = new AWS.S3();
exports.handler = (event, context, callback) => {
console.log(event);
let encodedImage =JSON.parse(event.body).image;
let fileType = encodedImage.match(/[^:/]\w+(?=;|,)/)[0];
let decodedImage = Buffer.from(encodedImage, 'base64');
var filePath = "posts/" + JSON.parse(event.body).imageID + "." + fileType
var params = {
"Body": decodedImage,
"Bucket": "ccarlson-blog-images",
"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);
}
});
};
The request I'm sending via postman is as follows
{
"image": "data:image/png;base64,iVBORw0KGgoAAAANSU...etc",
"imageID": "4651656516216546"
}
The fact that it is properly putting something in S3 leads me to believe the issue is in my decodedImage variable, but I'm unsure of what I could be doing wrong there. Any help is appreciated!