This repository has been archived by the owner on Jan 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
delete_bucket.py
executable file
·65 lines (52 loc) · 2.09 KB
/
delete_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
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
import uuid
import json
import logging
import argparse
import string
from copy_bucket import get_config, check_dead_letter, put_sqs
if __name__ == '__main__':
"""
deletes all files from source to destination bucket
"""
# Logging setup
logging.basicConfig(filename='scan.log',
filemode='a',
level=logging.INFO,
format='%(asctime)s %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p')
logger = logging.getLogger(__name__)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
console.setFormatter(logging.Formatter('%(asctime)s %(message)s', "%H:%M:%S"))
logger.addHandler(console)
# Command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("-b", "--bucket",
help="Bucket Name",
default='test-dest-keithrozario')
args = parser.parse_args()
bucket_name = args.bucket
# Get Configuration
config = get_config()
region = config['provider']['region']
service_name = config['service']
delete_queue_name = config['custom']['sqs_delete_objects'].replace('${self:service}', service_name)
logger.info(f"Deleting all objects in {bucket_name}")
logger.info(f'Using Serverless deployment {service_name}')
logger.info(f'Using SQS Queue: {delete_queue_name}')
message = {"bucket": args.bucket}
prefixes = string.printable
message_batch = []
for prefix in prefixes:
message['prefix'] = prefix
message_batch.append({'MessageBody': json.dumps(message), "Id": uuid.uuid4().__str__()})
# Putting messages onto the Que
put_sqs(message_batch, delete_queue_name)
# Check Queue
logger.info(f"No messages left on SQS Que, all objects beginning with the following chars were deleted: {prefixes}")
check_dead_letter(f"{service_name}-dl")
if check_dead_letter(f"{service_name}-dl") > 0:
logger.info(f"Errors found, please refer to {service_name}-dl for more info")
else:
logger.info("All Done")