-
Notifications
You must be signed in to change notification settings - Fork 2
/
s3_purge_bucket.py
57 lines (47 loc) · 1.62 KB
/
s3_purge_bucket.py
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
#!/usr/bin/python3
import boto3
import os
from argparse import ArgumentParser
access_key = 'XXX'
secret_key = 'YYYYYYYYYY'
rgw_endpoint_primary = 'http://ENDPOINT:80'
parser = ArgumentParser(description='Purges objects in the specified bucket')
parser.add_argument('--bucket-name',
dest='bucket_name',
action='store',
required=True,
help='target bucket for purging')
parser.add_argument('--max-delete',
dest='max_delete',
action='store',
required=True,
help='maximum number of deleted objects')
args = parser.parse_args()
s3 = boto3.resource('s3',
endpoint_url=rgw_endpoint_primary,
aws_access_key_id = access_key,
aws_secret_access_key = secret_key,
)
def purge_bucket(Bucket, S3Client, MaxDelete):
total_deleted = 0
response = S3Client.meta.client.list_objects_v2(Bucket=Bucket)
while 'Contents' in response and response['KeyCount'] > 0:
for key in response['Contents']:
value = key['Key']
key.clear()
key['Key'] = value
count = len(response["Contents"])
total_deleted = total_deleted + count
if total_deleted > MaxDelete:
print(f'Maximum number of keys reached, exit.')
return
print(f'Deleting {count} keys at {Bucket}')
out = S3Client.meta.client.delete_objects(
Bucket=Bucket,
Delete={'Objects': response['Contents']}
)
if 'Errors' in out:
print(f'Errors at {Bucket}: {out["Errors"]}')
response = S3Client.meta.client.list_objects_v2(Bucket=Bucket)
return Bucket
purge_bucket(Bucket=args.bucket_name,S3Client=s3,MaxDelete=int(args.max_delete))