-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3-empty-bucket.mjs
60 lines (48 loc) · 1.92 KB
/
s3-empty-bucket.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { DeleteBucketCommand, DeleteObjectsCommand, ListObjectsCommand } from '@aws-sdk/client-s3'
/**
* Empties AWS S3 bucket.
* @param {object} options - The destructured options object.
* @param {string} options.bucketName - The name of the bucket to empty.
* @param {boolean} options.doDelete - If true, then deletes the bucket after emptying it.
* @param {object} options.s3Client - Authenticated `S3Client`.
* @param {boolean} options.verbose - When true, will report actions to `process.stdout`.
*/
const emptyBucket = async ({ bucketName, doDelete, s3Client, verbose }) => {
let marker, isTruncated
do {
maybeSay('Cataloging files...\n', verbose)
const objects = []
const listObjectsCommand = new ListObjectsCommand({ Bucket : bucketName, Marker : marker })
const listObjectsResult = await s3Client.send(listObjectsCommand)
const contents = listObjectsResult.Contents || [];
({ IsTruncated: isTruncated } = listObjectsResult)
objects.push(...contents)
maybeSay(`Deleting ${objects.length} files...\n`, verbose)
const input = {
Bucket : bucketName,
Delete : {
Objects : objects.map(({ Key }) => ({ Key }))
},
Quiet : true
}
if (objects.length === 0) { // this can only happen on the first call
maybeSay('Bucket already empty.\n', verbose)
} else {
const deleteObjectsCommand = new DeleteObjectsCommand(input)
await s3Client.send(deleteObjectsCommand)
}
marker = isTruncated === true ? objects[objects.length - 1].Key : undefined
} while (isTruncated === true)
if (doDelete === true) {
maybeSay(`Deleting bucket '${bucketName}'...\n`)
const deleteBucketCommand = new DeleteBucketCommand({ Bucket : bucketName })
await s3Client.send(deleteBucketCommand)
}
maybeSay('Done!\n', verbose)
}
const maybeSay = (message, verbose) => {
if (verbose === true) {
process.stdout.write(message)
}
}
export { emptyBucket }