Last active
January 10, 2024 12:55
-
-
Save ksamirdev/806d8b614540d401c80acb7372f37c65 to your computer and use it in GitHub Desktop.
List all objects from s3 in nodejs with typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ListObjectsV2Command, type _Object } from "@aws-sdk/client-s3"; | |
let size = 0; | |
let keys: _Object[] = []; | |
const BUCKET_NAME = "": | |
const OBJECTS_PREFIX = ""; | |
async function fetchObjects(StartAfter?: string) { | |
const listCmd = new ListObjectsV2Command({ Bucket: BUCKET_NAME, Prefix: OBJECTS_PREFIX, StartAfter }); | |
try { | |
const data = await s3Client.send(listCmd); | |
const totalSize = data.Contents?.reduce((a, b) => a + (b.Size ?? 0), 0) ?? 0; | |
size = totalSize; | |
keys = keys.concat(keys, data.Contents ?? []) | |
if (data.IsTruncated && data.NextContinuationToken) { | |
await fetchObjects(data.NextContinuationToken) | |
} | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
// Here we call our function! | |
await fetchObjects(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment