Javascript - How to listObjects from AWS S3 - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
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.
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
Install Nodejs
Setup AWS
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.
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)
Javascript Async-await
Export AWS keys
Happy Learning 🙂

How add files to S3 Bucket using Shell Script
Python – AWS SAM Lambda Example
How to connect AWS EC2 Instance using PuTTY
How to Copy Local Files to AWS EC2 instance Manually ?
How to install AWS CLI on Windows 10
How set AWS Access Keys in Windows or Mac Environment
[Fixed] – Error: No changes to deploy. Stack is up to date
Angularjs Custom Filter Example
PHP File Handling fopen fread and fclose  Example
Angular http (AJAX) Example Tutorials
What are the different ways to Sort Objects in Python ?
How to get current date in Python
AngularJs Custom Directive Example
Pandas read_excel – Read Excel files in Pandas
Java 8 – Convert java.util.Date to java.time.LocalDate

How add files to S3 Bucket using Shell Script
Python – AWS SAM Lambda Example
How to connect AWS EC2 Instance using PuTTY
How to Copy Local Files to AWS EC2 instance Manually ?
How to install AWS CLI on Windows 10
How set AWS Access Keys in Windows or Mac Environment
[Fixed] – Error: No changes to deploy. Stack is up to date
Angularjs Custom Filter Example
PHP File Handling fopen fread and fclose  Example
Angular http (AJAX) Example Tutorials
What are the different ways to Sort Objects in Python ?
How to get current date in Python
AngularJs Custom Directive Example
Pandas read_excel – Read Excel files in Pandas
Java 8 – Convert java.util.Date to java.time.LocalDate
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows