Articles → AWS → Get The List Of Objects Inside The S3 Bucket Using The Lambda Function In AWS
Get The List Of Objects Inside The S3 Bucket Using The Lambda Function In AWS
Creation Of S3 Bucket
Click to Enlarge
Click to Enlarge
Creation Of A Lambda Function
Click to Enlarge
Install AWS-SDK Package
Write Code To Read Data From The S3 Bucket
import AWS from aws-sdk;
const s3 = new AWS.S3();
export const handler = async(event) => {
// TODO implement
const bucketName = gyansangrah;
// Set the parameters for the S3 listObjectsV2 operation
const params = {
Bucket: bucketName
};
// List all the objects in the S3 bucket
try {
const data = await s3.listObjectsV2(params).promise();
const objects = data.Contents.map(obj => obj.Key);
console.log(`Objects in ${bucketName}: ${objects}`);
return objects;
} catch (err) {
console.log(`Error listing objects: ${err}`);
throw err;
}
const response = {
statusCode: 200,
body: JSON.stringify(Hello from Lambda!),
};
return response;
};
Add Permission To The Lambda Function Role
Click to Enlarge
Upload Code To Lambda Function
Click to Enlarge
Click to Enlarge
Execute The Lambda Function
Click to Enlarge