s3_list1000plusobjects.ts - AWS Code Sample

s3_list1000plusobjects.ts

/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3), which is pending release. The preview version of the SDK is available at https://github.com/aws/aws-sdk-js-v3. This example is in the 'AWS SDK for JavaScript v3 Developer Guide' at https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/s3-example-creating-buckets.html. Purpose: s3_list1000plusObjects.ts demonstrates how to list more than 1000 objects in an Amazon S3 bucket. Inputs (replace in code): - REGION - BUCKET_NAME Running the code: s3_getbucketwebsite s3_list1000plusObjects.ts */ // Import required AWS SDK clients and commands for Node.js const { S3Client, ListObjectsCommand } = require("@aws-sdk/client-s3"); // Set the AWS Region const REGION = "REGION"; //e.g. "us-east-1" // Create the parameters for the bucket const bucketParams = { Bucket: "BUCKET_NAME" }; // Create S3 service object const s3 = new S3Client(REGION); async function run() { // Declare truncated as a flag that we will base our while loop on let truncated = true; // Declare a variable that we will assign the key of the last element in the response to let pageMarker; // While loop that runs until response.truncated is false while (truncated) { try { const response = await s3.send(new ListObjectsCommand(bucketParams)); response.Contents.forEach((item) => { console.log(item.Key); }); // Log the Key of every item in the response to standard output truncated = response.IsTruncated; // If 'truncated' is true, assign the key of the final element in the response to our variable 'pageMarker' if (truncated) { pageMarker = response.Contents.slice(-1)[0].Key; } // At end of the list, response.truncated is false and our function exits the while loop. } catch (err) { console.log("Error", err); truncated = false; } } } run(); //for unit tests export = { run };