Created
May 16, 2023 14:48
-
-
Save bentonmize/f25c8459b67a6399011a4270cce289dd to your computer and use it in GitHub Desktop.
Recursive S3 folder traversal in TS
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
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