Created
August 14, 2020 09:48
-
-
Save maxleiko/9b2e8ac7c6a103bc8774bee7540be969 to your computer and use it in GitHub Desktop.
S3 listObjects async generator
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 S3, { ObjectList } from 'aws-sdk/clients/s3'; | |
import { PromiseResult } from 'aws-sdk/lib/request'; | |
import { AWSError } from 'aws-sdk/lib/error'; | |
export async function* listObjects( | |
s3: S3, | |
Bucket: string, | |
Prefix?: string | undefined, | |
StartAfter?: string | undefined, | |
): AsyncGenerator<ObjectList> { | |
let res: PromiseResult<S3.ListObjectsV2Output, AWSError>; | |
while ( | |
(res = await s3.listObjectsV2({ Bucket, Prefix, StartAfter }).promise()) && | |
res.Contents && | |
res.Contents.length > 0 | |
) { | |
StartAfter = res.Contents[res.Contents.length - 1].Key!; // eslint-disable-line @typescript-eslint/no-non-null-assertion | |
yield res.Contents; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment