To get response from S3 getObject in Node.js, we call the getObject
method.
For instance, we write
const AWS = require("aws-sdk");
const s3 = new AWS.S3();
const getObject = async (bucket, objectKey) => {
try {
const params = {
Bucket: bucket,
Key: objectKey,
};
const data = await s3.getObject(params).promise();
return data.Body.toString("utf-8");
} catch (e) {
throw new Error(`Could not retrieve file from S3: ${e.message}`);
}
};
const myObject = await getObject("my-bucket", "path/to/the/object.txt");
to define the getObject
function.
In it we call getObject
with the params
object that has the Bucket
and Key
into to get the object.
We call promise
to return a promise with the object data
.
Then we convert it to a string with data.Body
.toString`.