Skip to content

Instantly share code, notes, and snippets.

@bentonmize
Created May 16, 2023 14:48
Show Gist options
  • Save bentonmize/f25c8459b67a6399011a4270cce289dd to your computer and use it in GitHub Desktop.
Save bentonmize/f25c8459b67a6399011a4270cce289dd to your computer and use it in GitHub Desktop.
Recursive S3 folder traversal in TS
const getKeysFromPrefix = async (s3: S3Client, prefix: string, bucket: string) => {
const keys = await s3.send(new ListObjectsCommand({
Bucket: bucket,
Delimiter: '/',
Prefix: prefix
})).then(response => {
if(response.CommonPrefixes) {
// Recursive call to go into folders
const promises = []
response.CommonPrefixes.forEach(p => promises.push(getKeysFromPrefix(s3, p.Prefix, bucket)))
return Promise.all(promises);
} else {
// Collect the keys from contents
const keys = []
response.Contents.forEach(c => keys.push({
key: c.Key,
lastModified: c.LastModified,
etag: c.ETag,
size: c.Size,
storageClass: c.StorageClass,
owner: c.Owner
}))
return keys;
}
})
// Flatten the response arrays of keys
return keys.flat()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment