forked from AsravanthiR/aws_s3_nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3_getobject.js
More file actions
34 lines (32 loc) · 1.19 KB
/
s3_getobject.js
File metadata and controls
34 lines (32 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { GetObjectCommand } from "@aws-sdk/client-s3";
import { s3Client } from "./s3Client.js"; // Helper function that creates an Amazon S3 service client module.
import ff from "fs";
export const bucketParams = {
Bucket: "bucket-sravanthi",
Key: "index.html",
};
export const run = async () => {
try {
// Create a helper function to convert a ReadableStream to a string.
const streamToString = (stream) =>
new Promise((resolve, reject) => {
const chunks = [];
stream.on("data", (chunk) => chunks.push(chunk));
stream.on("error", reject);
stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
});
// Get the object} from the Amazon S3 bucket. It is returned as a ReadableStream.
const data = await s3Client.send(new GetObjectCommand(bucketParams));
//return data; // For unit tests.
// Convert the ReadableStream to a string.
const bodyContents = await streamToString(data.Body);
console.log(bodyContents);
fs.writeFile("index.html", bodyContents, function (err) {
console.log(err ? "Error :" + err : "ok");
});
return bodyContents;
} catch (err) {
console.log("Error", err);
}
};
run();