-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy paths3_script.py
184 lines (137 loc) · 5.58 KB
/
s3_script.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#created by Ibrahim Gabr
import os
import boto3
import botocore
ACCESS_KEY = os.environ["AWS_ACCESS_KEY"]
SECRET_KEY = os.environ['AWS_SECRET_KEY']
def view_all_buckets():
"""
There are no inputs to this function.
"""
s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
response = s3.list_buckets()
buckets = [bucket['Name'] for bucket in response['Buckets']]
print("Here are all your buckets on Amazon S3:\n")
print(buckets)
print()
return buckets
def create_bucket(name_of_bucket):
"""
Enter the name of the bucket to be created.
"""
s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
created = s3.create_bucket(Bucket=name_of_bucket)
if created:
print("Bucket {} created!".format(name_of_bucket))
def view_keys_in_bucket(name_of_bucket):
"""
Enter the name of the bucket.
"""
s3 = boto3.resource('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
bucket = s3.Bucket(name_of_bucket)
print("Here are all the files stored in your bucket:\n")
for key in bucket.objects.all():
print(key.key)
print()
def upload_to_bucket(name_of_file, key, bucket_name):
"""
Name of file, key, bucket name
"""
s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
check_s3 = boto3.resource('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
bucket = bucket_name
filename = name_of_file
try:
check_s3.meta.client.head_bucket(Bucket=bucket)
except botocore.exceptions.ClientError as e:
error_code = int(e.response['Error']['Code'])
if error_code == 404:
print("{0} bucket does not exist".format(bucket_name))
s3.upload_file(filename, bucket, key)
print("{0} uploaded to {1} bucket with a key of {2}".format(name_of_file, bucket_name, key))
def upload_multiple_files(files, keys, bucket_name):
"""
list of files, list of keys, bucket name
"""
s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
check_s3 = boto3.resource('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
bucket = bucket_name
try:
check_s3.meta.client.head_bucket(Bucket=bucket)
except botocore.exceptions.ClientError as e:
error_code = int(e.response['Error']['Code'])
if error_code == 404:
print("{0} bucket does not exist".format(bucket_name))
for index, filename in enumerate(files):
s3.upload_file(filename, bucket, keys[index])
print("Uploaded {0} to {1} bucket with the key {2}".format(filename, bucket, keys[index]))
print()
def download_from_bucket(bucket_name, key, local_filename):
"""
bucket name, key, local filename
"""
s3 = boto3.resource('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
bucket = bucket_name
key_id = key
try:
download = s3.Bucket(bucket).download_file(key_id, local_filename)
print("{0} file/object downloaded from {1} bucket as {2}.\n\nFile/object can be found here: {3}".format(key, bucket_name, local_filename, os.getcwd()))
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
print("The file/object does not exist - do you have the right bucket?")
else:
raise
def download_bucket(bucket_name):
"""
Downloads an entire bucket!
Just provide the bucket_name
"""
s3 = boto3.resource('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
bucket_obj = s3.Bucket(bucket_name)
all_files = []
for key in bucket_obj.objects.all():
all_files.append(key.key)
for key in all_files:
download = s3.Bucket(bucket_name).download_file(key, key)
print("{0} file/object downloaded from {1} bucket as {2}.\n\nFile/object can be found here: {3}".format(key, bucket_name, key, os.getcwd()))
print()
print("The entire {} bucket has been downloaded!".format(bucket_name))
def delete_key(key, bucket_name):
"""
key, bucket name
"""
s3 = boto3.resource('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
bucket = s3.Bucket(bucket_name)
try:
s3.meta.client.head_bucket(Bucket=bucket_name)
except botocore.exceptions.ClientError as e:
error_code = int(e.response['Error']['Code'])
if error_code == 404:
print("{0} bucket does not exist".format(bucket_name))
for key_id in bucket.objects.all():
if key_id.key == key:
key_id.delete()
print("{0} file/object deleted! from {1} bucket".format(key, bucket_name))
def delete_bucket(bucket_name):
"""
bucket name
"""
s3 = boto3.resource('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
bucket = s3.Bucket(bucket_name)
view_keys_in_bucket(bucket_name)
answer = input("To delete a bucket, you must delete all the keys inside that bucket first. Do you want to continue? [y/n]: ")
if answer == 'y':
check = input("Are you sure? [y/n]")
if check == 'y':
for key in bucket.objects.all():
print("deleting {0}".format(key.key))
print()
key.delete()
bucket.delete()
print("{0} bucket has been deleted".format(bucket_name))
else:
print()
print("delete_bucket function exited")
else:
print()
print("delete_bucket function exited")