Listing objects from AWS s3 bucket using Javascript (NodeJs) is a simple/regular use case for AWS development. So here we are going to see how can we achieve this simple task more simply.
How to listObjects from AWS S3:
This example has been tested on the below versions, please make sure.
- Node v12.18.4
- Yarn 1.22.10
- AWS-SDK 2.77.0
Pre-requisites:
Example listObjects from AWS S3:
Make sure to add aws-sdk
into your package.json
and run yarn install
to install dependencies.
var AWS = require('aws-sdk');
AWS.config.update({ region: 'us-west-2' });
var s3 = new AWS.S3({ apiVersion: '2006-03-01' });
var bucketParams = {
Bucket: 'my-sample-reports',
};
export async function getReports(objects = []) {
const response = await s3.listObjectsV2(bucketParams).promise();
response.Contents.forEach(obj => objects.push(obj));
if (response.NextContinuationToken) {
bucketParams.ContinuationToken = response.NextContinuationToken;
await getReports(bucketParams, objects);
}
console.log("objects ",objects)
return objects;
}
Output:
objects
(7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {Key: "1623061746-report.xlsx", LastModified: Mon Jun 07 2021 15:59:12 GMT+0530 (India Standard Time), ETag: "\"aef77773de18e148938be7a98baac57d\"", Size: 42252, StorageClass: "STANDARD"}
1: {Key: "1623062754-report.xlsx", LastModified: Mon Jun 07 2021 16:15:59 GMT+0530 (India Standard Time), ETag: "\"feed92db051e7a4dd1faacde270a21f3\"", Size: 42342, StorageClass: "STANDARD"}
2: {Key: "1623064934-report.xlsx", LastModified: Mon Jun 07 2021 16:52:20 GMT+0530 (India Standard Time), ETag: "\"704d374fd1fbd0b4751d355d3f8e5ff5\"", Size: 42044, StorageClass: "STANDARD"}
3: {Key: "1623065997-report.xlsx", LastModified: Mon Jun 07 2021 17:10:02 GMT+0530 (India Standard Time), ETag: "\"8ab079ba5a816b198106cab41c6f33ad\"", Size: 42045, StorageClass: "STANDARD"}
4: {Key: "1623145510-report.xlsx", LastModified: Tue Jun 08 2021 15:15:16 GMT+0530 (India Standard Time), ETag: "\"039999bfe06551a2894a565bfb4bb571\"", Size: 44686, StorageClass: "STANDARD"}
5: {Key: "1623404329-report.xlsx", LastModified: Fri Jun 11 2021 15:08:55 GMT+0530 (India Standard Time), ETag: "\"85230b71f4b6aaa32d45845a9772086e\"", Size: 45357, StorageClass: "STANDARD"}
6: {Key: "1623419247-report.xlsx", LastModified: Fri Jun 11 2021 19:17:32 GMT+0530 (India Standard Time), ETag: "\"bee56b7101a8fe98b89fcfc59d850ebf\"", Size: 45357, StorageClass: "STANDARD"}
length: 7
__proto__: Array(0)
On the above output, we can see the list of objects from the s3 bucket. However, the output contains the raw response from S3.
Customise S3 listObjects response:
This example shows how to customise the S3 raw response into our requirement.
var AWS = require('aws-sdk');
AWS.config.update({ region: 'us-west-2' });
var s3 = new AWS.S3({ apiVersion: '2006-03-01' });
var bucketParams = {
Bucket: 'my-sample-reports',
};
export async function getSample(objects = []) {
const response = await s3.listObjectsV2(bucketParams).promise();
response.Contents.forEach(obj => {
var date = new Date(obj.LastModified);
var actual = date.getDate() + '/' + Number(date.getMonth() + 1) + '/' + date.getFullYear();
const size = convertBytes(obj.Size)
objects.push({ file: obj.Key, date: actual, filesize: size });
});
if (response.NextContinuationToken) {
bucketParams.ContinuationToken = response.NextContinuationToken;
await getReports(bucketParams, objects);
}
console.log("objects ",objects)
return objects;
}
const units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
export function convertBytes(x) {
let l = 0, n = parseInt(x, 10) || 0;
while(n >= 1024 && ++l){
n = n/1024;
}
return(n.toFixed(n < 10 && l > 0 ? 1 : 0) + ' ' + units[l]);
}
Output:
objects
(7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {file: "1623061746-report.xlsx", date: "7/6/2021", filesize: "41 KB"}
1: {file: "1623062754-report.xlsx", date: "7/6/2021", filesize: "41 KB"}
2: {file: "1623064934-report.xlsx", date: "7/6/2021", filesize: "41 KB"}
3: {file: "1623065997-report.xlsx", date: "7/6/2021", filesize: "41 KB"}
4: {file: "1623145510-report.xlsx", date: "8/6/2021", filesize: "44 KB"}
5: {file: "1623404329-report.xlsx", date: "11/6/2021", filesize: "44 KB"}
6: {file: "1623419247-report.xlsx", date: "11/6/2021", filesize: "44 KB"}
length: 7
__proto__: Array(0)
References:
Happy Learning 🙂