-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·165 lines (152 loc) · 5.06 KB
/
main.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
#!/usr/bin/env python
from minio import Minio
from minio.error import S3Error
from yaml import FullLoader
from yaml import load as yamlload
from argparse import ArgumentParser
from os.path import abspath, basename
class Parse:
def __init__(self):
self.parser = ArgumentParser()
self.loading = self.parser.add_mutually_exclusive_group()
self.parser.add_argument(
"-c", "--config",
type=str,
help="path to configfile for overriding default name",
default="config.yaml"
)
self.parser.add_argument(
"--host",
type=str,
help="override hostname of MinIO server",
default=None
)
self.parser.add_argument(
"-a", "--access_key",
type=str,
help="override access_key of MinIO server's user",
default=None
)
self.parser.add_argument(
"-s", "--secret_key",
type=str,
help="override secret_key of MinIO server's user",
default=None
)
self.loading.add_argument(
"-u", "--upload",
type=str,
help="path to file that will be uploaded",
default=None
)
self.loading.add_argument(
"-d", "--download",
action="store_true",
help="name of file that will be downloaded from bucket",
default=False
)
self.loading.add_argument(
"-l", "--list_files",
action="store_true",
help="list of files in the bucket",
default=False
)
self.parser.add_argument(
"-b", "--bucket",
type=str,
help="name of the bucket",
default=None
)
self.args = vars(self.parser.parse_args())
class Config:
def config_read(self):
with open(self.args['config'], "r") as cfgyaml:
data = cfgyaml.read()
json = yamlload(data, Loader=FullLoader)
cfgyaml.close()
json['upload'] = False
json['list_files'] = False
json['download'] = False
for key in self.args:
if self.args[key] is not None:
json[key] = self.args[key]
return json
def __init__(self, args):
self.args = args
self.fullconfig = self.config_read()
class Client:
def __init__(self, conf):
self.conf = conf
self.client = Minio(
self.conf['host'],
access_key=self.conf['access_key'],
secret_key=self.conf['secret_key'],
secure=True
)
def check_bucket(self):
found = self.client.bucket_exists(self.conf['bucket'])
if not found:
if self.conf['list_files']:
print("No such bucket, abort.")
exit(0)
else:
self.client.make_bucket(self.conf['bucket'])
def upload(self):
self.check_bucket()
try:
filepath = abspath(self.conf['upload'])
filename = basename(filepath)
self.client.fput_object(
bucket_name=self.conf['bucket'],
object_name=filename,
file_path=filepath
)
print(f"{filepath} was successfully uploaded as {filename}")
except S3Error as exc:
print(exc)
except Exception as exc:
print(exc)
def download(self):
objects = self.list_files()
while True:
try:
choice = int(input("Choose file to download: "))
choice -= 1 # for indexing in list
filename = objects[choice]
obj = self.client.get_object(self.conf['bucket'], filename['_object_name'])
with open(filename['_object_name'], "wb") as file:
file.write(obj.data)
file.close()
print(f"File {filename['_object_name']} downloaded successfully.")
break
except KeyboardInterrupt:
print('Cancelled.')
break
except IndexError:
print('No such object, try again.')
continue
def list_files(self):
self.check_bucket()
objects = self.client.list_objects(bucket_name=self.conf['bucket'], recursive=True)
obj_dict = []
for obj in objects:
obj_dict.append(obj.__dict__)
counter = 1
print("Files in bucket:")
for obj in obj_dict:
print(f"{counter} {obj['_object_name']:<60} {obj['_last_modified']} {obj['_size']}")
counter += 1
if self.conf['download']:
return obj_dict
if __name__ == "__main__":
parser = Parse()
yaml = Config(parser.args)
minio_cli = Client(yaml.fullconfig)
if yaml.fullconfig['upload']:
minio_cli.upload()
elif yaml.fullconfig['download']:
minio_cli.download()
elif yaml.fullconfig['list_files']:
minio_cli.list_files()
else:
print('No actions')