Carova
Read Data From AWS S3 With Nodejs
Example of getting data from S3 and a stream function to read the data into a useable format.
1getFile = async (bucket: string, key: string): Promise<any> => {
2 const { Body } = await this.s3.send(
3 new GetObjectCommand({
4 Bucket: bucket, // name of the S3 bucket ex: "configuration-bucket"
5 Key: key // File Key ex: "configs/config.json" - No leading "/"
6 })
7 );
8 const bodyContents: any = await this.streamToString(Body);
9 if (!bodyContents || typeof bodyContents !== 'string') {
10 throw new Error('Config Type Error');
11 }
12 return JSON.parse(bodyContents);
13}
14
15streamToString = (stream: any) =>
16 new Promise((resolve, reject) => {
17 const chunks: any = [];
18 stream.on("data", (chunk: any) => chunks.push(chunk));
19 stream.on("error", reject);
20 stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
21 });