-
Notifications
You must be signed in to change notification settings - Fork 17
/
emptyVersionedBucket.py
52 lines (37 loc) · 1.2 KB
/
emptyVersionedBucket.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
import boto3
import argparse
import sys
from botocore.exceptions import BotoCoreError
from botocore.exceptions import ClientError
def run():
global args
args = parse_args()
if args.profile:
session = boto3.Session(profile_name=args.profile)
else:
session = boto3.Session()
s3 = session.resource(service_name='s3')
bucket = s3.Bucket(args.bucket)
bucket.object_versions.delete()
if args.delete_bucket:
bucket.delete()
def parse_args():
description = "Delete all objects and versions from Version Enabled S3 Bucket"
parser = argparse.ArgumentParser(description=description)
parser.add_argument('-b', '--bucket', required=True,
help='A vaild s3 bucket name')
parser.add_argument('-p', '--profile',
help='A AWS profile name located in ~/.aws/config')
parser.add_argument('-d', '--delete_bucket', action='store_true',
help='Remove the bucket after emptying')
return parser.parse_args()
code = 0
try:
run()
except (BotoCoreError, ClientError) as e:
print(e.response['Error']['Code'])
code = 1
except Exception as e:
raise
code = 2
sys.exit(code)