-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3.py
More file actions
194 lines (177 loc) · 6.73 KB
/
s3.py
File metadata and controls
194 lines (177 loc) · 6.73 KB
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
import os
import tqdm
import glob
import boto3
import typing
import logging
from pathlib import Path
from .err.s3 import errorhandler
logging.basicConfig(
level=logging.INFO, format="%(asctime)s -- [%(module)s:%(lineno)s - %(levelname)s] -- %(message)s"
)
logger = logging.getLogger(__name__)
class S3():
def __init__(
self,
bucket: str = None,
aws_access_key_id=os.environ.get("AWS_ACCESS_KEY_ID", None),
aws_secret_access_key=os.environ.get("AWS_SECRET_ACCESS_KEY", None),
region_name=os.environ.get("AWS_DEFAULT_REGION", None)
):
self.bucket = bucket
# resource
self.s3r = boto3.resource(
's3',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=region_name
)
# client
self.s3c = boto3.client(
's3',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=region_name
)
@errorhandler
def contains(self, prefix: str, bucket: str = None) -> bool:
"""Check if prefix folder contains folder/file path"""
bucket = bucket or self.bucket
if len(prefix.split("/")[-1].split(".")) <= 1:
if prefix.endswith("/"):
prefix = prefix[:-1]
objects = self.s3c.list_objects(
Bucket=bucket, Prefix=prefix, Delimiter="/", MaxKeys=1)
return prefix + "/" == objects.get('CommonPrefixes')[0].get("Prefix")
else:
self.s3r.Object(bucket, os.path.join(prefix)).load()
return True
@errorhandler
def list_all(self, prefix: str, bucket: str = None) -> list:
"""
List all files recursively within the prefix folder
Maximum Len Returns: 1000
"""
bucket = bucket or self.bucket
objects = self.s3c.list_objects(Bucket=bucket, Prefix=prefix)
paths = [x.get("Key") for x in objects.get("Contents", [])]
return paths
@errorhandler
def list_dirs(self, prefix: str, delimiter="/", bucket: str = None) -> list:
"""
List all folders one level within the prefix folder
Maximum Len Returns: 1000
"""
bucket = bucket or self.bucket
objects = self.s3c.list_objects(
Bucket=bucket, Prefix=prefix, Delimiter=delimiter)
assert objects.get(
"CommonPrefixes"), "No objects found inside the directory (Please provide existed directory)"
paths = [x.get("Prefix") for x in objects.get("CommonPrefixes")]
return paths
@errorhandler
def upload_file(
self,
prefix: str,
path: str,
filename: str = "",
bucket: str = None
) -> None:
bucket = bucket or self.bucket
prefix = self.parse_prefix(prefix)
if not filename:
file = os.path.basename(path)
self.s3c.upload_file(path, bucket, os.path.join(prefix, file))
else:
if "." not in filename:
_, ext = self.parse_file_ext(path)
filename += ext
self.s3c.upload_file(path, bucket, os.path.join(prefix, filename))
@errorhandler
def upload_dir(
self,
prefix: str,
path: str,
include_root: bool = False,
folder_name: str = "",
upload_only: typing.List = [],
bucket: str = None
):
bucket = bucket or self.bucket
if not folder_name:
folder_name = self.parse_prefix(path).split("/")[-1]
path = self.parse_prefix(path)
cwd = str(Path.cwd())
p = Path(os.path.join(Path.cwd(), path))
dir = list(p.glob('**'))
for d in tqdm.tqdm(dir, desc=f"Uploading artifacts"):
files = glob.glob(os.path.join(d, "*"))
for file in files:
if not Path(file).is_dir():
file = self.parse_prefix(str(file).replace(cwd, ''), True)
if len(upload_only) > 0 and file.replace(path + "/", "").split("/")[0] not in upload_only:
continue
path = path.replace(".", "").replace("/", "")
if include_root:
s3Path = self.parse_prefix(
file.replace(path, folder_name), True)
else:
s3Path = self.parse_prefix(
file.replace(path, ""), True)
s3Path = os.path.join(self.parse_prefix(prefix), s3Path)
self.s3c.upload_file(file, bucket, s3Path)
@errorhandler
def download_file(
self,
prefix: str,
save_path=".",
rename_to="",
bucket: str = None
) -> str:
bucket = bucket or self.bucket
filename = os.path.basename(prefix)
filename = rename_to + \
os.path.splitext(filename)[-1] if rename_to else filename
path = os.path.join(save_path, filename)
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
self.s3c.download_file(bucket, prefix, path)
return os.path.join(path)
@errorhandler
def download_file_from_uri(
self,
s3_uri,
save_path=".",
rename_to=""
) -> str:
bucket, prefix = self.parse_s3_uri(s3_uri)
return self.download_file(prefix, save_path, rename_to, bucket=bucket)
@errorhandler
def download_dir(self, prefix: str, save_path=".", bucket: str = None) -> None:
bucket = bucket or self.bucket
bucket = self.s3r.Bucket(bucket)
for obj in bucket.objects.filter(Prefix=prefix):
path = os.path.join(save_path, os.path.relpath(obj.key, prefix))
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
bucket.download_file(obj.key, path)
@errorhandler
def download_dir_from_uri(self, s3_uri: str, save_path=".") -> None:
bucket, prefix = self.parse_s3_uri(s3_uri)
self.download_dir(prefix, save_path, bucket=bucket)
@staticmethod
def parse_s3_uri(s3_uri: str) -> typing.Tuple[str, str]:
uri = s3_uri.replace("s3://", "")
bucket, prefix = uri.split("/")[0], '/'.join(uri.split("/")[1:])
return bucket, prefix
@staticmethod
def parse_prefix(prefix: str, include_file: bool = False) -> str:
"""Turn prefix string to good format"""
p = prefix.replace("/", " ").split()
if not include_file:
p = p[:-1] if "." in p[-1] else p
return '/'.join(p)
@staticmethod
def parse_file_ext(path: str) -> typing.Tuple[str, str]:
file = os.path.basename(path)
return os.path.splitext(file)