Skip to content

Instantly share code, notes, and snippets.

@samocodes
Last active January 10, 2024 12:55
Show Gist options
  • Save samocodes/806d8b614540d401c80acb7372f37c65 to your computer and use it in GitHub Desktop.
Save samocodes/806d8b614540d401c80acb7372f37c65 to your computer and use it in GitHub Desktop.
List all objects from s3 in nodejs with typescript
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