Skip to content

Instantly share code, notes, and snippets.

@tksst
Created August 18, 2019 06:45
Show Gist options
  • Save tksst/a357e9b1a95e652ab20985df52da76b7 to your computer and use it in GitHub Desktop.
Save tksst/a357e9b1a95e652ab20985df52da76b7 to your computer and use it in GitHub Desktop.
import S3 from "aws-sdk/clients/s3";
const s3 = new S3();
export async function* s3ListAllObjects(bucketName: string, limit: number = Number.MAX_VALUE) {
let p = s3.listObjectsV2({ Bucket: bucketName }).promise();
for (let i = 0; i < limit; ++i) {
const result = await p;
if (!result.IsTruncated || result.NextContinuationToken === undefined) {
yield result.Contents;
break;
}
p = s3.listObjectsV2({ Bucket: bucketName, ContinuationToken: result.NextContinuationToken }).promise();
yield result.Contents;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment