In my application I upload images to a local /tmp folder and make some transformations. Pictures are saved properly there. After that I want to upload this images to an S3 bucket, but so far I only manage to produce blank pictures.
This is my code:
//Pick the local image and make it binary
var fs = require('fs');
var bufferedData = '';
fs.readFile(imagePath, function (err, data) {
if (err) { throw err; }
bufferedData = new Buffer(data, 'binary');
}
//Send data to s3
const uploadToS3 = async (idKey: string, modifiers: string, bufferedData) => {
try {
return await S3.upload({
Bucket: 'mirage-thumbnails',
Key: `${process.env.APP_ENV}/${idKey}/${modifiers}`,
Body: bufferedData,
ContentType: 'image/png',
ACL: 'public-read',
CacheControl: 'max-age=0',
}).promise();
} catch (e) {
console.error(e.message, e);
}
};