This repository has been archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 340
/
Copy paths3dataEndpoint.py
211 lines (186 loc) · 9.04 KB
/
s3dataEndpoint.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import os
import boto3
import logging
from botocore.exceptions import ClientError
from boto3.s3.transfer import TransferConfig
from botocore.client import Config
import progressPercent
class S3DataEndpoint:
def __init__(self, end_url, accessKey, secretKey):
self.end_url = end_url
self.accessKey = accessKey
self.secretKey = secretKey
self.s3_resource = boto3.resource('s3', endpoint_url=self.end_url,
aws_access_key_id=self.accessKey,
aws_secret_access_key=self.secretKey,
config=Config(signature_version='s3v4'),
region_name='US')
# command to access data from default session
self.s3_client = boto3.client('s3', endpoint_url=self.end_url,
aws_access_key_id=self.accessKey,
aws_secret_access_key=self.secretKey,
config=Config(signature_version='s3v4'),
region_name='US')
def multipart_upload_with_s3(self, bucket_name, file_path=None, object_name=None):
# Multipart upload (see notes)
config = TransferConfig(multipart_threshold=1024 * 25, max_concurrency=10,
multipart_chunksize=1024 * 25, use_threads=True)
key_path = 'multipart_files/{}'.format(object_name)
print(bucket_name, file_path, object_name, key_path)
self.s3_client.upload_file(file_path, bucket_name, key_path,
ExtraArgs={'ACL': 'public-read',
'ContentType': 'text/pdf'},
Config=config, Callback=progressPercent.ProgressPercentage(file_path))
def multipart_download_with_s3(self, bucket_name, file_path=None, object_name=None):
config = TransferConfig(multipart_threshold=1024 * 25, max_concurrency=10,
multipart_chunksize=1024 * 25, use_threads=True)
temp_file = os.path.dirname(__file__)
self.s3_resource.Object(bucket_name,
object_name
).download_file(file_path, Config=config,
Callback=progressPercent.ProgressPercentage(temp_file))
# Functions for buckets operation
def create_bucket_op(self, bucket_name, region):
if region is None:
self.s3_client.create_bucket(Bucket=bucket_name)
else:
location = {'LocationConstraint': region}
self.s3_client.create_bucket(Bucket=bucket_name,
CreateBucketConfiguration=location)
def list_bucket_op(self, bucket_name, region, operation):
buckets = self.s3_client.list_buckets()
if buckets['Buckets']:
for bucket in buckets['Buckets']:
print(bucket)
return True
else:
logging.error('unknown bucket operation')
return False
def bucket_operation(self, bucket_name, region=None, operation='list'):
try:
if operation == 'delete':
self.s3_client.delete_bucket(Bucket=bucket_name)
elif operation == 'create':
self.create_bucket_op(bucket_name, region)
elif operation == 'list':
return self.list_bucket_op(bucket_name, region, operation)
else:
logging.error('unknown bucket operation')
return False
except ClientError as e:
logging.error(e)
return False
return True
# Functions for objects operation
def list_object_op(self, bucket_name):
self.s3_objects = self.s3_client.list_objects_v2(Bucket=bucket_name)
if self.s3_objects.get('Contents'):
for obj in self.s3_objects[ 'Contents' ]:
print(obj)
def delete_object_op(self, bucket_name, object_name, operation):
if not object_name:
logging.error('object_name missing for {}'.format(operation))
return False
self.s3_client.delete_object(Bucket=bucket_name, Key=object_name)
return True
def upload_download_object_op(self, bucket_name, object_name, file_path, operation):
if not file_path or not object_name:
logging.error('file_path and/or object_name missing for upload')
return False
if operation == 'upload':
self.multipart_upload_with_s3(bucket_name=bucket_name, file_path=file_path,
object_name=object_name)
else:
self.multipart_download_with_s3(bucket_name=bucket_name, file_path=file_path,
object_name=object_name)
return True
def object_operation(self, bucket_name=None, object_name=None, file_path=None,
operation='list'):
try:
if not bucket_name:
logging.error('The bucket name %s is missing for %s operation!'
% (bucket_name, operation))
return False
if operation == 'list':
self.list_object_op(bucket_name)
elif operation == 'delete':
return self.delete_object_op(bucket_name, object_name, operation)
elif operation == 'upload' or operation == 'download':
return self.upload_download_object_op(bucket_name, object_name,
file_path, operation)
else:
logging.error('unknown object operation')
return False
except ClientError as e:
logging.error(e)
return False
return True
# Functions for files operation
def list_op_file(self, bucket_name):
current_bucket = self.s3_resource.Bucket(bucket_name)
print('The files in bucket %s:\n' % bucket_name)
for obj in current_bucket.objects.all():
print(obj.meta.data)
return True
def delete_op_file(self, bucket_name, file_name, operation):
if not file_name:
logging.error('The file name %s is missing for%s operation!'
% (file_name, operation))
return False
self.s3_client.delete_object(Bucket=bucket_name, Key=file_name)
return True
def upload_download_op_file(self, bucket_name, file_name, file_location,
region, operation):
if not file_location:
logging.error('The file location %d is missing for %s operation!'
% (file_location, operation))
return False
if operation == 'download':
self.s3_resource.Bucket(bucket_name).download_file(file_name, file_location)
elif operation == 'upload' and region is None:
self.s3_resource.Bucket(bucket_name).upload_file(file_location, file_name)
else:
location = {'LocationConstraint': region}
self.s3_resource.Bucket(bucket_name).upload_file(file_location, file_name,
CreateBucketConfiguration=location)
return True
def file_operation(self, bucket_name=None, file_name=None, file_location=None,
region=None, operation='list'):
if not bucket_name:
logging.error('The bucket name is %s missing!' % bucket_name)
return False
try:
if operation == 'list':
return self.list_op_file(bucket_name)
elif operation == 'delete':
return self.delete_op_file(bucket_name, file_name, operation)
elif operation == 'upload' or operation == 'download':
return self.upload_download_op_file(bucket_name, file_name,
file_location, region, operation)
else:
logging.error('unknown file operation')
return False
except ClientError as e:
logging.error(e)
return False
return True
# dummy calls
def main():
END_POINT_URL = 'http://uvo100ebn7cuuq50c0t.vm.cld.sr'
A_KEY = 'AKIAtEpiGWUcQIelPRlD1Pi6xQ'
S_KEY = 'YNV6xS8lXnCTGSy1x2vGkmGnmdJbZSapNXaSaRhK'
s3 = S3DataEndpoint(end_url=END_POINT_URL, accessKey=A_KEY, secretKey=S_KEY)
# bucket_name2 = 'otodata'
# bucket_name = 'testbucket'
# file_name = 'bee39.wav'
# file_name2 = 'aom (1).png'
#
# path_file_upload2 = '/home/sumit/Documents/Oto-DataSet/aom (1).png'
# path_file_download = 'bee39.wav'
# if s3.file_operation(bucket_name2, file_name2, path_file_upload2, None, 'upload'):
# print("Uploading file to S3 completed successfully!")
#
# if s3.file_operation(bucket_name, file_name, path_file_download, None, 'download'):
# print("Downloading the file to S3 has been completed successfully!")
if __name__ == "__main__":
main()