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 pathcortxTest.py
172 lines (141 loc) · 5.87 KB
/
cortxTest.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
import os
import sys
import threading
import boto3
import logging
import shutil
from botocore.client import Config
from matplotlib import pyplot as plt
from botocore.exceptions import ClientError
from boto3.s3.transfer import TransferConfig
END_POINT_URL = 'http://uvo1baooraa1xb575uc.vm.cld.sr/'
A_KEY = 'AKIAtEpiGWUcQIelPRlD1Pi6xQ'
S_KEY = 'YNV6xS8lXnCTGSy1x2vGkmGnmdJbZSapNXaSaRhK'
class ProgressPercentage(object):
def __init__(self, filename):
self._filename = filename
self._size = float(os.path.getsize(filename))
self._seen_so_far = 0
self._lock = threading.Lock()
def __call__(self, bytes_amount):
# To simplify, assume this is hooked up to a single filename
with self._lock:
self._seen_so_far += bytes_amount
percentage = (self._seen_so_far / self._size) * 100
sys.stdout.write("\r%s %s / %s (%.2f%%)" %
(self._filename, self._seen_so_far,
self._size, percentage))
sys.stdout.flush()
"""Functions for buckets operation"""
def create_bucket_op(bucket_name, region):
if region is None:
s3_client.create_bucket(Bucket=bucket_name)
else:
location = {'LocationConstraint': region}
s3_client.create_bucket(Bucket=bucket_name,
CreateBucketConfiguration=location)
def list_bucket_op(bucket_name, region, operation):
buckets = 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(bucket_name, region=None, operation='list'):
try:
if operation == 'delete':
s3_client.delete_bucket(Bucket=bucket_name)
elif operation == 'create':
create_bucket_op(bucket_name, region)
elif operation == 'list':
return 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
def upload_download_op_file(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':
s3_resource.Bucket(bucket_name).download_file(file_name, file_location)
elif operation == 'upload' and region is None:
s3_resource.Bucket(bucket_name).upload_file(file_location, file_name)
else:
location = {'LocationConstraint': region}
s3_resource.Bucket(bucket_name
).upload_file(file_location, file_name,
CreateBucketConfiguration=location)
return True
"""Functions for files operation"""
def list_op_file(bucket_name):
current_bucket = 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(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
s3_client.delete_object(Bucket=bucket_name, Key=file_name)
return True
def file_operation(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 list_op_file(bucket_name)
elif operation == 'delete':
return delete_op_file(bucket_name, file_name, operation)
elif operation == 'upload' or operation == 'download':
return 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
s3_resource = boto3.resource('s3', endpoint_url=END_POINT_URL,
aws_access_key_id=A_KEY,
aws_secret_access_key=S_KEY,
config=Config(signature_version='s3v4'),
region_name='US')
s3_client = boto3.client('s3', endpoint_url=END_POINT_URL,
aws_access_key_id=A_KEY,
aws_secret_access_key=S_KEY,
config=Config(signature_version='s3v4'),
region_name='US')
bucket_name = 'detection'
file_name = r'0_5.txt'
# path_file_upload = r'C:\PycharmProjects\cortxHackton\upload\0_5.txt'
# assert os.path.isfile(path_file_upload)
# with open(path_file_upload, "r") as f:
# pass
path_file_download = r'download\0_5.txt'
path_save = ''
if bucket_operation(bucket_name, None, 'list'):
print("Bucket creation completed successfully!")
#
# if file_operation(bucket_name, file_name, path_file_upload, None, 'upload'):
# print("Uploading file to S3 completed successfully!")
if file_operation(bucket_name, file_name, path_file_download, None, 'download'):
print("Downloading the file to S3 has been completed successfully!")
# if file_operation(bucket_name, file_name, path_file_download, None, 'delete'):
# print("Downloading the file to S3 has been completed successfully!")
# zip_point = ''
# shutil.make_archive(zip_point, 'zip', path_save)
# if file_operation(bucket_name, '.json', path_save + '.json', None, 'upload'):
# print("Uploading file to S3 completed successfully!")