2 Answers
- Newest
- Most votes
- Most comments
0
Hello.
I'm not very good at JavaScript, but I created some sample code.
I was able to encode the image in S3 to Base64 using the code below.
The code below can get the image "client.drawio.png" from the S3 bucket using "GetObjectCommand" and convert it to base64.
https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/s3/command/GetObjectCommand/
The image is converted to base64 in the "const base64Image = buffer.toString('base64');" part.
import {
S3Client,
GetObjectCommand
} from '@aws-sdk/client-s3'
const s3 = new S3Client({
region: 'ap-northeast-1'
})
export const handler = async (event) => {
// TODO implement
const response = await s3.send(
new GetObjectCommand({
Bucket: 'kobayashi-lambda',
Key: 'client.drawio.png'
})
);
const chunks = [];
for await (let chunk of response.Body) {
chunks.push(chunk);
}
const buffer = Buffer.concat(chunks);
const base64Image = buffer.toString('base64');
console.log(base64Image);
};
0
Hi,
This article proposes you 3 different methods to do this conversion: https://pqina.nl/blog/convert-an-image-to-a-base64-string-with-javascript/
Best,
Didier
Relevant content
- asked 3 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 7 months ago

thanks for your answer, finally this : const base64ImageString = await imageObjectBody.transformToString("base64"); workout for me