-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paths3_utils.py
473 lines (430 loc) · 19.9 KB
/
s3_utils.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import concurrent.futures
import datetime
import math
import pathlib
import random
import threading
import time
from timeit import default_timer as timer
import os
import io
import boto3
import botocore
from botocore.client import Config
default_profile = 'default'
default_cache_root_path = (pathlib.Path(__file__).parent / '../data/s3_cache').resolve()
def get_s3_client():
if default_profile in boto3.Session()._session.available_profiles:
session = boto3.Session(profile_name=default_profile)
else:
session = boto3.Session()
config = Config(connect_timeout=250, read_timeout=250)
return session.client('s3', config=config)
def get_s3_resource():
if default_profile in boto3.Session()._session.available_profiles:
session = boto3.Session(profile_name=default_profile)
else:
session = boto3.Session()
config = Config(connect_timeout=250, read_timeout=250)
return session.client('s3', config=config)
def key_exists(bucket, key):
# Return true if a key exists in s3 bucket
client = get_s3_client()
try:
client.head_object(Bucket=bucket, Key=key)
return True
except botocore.exceptions.ClientError as exc:
if exc.response['Error']['Code'] != '404':
raise
return False
except:
raise
def get_s3_object_bytes_parallel(keys, *,
bucket,
cache_on_local_disk=True,
cache_root_path=None,
verbose=True,
max_num_threads=20,
num_tries=5,
initial_delay=1.0,
delay_factor=math.sqrt(2.0),
download_callback=None,
skip_modification_time_check=False):
if cache_on_local_disk:
assert cache_root_path is not None
cache_root_path = pathlib.Path(cache_root_path).resolve()
missing_keys = []
existing_keys = []
for key in keys:
local_filepath = cache_root_path / key
if not local_filepath.is_file():
missing_keys.append(key)
local_filepath.parent.mkdir(parents=True, exist_ok=True)
else:
existing_keys.append(key)
keys_to_download = missing_keys.copy()
if skip_modification_time_check:
if verbose:
print(f'Skipping the file modification time check for {len(existing_keys)} keys that have local copies.')
for key in existing_keys:
if download_callback:
download_callback(1)
else:
if verbose:
print(f'Getting metadata for {len(existing_keys)} keys that have local copies ... ', end='')
metadata_start = timer()
metadata = get_s3_object_metadata_parallel(existing_keys,
bucket=bucket,
verbose=False,
max_num_threads=max_num_threads,
num_tries=num_tries,
initial_delay=initial_delay,
delay_factor=delay_factor,
download_callback=None)
metadata_end = timer()
if verbose:
print(f'took {metadata_end - metadata_start:.3f} seconds')
for key in existing_keys:
local_filepath = cache_root_path / key
assert local_filepath.is_file
local_time = datetime.datetime.fromtimestamp(local_filepath.stat().st_mtime,
datetime.timezone.utc)
remote_time = metadata[key]['LastModified']
if local_time <= remote_time:
if verbose:
print(f'Local copy of key "{key}" is outdated')
keys_to_download.append(key)
elif download_callback:
download_callback(1)
tl = threading.local()
def cur_download_file(key):
local_filepath = cache_root_path / key
if verbose:
print('{} not available locally our outdated, downloading from S3 ... '.format(key))
download_s3_file_with_backoff(key,
str(local_filepath),
bucket=bucket,
num_tries=num_tries,
initial_delay=initial_delay,
delay_factor=delay_factor,
thread_local=tl)
return local_filepath.is_file()
if len(keys_to_download) > 0:
download_start = timer()
with concurrent.futures.ThreadPoolExecutor(max_workers=max_num_threads) as executor:
future_to_key = {executor.submit(cur_download_file, key): key for key in keys_to_download}
for future in concurrent.futures.as_completed(future_to_key):
key = future_to_key[future]
try:
success = future.result()
assert success
if download_callback:
download_callback(1)
except Exception as exc:
print('Key {} generated an exception: {}'.format(key, exc))
raise exc
download_end = timer()
if verbose:
print('Downloading took {:.3f} seconds'.format(download_end - download_start))
result = {}
# TODO: parallelize this as well?
for key in keys:
local_filepath = cache_root_path / key
if verbose:
print('Reading from local file {} ... '.format(local_filepath), end='')
with open(local_filepath, 'rb') as f:
result[key] = f.read()
if verbose:
print('done')
else:
tl = threading.local()
def cur_get_object_bytes(key):
if verbose:
print('Loading {} from S3 ... '.format(key))
return get_s3_object_bytes_with_backoff(key,
bucket=bucket,
num_tries=num_tries,
initial_delay=initial_delay,
delay_factor=delay_factor,
thread_local=tl)[0]
download_start = timer()
result = {}
with concurrent.futures.ThreadPoolExecutor(max_workers=max_num_threads) as executor:
future_to_key = {executor.submit(cur_get_object_bytes, key): key for key in keys}
for future in concurrent.futures.as_completed(future_to_key):
key = future_to_key[future]
try:
result[key] = future.result()
if download_callback:
download_callback(1)
except Exception as exc:
print('Key {} generated an exception: {}'.format(key, exc))
raise exc
download_end = timer()
if verbose:
print('Getting object bytes took {} seconds'.format(download_end - download_start))
return result
def get_s3_object_bytes_with_backoff(key, *,
bucket,
num_tries=5,
initial_delay=1.0,
delay_factor=math.sqrt(2.0),
num_replicas=1,
thread_local=None):
if thread_local is None:
client = get_s3_client()
else:
if not hasattr(thread_local, 'get_object_client'):
thread_local.get_object_client = get_s3_client()
client = thread_local.get_object_client
delay = initial_delay
num_tries_left = num_tries
if num_replicas > 1:
replicas_counter_len = len(str(num_replicas))
format_string = '_replica{{:0{}d}}-{{}}'.format(replicas_counter_len)
while num_tries_left >= 1:
try:
if num_replicas > 1:
cur_replica = random.randint(1, num_replicas)
cur_key = key + format_string.format(cur_replica, num_replicas)
else:
cur_key = key
read_bytes = client.get_object(Key=cur_key, Bucket=bucket)["Body"].read()
return read_bytes, cur_key
except:
if num_tries_left == 1:
raise Exception('get backoff failed ' + key + ' ' + str(delay))
else:
time.sleep(delay)
delay *= delay_factor
num_tries_left -= 1
def get_s3_object_metadata_with_backoff(key, *,
bucket,
num_tries=5,
initial_delay=1.0,
delay_factor=math.sqrt(2.0),
thread_local=None):
if thread_local is None:
client = get_s3_client()
else:
if not hasattr(thread_local, 'get_object_client'):
thread_local.get_object_client = get_s3_client()
client = thread_local.get_object_client
delay = initial_delay
num_tries_left = num_tries
while num_tries_left >= 1:
try:
metadata = client.head_object(Key=key, Bucket=bucket)
return metadata
except:
if num_tries_left == 1:
raise Exception('get backoff failed ' + key + ' ' + str(delay))
else:
time.sleep(delay)
delay *= delay_factor
num_tries_left -= 1
def get_s3_object_metadata_parallel(keys,
bucket,
verbose=True,
max_num_threads=20,
num_tries=5,
initial_delay=1.0,
delay_factor=math.sqrt(2.0),
download_callback=None):
tl = threading.local()
def cur_get_object_metadata(key):
if verbose:
print('Loading metadata for {} from S3 ... '.format(key))
return get_s3_object_metadata_with_backoff(key,
bucket=bucket,
num_tries=num_tries,
initial_delay=initial_delay,
delay_factor=delay_factor,
thread_local=tl)
download_start = timer()
result = {}
with concurrent.futures.ThreadPoolExecutor(max_workers=max_num_threads) as executor:
future_to_key = {executor.submit(cur_get_object_metadata, key): key for key in keys}
for future in concurrent.futures.as_completed(future_to_key):
key = future_to_key[future]
try:
result[key] = future.result()
if download_callback:
download_callback(1)
except Exception as exc:
print('Key {} generated an exception: {}'.format(key, exc))
raise exc
download_end = timer()
if verbose:
print('Getting object metadata took {} seconds'.format(download_end - download_start))
return result
def put_s3_object_bytes_with_backoff(file_bytes, key, bucket, num_tries=10, initial_delay=1.0, delay_factor=2.0):
client = get_s3_client()
delay = initial_delay
num_tries_left = num_tries
while num_tries_left >= 1:
try:
bio = io.BytesIO(file_bytes)
client.upload_fileobj(bio, Key=key, Bucket=bucket, ExtraArgs={'ACL': 'bucket-owner-full-control'})
return
except:
if num_tries_left == 1:
print('put backoff failed' + key)
raise Exception('put backoff failed ' + key + ' ' + str(len(file_bytes))+ ' ' + str(delay))
else:
time.sleep(delay)
delay *= delay_factor
num_tries_left -= 1
def list_all_keys(client, bucket, prefix, max_keys=None):
objects = client.list_objects(Bucket=bucket, Prefix=prefix, Delimiter=prefix)
if (objects.get('Contents') == None):
return []
keys = list(map(lambda x: x['Key'], objects.get('Contents', [] )))
truncated = objects['IsTruncated']
next_marker = objects.get('NextMarker')
while truncated:
objects = client.list_objects(Bucket=bucket, Prefix=prefix,
Delimiter=prefix, Marker=next_marker)
truncated = objects['IsTruncated']
next_marker = objects.get('NextMarker')
keys += list(map(lambda x: x['Key'], objects['Contents']))
if (max_keys is not None and len(keys) >= max_keys):
break
return list(filter(lambda x: len(x) > 0, keys))
def download_s3_file_with_backoff(key, local_filename, *,
bucket,
num_tries=5,
initial_delay=1.0,
delay_factor=math.sqrt(2.0),
num_replicas=1,
thread_local=None):
if thread_local is None:
client = get_s3_client()
else:
if not hasattr(thread_local, 's3_client'):
thread_local.s3_client = get_s3_client()
client = thread_local.s3_client
delay = initial_delay
num_tries_left = num_tries
if num_replicas > 1:
replicas_counter_len = len(str(num_replicas))
format_string = '_replica{{:0{}d}}-{{}}'.format(replicas_counter_len)
while num_tries_left >= 1:
try:
if num_replicas > 1:
cur_replica = random.randint(1, num_replicas)
cur_key = key + format_string.format(cur_replica, num_replicas)
else:
cur_key = key
client.download_file(bucket, cur_key, local_filename)
return cur_key
except:
if num_tries_left == 1:
raise Exception('download backoff failed ' + ' ' + str(key) + ' ' + str(delay))
else:
time.sleep(delay)
delay *= delay_factor
num_tries_left -= 1
def upload_file_to_s3_with_backoff(local_filename, key, *,
bucket,
num_tries=5,
initial_delay=1.0,
delay_factor=math.sqrt(2.0),
thread_local=None):
assert pathlib.Path(local_filename).is_file()
if thread_local is None:
client = get_s3_client()
else:
if not hasattr(thread_local, 's3_client'):
thread_local.s3_client = get_s3_client()
client = thread_local.s3_client
delay = initial_delay
num_tries_left = num_tries
while num_tries_left >= 1:
try:
client.upload_file(local_filename, bucket, key)
return
except:
if num_tries_left == 1:
raise Exception('upload backoff failed ' + ' ' + str(key) + ' ' + str(delay))
else:
time.sleep(delay)
delay *= delay_factor
num_tries_left -= 1
def default_option_if_needed(*, user_option, default):
if user_option is None:
return default
else:
return user_option
class S3Wrapper:
def __init__(self,
bucket,
cache_on_local_disk=True,
cache_root_path=default_cache_root_path,
verbose=False,
max_num_threads=20,
num_tries=5,
initial_delay=1.0,
delay_factor=math.sqrt(2.0),
skip_modification_time_check=True):
self.bucket = bucket
self.cache_on_local_disk = cache_on_local_disk
self.client = get_s3_client()
if self.cache_on_local_disk:
assert cache_root_path is not None
self.cache_root_path = pathlib.Path(cache_root_path).resolve()
self.cache_root_path.mkdir(parents=True, exist_ok=True)
assert self.cache_root_path.is_dir()
else:
self.cache_root_path = None
self.verbose = verbose
self.max_num_threads = max_num_threads
self.num_tries = num_tries
self.initial_delay = initial_delay
self.delay_factor = delay_factor
self.skip_modification_time_check = skip_modification_time_check
def list_keys(self, prefix, max_keys=None):
return list_all_keys(self.client, self.bucket, prefix, max_keys)
def put(self, bytes_to_store, key, verbose=None):
cur_verbose = default_option_if_needed(user_option=verbose, default=self.verbose)
put_s3_object_bytes_with_backoff(bytes_to_store,
key,
bucket=self.bucket,
num_tries=self.num_tries,
initial_delay=self.initial_delay,
delay_factor=self.delay_factor)
if cur_verbose:
print('Stored {} bytes under key {}'.format(len(bytes_to_store), key))
def put_multiple(self, data, verbose=None, callback=None):
# TODO: add a new function for parallel uploading
for key, bytes_to_store in data.items():
self.put(bytes_to_store, key, verbose)
def upload_file(self, filename, key):
upload_file_to_s3_with_backoff(filename,
key,
bucket=self.bucket,
num_tries=self.num_tries,
initial_delay=self.initial_delay,
delay_factor=self.delay_factor,
thread_local=None)
def get(self, key, verbose=None, skip_modification_time_check=None):
return self.get_multiple([key], verbose=verbose, skip_modification_time_check=skip_modification_time_check)[key]
def get_multiple(self, keys, verbose=None, callback=None, skip_modification_time_check=None):
if verbose is None:
cur_verbose = self.verbose
else:
cur_verbose = verbose
cur_verbose = default_option_if_needed(user_option=verbose, default=self.verbose)
cur_skip_time_check = default_option_if_needed(user_option=skip_modification_time_check,
default=self.skip_modification_time_check)
return get_s3_object_bytes_parallel(keys,
bucket=self.bucket,
cache_on_local_disk=self.cache_on_local_disk,
cache_root_path=self.cache_root_path,
verbose=cur_verbose,
max_num_threads=self.max_num_threads,
num_tries=self.num_tries,
initial_delay=self.initial_delay,
delay_factor=self.delay_factor,
download_callback=callback,
skip_modification_time_check=cur_skip_time_check)