Skip to content

Instantly share code, notes, and snippets.

@maxleiko
Created August 14, 2020 09:48
Show Gist options
  • Save maxleiko/9b2e8ac7c6a103bc8774bee7540be969 to your computer and use it in GitHub Desktop.
Save maxleiko/9b2e8ac7c6a103bc8774bee7540be969 to your computer and use it in GitHub Desktop.
S3 listObjects async generator
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