I am saving an image from url to s3 bucket. The image is saved but when opening the object url of that image, it is not displaying the image properly except a small square. The image size is not zero either. I know there is something wrong but not sure where it is and how to fix it. Any suggestion would be great.
Here is my code:
const AWS = require('aws-sdk');
const axios = require('axios');
AWS.config.update({
accessKeyId: 'xxxxxx',
secretAccessKey: 'xxx',
});
const S3 = new AWS.S3();
const url = '//../path-to-image';
async function uploadToS3(uri, cb) {
const image = await axios.get(uri);
const finalImage = await new Buffer.from(image.data);
S3.putObject(
{
Bucket: 'my-images',
Key: Date.now() + '.jpg',
ACL: 'public-read',
ContentType: 'image/jpeg',
Body: finalImage,
},
function (err) {
if (err) {
console.error('Image upload to s3 failed: ', err);
}
console.log('Upload to s3 successfull');
}
);
}
const handleError = (err, d) => {
if (err) return new Error(err.stack);
else console.log('Operation successfull', d);
};
uploadToS3(url, handleError);