Hey. I'm trying to create a command where the user uploads a picture and then the picture is stored in s3 bucket.
But I have tried it multiple times, the image is always corrupted in the bucket.
Here's the code snippet for the upload part :
I first download the image locally and then upload it to the bucket
const s3 = new AWS.S3();
const imageBuffer = Buffer.from(
img.replace(/^data:image\/\w+;base64,/, ''),
'base64'
);
// Save the image locally
const localImagePath = 'temp-image.jpg';
fs.writeFileSync(localImagePath, imageBuffer);
const params = {
Bucket: 'beast-db',
Key: `${tag}.jpg`,
Body: fs.createReadStream(localImagePath),
ContentType: 'image/jpeg',
};
// Upload the image to S3
const uploadResult = await s3.upload(params).promise();
tag is the image name entered by the user. img is the url of the image from the attachment option payload.