Skip to content

Instantly share code, notes, and snippets.

@mooyoul
Created July 8, 2019 07:22
Show Gist options
  • Save mooyoul/78d30680145790523835f59a1cc8f7be to your computer and use it in GitHub Desktop.
Save mooyoul/78d30680145790523835f59a1cc8f7be to your computer and use it in GitHub Desktop.
S3 Helper
import { S3 } from "aws-sdk";
import * as BbPromise from "bluebird";
import * as _ from "lodash";
export async function listAllObjects(s3: S3, bucket: string, prefix: string) {
const keys: string[] = [];
let nextContinuationToken: any;
do {
const result = await s3.listObjectsV2({
Bucket: bucket,
Prefix: prefix,
ContinuationToken: nextContinuationToken,
}).promise();
const contents = result.Contents || [];
Array.prototype.push.apply(keys, contents.map((content) => content.Key!));
nextContinuationToken = result.IsTruncated ? result.NextContinuationToken : undefined;
} while (nextContinuationToken);
return keys;
}
export async function deleteObjectsByPrefix(s3: S3, bucket: string, prefix: string, quiet = true) {
const keys = await listAllObjects(s3, bucket, prefix);
await BbPromise.map(_.chunk(keys, 1000), async (chunkedKeys) => {
await s3.deleteObjects({
Bucket: bucket,
Delete: {
Objects: chunkedKeys.map((key) => ({ Key: key })),
Quiet: quiet,
},
}).promise();
}, { concurrency: 4 });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment