-
Notifications
You must be signed in to change notification settings - Fork 2
/
bucket_menu.py
132 lines (108 loc) · 3.56 KB
/
bucket_menu.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import boto3, logging
from botocore.exceptions import ClientError
menu_options = {
1: 'Bucket List',
2: 'Create a Bucket',
3: 'Delete a Bucket',
4: 'Delete a file from a Bucket',
5: 'Add a file to Bucket',
6: 'Exit',
}
s3 = boto3.resource('s3')
s3_client = boto3.client('s3')
def print_menu():
for key in menu_options.keys():
print(key, '-', menu_options[key])
print("")
def print_bucket_list():
print('Handle option \'print_bucket_list\'')
try:
bucket_number = 0
print('Existing buckets: ')
for bucket in s3.buckets.all():
bucket_number += 1
print(f"{bucket_number}: {bucket.name}")
print("")
except ClientError as e:
logging.error(e)
return False
return True
def create_bucket():
print('Handle option \'create_bucket\'')
bucket_name = input('Please enter bucket name: ')
bucket_region = input('Please enter bucket region: ')
try:
if bucket_region is None:
s3_client.create_bucket(Bucket=bucket_name)
else:
s3_client = boto3.client('s3', region_name=bucket_region)
location = {'LocationConstraint': bucket_region}
s3_client.create_bucket(Bucket=bucket_name,
CreateBucketConfiguration=location)
except ClientError as e:
logging.error(e)
return False
return True
def delete_bucket():
print('Handle option \'delete_bucket\'')
print_bucket_list()
try:
bucket_for_delete = input('Please enter bucket name for delete: ')
response = s3_client.delete_bucket(Bucket=bucket_for_delete)
except ClientError as e:
logging.error(e)
return False
return True
def delete_file():
try:
print('Handle option \'delete_file\'')
bucket_name = input('Please enter bucket name: ')
my_bucket = s3.Bucket(bucket_name)
file_number= 0
for file in my_bucket.objects.all():
file_number += 1
print(f"{file_number}: {file.key}")
file_name = input('Please enter file name for delete: ')
s3.Object(bucket_name, file_name).delete()
except ClientError as e:
logging.error(e)
return False
return True
def add_file():
print('Handle option \'add_file\'')
print_bucket_list()
bucket_name = input('Please enter bucket name: ')
file_name = input('Please enter file name with path: ')
object_name = input('Please enter AWS file display name: ')
try:
response = s3_client.upload_file(file_name, bucket_name, object_name)
except ClientError as e:
logging.error(e)
return False
return True
def Exit():
print('Handle option \'Exit\'')
if __name__=='__main__':
while(True):
print_menu()
option = ''
try:
option = int(input('Enter your choice: '))
except:
print('Worng input, Please enter a number')
if option == 1:
print_bucket_list()
elif option == 2:
create_bucket()
elif option == 3:
delete_bucket()
elif option == 4:
delete_file()
elif option == 5:
add_file()
elif option == 6:
print('Thanks message before exiting')
Exit()
break
else:
print('Invalid option. Please enter a number between 1 and 6.')