-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathdata_transfer.py
More file actions
1473 lines (1214 loc) · 54.4 KB
/
data_transfer.py
File metadata and controls
1473 lines (1214 loc) · 54.4 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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import binascii
import concurrent
import functools
import hashlib
import itertools
import logging
import math
import os
import pathlib
import queue
import shutil
import stat
import threading
import types
import warnings
from codecs import iterdecode
from collections import defaultdict, deque
from concurrent.futures import Future, ThreadPoolExecutor
from dataclasses import dataclass
from enum import Enum
from threading import Lock
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple
import jsonlines
from boto3.s3.transfer import TransferConfig
from botocore import UNSIGNED
from botocore.client import Config
from botocore.exceptions import (
ClientError,
ConnectionError,
HTTPClientError,
ReadTimeoutError,
)
from s3transfer.utils import ReadFileChunk
from tenacity import (
retry,
retry_if_not_result,
retry_if_result,
stop_after_attempt,
wait_exponential,
)
from tqdm import tqdm
from . import hooks, util
from .session import get_boto3_session
from .util import DISABLE_TQDM, PhysicalKey, QuiltException
MAX_COPY_FILE_LIST_RETRIES = 3
MAX_FIX_HASH_RETRIES = 3
MAX_CONCURRENCY = util.get_pos_int_from_env('QUILT_TRANSFER_MAX_CONCURRENCY') or 10
logger = logging.getLogger(__name__)
class S3Api(Enum):
GET_OBJECT = "GET_OBJECT"
HEAD_OBJECT = "HEAD_OBJECT"
LIST_OBJECT_VERSIONS = "LIST_OBJECT_VERSIONS"
LIST_OBJECTS_V2 = "LIST_OBJECTS_V2"
class S3NoValidClientError(Exception):
def __init__(self, message, **kwargs):
# We use NewError("Prefix: " + str(error)) a lot.
# To be consistent across Python 2.7 and 3.x:
# 1) This `super` call must exist, or 2.7 will have no text for str(error)
# 2) This `super` call must have only one argument (the message) or str(error) will be a repr of args
super().__init__(message)
self.message = message
for k, v in kwargs.items():
setattr(self, k, v)
class S3ClientProvider:
"""
An s3_client is either signed with standard credentials or unsigned. This class exists to dynamically provide the
correct s3 client (either standard_client or unsigned_client) for a bucket. This means if standard credentials
can't read from the bucket, check if the bucket is public in which case we should be using an unsigned client.
This check is expensive at scale so the class also keeps track of which client to use for each bucket+api_call.
If there are no credentials available at all (i.e. you don't have AWS credentials and you don't have a
Quilt-provided role from quilt3.login()), the standard client will also be unsigned so that users can still
access public s3 buckets.
We assume that public buckets are read-only: write operations should always use S3ClientProvider.standard_client
"""
_client_map = {}
@classmethod
def set_s3_client(cls, bucket: str, client):
assert bucket is not None
cls._client_map[bucket] = client
def __init__(self):
self._use_unsigned_client = {} # f'{action}/{bucket}' -> use_unsigned_client_bool
self._standard_client = None
self._unsigned_client = None
def get_standard_client(self, bucket):
mapped_client = self.__class__._client_map.get(bucket)
if mapped_client is not None:
return mapped_client
else:
if self._standard_client is None:
self._build_standard_client()
return self._standard_client
@property
def unsigned_client(self):
if self._unsigned_client is None:
self._build_unsigned_client()
return self._unsigned_client
def get_correct_client(self, action: S3Api, bucket: str):
if not self.client_type_known(action, bucket):
raise RuntimeError("get_correct_client was called, but the correct client type is not known. Only call "
"get_correct_client() after checking if client_type_known()")
if self.should_use_unsigned_client(action, bucket):
return self.unsigned_client
else:
return self.get_standard_client(bucket)
def key(self, action: S3Api, bucket: str):
return f"{action}/{bucket}"
def set_cache(self, action: S3Api, bucket: str, use_unsigned: bool):
self._use_unsigned_client[self.key(action, bucket)] = use_unsigned
def should_use_unsigned_client(self, action: S3Api, bucket: str):
# True if should use unsigned, False if should use standard, None if don't know yet
return self._use_unsigned_client.get(self.key(action, bucket))
def client_type_known(self, action: S3Api, bucket: str):
return self.should_use_unsigned_client(action, bucket) is not None
def find_correct_client(self, api_type, bucket, param_dict):
if self.client_type_known(api_type, bucket):
return self.get_correct_client(api_type, bucket)
else:
check_fn_mapper = {
S3Api.GET_OBJECT: check_get_object_works_for_client,
S3Api.HEAD_OBJECT: check_head_object_works_for_client,
S3Api.LIST_OBJECTS_V2: check_list_objects_v2_works_for_client,
S3Api.LIST_OBJECT_VERSIONS: check_list_object_versions_works_for_client
}
assert api_type in check_fn_mapper, f"Only certain APIs are supported with unsigned_client. The " \
f"API '{api_type}' is not current supported. You may want to use S3ClientProvider.standard_client " \
f"instead "
check_fn = check_fn_mapper[api_type]
if check_fn(self.get_standard_client(bucket), param_dict):
self.set_cache(api_type, bucket, use_unsigned=False)
return self.get_standard_client(bucket)
else:
if check_fn(self.unsigned_client, param_dict):
self.set_cache(api_type, bucket, use_unsigned=True)
return self.unsigned_client
else:
raise S3NoValidClientError(f"S3 AccessDenied for {api_type} on bucket: {bucket}")
def get_boto_session(self):
return get_boto3_session()
@staticmethod
def _build_client_base(session, client_kwargs):
return session.client('s3', **client_kwargs)
def _build_client(self, is_unsigned):
session = self.get_boto_session()
conf_kwargs = {
"max_pool_connections": MAX_CONCURRENCY,
}
if is_unsigned(session):
conf_kwargs["signature_version"] = UNSIGNED
client_kwargs = {
"config": Config(**conf_kwargs),
}
hook = hooks.get_build_s3_client_hook()
return (
self._build_client_base(session, client_kwargs)
if hook is None else
hook(self._build_client_base, session, client_kwargs)
)
def _build_standard_client(self):
s3_client = self._build_client(lambda session: session.get_credentials() is None)
self._standard_client = s3_client
def _build_unsigned_client(self):
s3_client = self._build_client(lambda session: True)
self._unsigned_client = s3_client
def check_list_object_versions_works_for_client(s3_client, params):
try:
s3_client.list_object_versions(**params, MaxKeys=1) # Make this as fast as possible
except ClientError as e:
return e.response["Error"]["Code"] != "AccessDenied"
return True
def check_list_objects_v2_works_for_client(s3_client, params):
try:
s3_client.list_objects_v2(**params, MaxKeys=1) # Make this as fast as possible
except ClientError as e:
if e.response["Error"]["Code"] == "AccessDenied":
return False
return True
def check_get_object_works_for_client(s3_client, params):
try:
head_args = dict(
Bucket=params["Bucket"],
Key=params["Key"]
)
if "VersionId" in params:
head_args["VersionId"] = params["VersionId"]
s3_client.head_object(**head_args) # HEAD/GET share perms, but HEAD always fast
except ClientError as e:
if e.response["Error"]["Code"] == "403":
# This can also happen if you have full get_object access, but not list_objects_v2, and the object does not
# exist. Instead of returning a 404, S3 will return a 403.
return False
return True
def check_head_object_works_for_client(s3_client, params):
try:
s3_client.head_object(**params)
except ClientError as e:
if e.response["Error"]["Code"] == "403":
# This can also happen if you have full get_object access, but not list_objects_v2, and the object does not
# exist. Instead of returning a 404, S3 will return a 403.
return False
return True
s3_transfer_config = TransferConfig()
def read_file_chunks(file, chunksize=s3_transfer_config.io_chunksize):
return itertools.takewhile(bool, map(file.read, itertools.repeat(chunksize)))
# When uploading files at least this size, compare the ETags first and skip the upload if they're equal;
# copy the remote file onto itself if the metadata changes.
UPLOAD_ETAG_OPTIMIZATION_THRESHOLD = 1024
# 8 MiB - same as TransferConfig().multipart_threshold - but hard-coded to guarantee it won't change.
CHECKSUM_MULTIPART_THRESHOLD = 8 * 1024 * 1024
# Maximum number of parts supported by S3
CHECKSUM_MAX_PARTS = 10_000
@dataclass
class WorkerContext:
s3_client_provider: S3ClientProvider
progress: Callable[[int], None]
done: Callable[[PhysicalKey, Optional[str]], None]
run: Callable[..., None]
def get_checksum_chunksize(file_size: int) -> int:
"""
Calculate the chunk size to be used for the checksum. It is normally 8 MiB,
but gets doubled as long as the number of parts exceeds the maximum of 10,000.
It is the same as
`ChunksizeAdjuster().adjust_chunksize(s3_transfer_config.multipart_chunksize, file_size)`,
but hard-coded to guarantee it won't change and make the current behavior a part of the API.
"""
chunksize = 8 * 1024 * 1024
num_parts = math.ceil(file_size / chunksize)
while num_parts > CHECKSUM_MAX_PARTS:
chunksize *= 2
num_parts = math.ceil(file_size / chunksize)
return chunksize
def is_mpu(file_size: int) -> bool:
return file_size >= CHECKSUM_MULTIPART_THRESHOLD
_EMPTY_STRING_SHA256 = hashlib.sha256(b'').digest()
def _simple_s3_to_quilt_checksum(s3_checksum: str) -> str:
"""
Converts a SHA256 hash from a regular (non-multipart) S3 upload into a multipart hash,
i.e., base64(sha256(bytes)) -> base64(sha256([sha256(bytes)])).
Edge case: a 0-byte upload is treated as an empty list of chunks, rather than a list of a 0-byte chunk.
Its checksum is sha256(''), NOT sha256(sha256('')).
"""
s3_checksum_bytes = binascii.a2b_base64(s3_checksum)
if s3_checksum_bytes == _EMPTY_STRING_SHA256:
# Do not hash it again.
return s3_checksum
quilt_checksum_bytes = hashlib.sha256(s3_checksum_bytes).digest()
return binascii.b2a_base64(quilt_checksum_bytes, newline=False).decode()
def _copy_local_file(ctx: WorkerContext, size: int, src_path: str, dest_path: str):
pathlib.Path(dest_path).parent.mkdir(parents=True, exist_ok=True)
# TODO(dima): More detailed progress.
shutil.copyfile(src_path, dest_path)
ctx.progress(size)
shutil.copymode(src_path, dest_path)
ctx.done(PhysicalKey.from_path(dest_path), None)
def _upload_file(ctx: WorkerContext, size: int, src_path: str, dest_bucket: str, dest_key: str):
s3_client = ctx.s3_client_provider.get_standard_client(dest_bucket)
if not is_mpu(size):
with ReadFileChunk.from_filename(src_path, 0, size, [ctx.progress]) as fd:
resp = s3_client.put_object(
Body=fd,
Bucket=dest_bucket,
Key=dest_key,
ChecksumAlgorithm='SHA256',
)
version_id = resp.get('VersionId') # Absent in unversioned buckets.
checksum = _simple_s3_to_quilt_checksum(resp['ChecksumSHA256'])
ctx.done(PhysicalKey(dest_bucket, dest_key, version_id), checksum)
else:
resp = s3_client.create_multipart_upload(
Bucket=dest_bucket,
Key=dest_key,
ChecksumAlgorithm='SHA256',
)
upload_id = resp['UploadId']
chunksize = get_checksum_chunksize(size)
chunk_offsets = list(range(0, size, chunksize))
lock = Lock()
remaining = len(chunk_offsets)
parts = [None] * remaining
def upload_part(i, start, end):
nonlocal remaining
part_id = i + 1
with ReadFileChunk.from_filename(src_path, start, end-start, [ctx.progress]) as fd:
part = s3_client.upload_part(
Body=fd,
Bucket=dest_bucket,
Key=dest_key,
UploadId=upload_id,
PartNumber=part_id,
ChecksumAlgorithm='SHA256',
)
with lock:
parts[i] = dict(
PartNumber=part_id,
ETag=part['ETag'],
ChecksumSHA256=part['ChecksumSHA256'],
)
remaining -= 1
done = remaining == 0
if done:
resp = s3_client.complete_multipart_upload(
Bucket=dest_bucket,
Key=dest_key,
UploadId=upload_id,
MultipartUpload={'Parts': parts},
)
version_id = resp.get('VersionId') # Absent in unversioned buckets.
checksum, _ = resp['ChecksumSHA256'].split('-', 1)
ctx.done(PhysicalKey(dest_bucket, dest_key, version_id), checksum)
for i, start in enumerate(chunk_offsets):
end = min(start + chunksize, size)
ctx.run(upload_part, i, start, end)
def _download_file(
ctx: WorkerContext,
size: int,
src_bucket: str,
src_key: str,
src_version: Optional[str],
dest_path: str
):
dest_file = pathlib.Path(dest_path)
if dest_file.is_reserved():
raise ValueError("Cannot download to %r: reserved file name" % dest_path)
params = dict(Bucket=src_bucket, Key=src_key)
s3_client = ctx.s3_client_provider.find_correct_client(S3Api.GET_OBJECT, src_bucket, params)
dest_file.parent.mkdir(parents=True, exist_ok=True)
with dest_file.open('wb') as f:
fileno = f.fileno()
is_regular_file = stat.S_ISREG(os.stat(fileno).st_mode)
# TODO: To enable this we need to fix some tests in test_packages,
# that setup mocked responses to return less data than expected/specified in the manifest.
# if is_regular_file:
# # Preallocate file.
# if hasattr(os, 'posix_fallocate'):
# os.posix_fallocate(fileno, 0, size)
# else:
# f.truncate(size)
if src_version is not None:
params.update(VersionId=src_version)
# Note: we are not calculating checksums when downloading,
# so we're free to use S3 defaults (or anything else) here.
part_size = s3_transfer_config.multipart_chunksize
is_multi_part = (
is_regular_file
and size >= s3_transfer_config.multipart_threshold
and size > part_size
)
part_numbers = (
range(math.ceil(size / part_size))
if is_multi_part else
(None,)
)
remaining_counter = len(part_numbers)
remaining_counter_lock = Lock()
def download_part(part_number):
nonlocal remaining_counter
with dest_file.open('r+b') as chunk_f:
if part_number is not None:
start = part_number * part_size
end = min(start + part_size, size) - 1
part_params = dict(params, Range=f'bytes={start}-{end}')
chunk_f.seek(start)
else:
part_params = params
resp = s3_client.get_object(**part_params)
body = resp['Body']
while True:
chunk = body.read(s3_transfer_config.io_chunksize)
if not chunk:
break
ctx.progress(chunk_f.write(chunk))
with remaining_counter_lock:
remaining_counter -= 1
done = remaining_counter == 0
if done:
ctx.done(PhysicalKey.from_path(dest_path), None)
for part_number in part_numbers:
ctx.run(download_part, part_number)
def _copy_remote_file(ctx: WorkerContext, size: int, src_bucket: str, src_key: str, src_version: Optional[str],
dest_bucket: str, dest_key: str, extra_args: Optional[Iterable[Tuple[str, Any]]] = None):
src_params = dict(
Bucket=src_bucket,
Key=src_key
)
if src_version is not None:
src_params.update(
VersionId=src_version
)
s3_client = ctx.s3_client_provider.get_standard_client(dest_bucket)
if not is_mpu(size):
params: Dict[str, Any] = dict(
CopySource=src_params,
Bucket=dest_bucket,
Key=dest_key,
ChecksumAlgorithm='SHA256',
)
if extra_args:
params.update(extra_args)
resp = s3_client.copy_object(**params)
ctx.progress(size)
version_id = resp.get('VersionId') # Absent in unversioned buckets.
checksum = _simple_s3_to_quilt_checksum(resp['CopyObjectResult']['ChecksumSHA256'])
ctx.done(PhysicalKey(dest_bucket, dest_key, version_id), checksum)
else:
resp = s3_client.create_multipart_upload(
Bucket=dest_bucket,
Key=dest_key,
ChecksumAlgorithm='SHA256',
)
upload_id = resp['UploadId']
chunksize = get_checksum_chunksize(size)
chunk_offsets = list(range(0, size, chunksize))
lock = Lock()
remaining = len(chunk_offsets)
parts = [None] * remaining
def upload_part(i, start, end):
nonlocal remaining
part_id = i + 1
part = s3_client.upload_part_copy(
CopySource=src_params,
CopySourceRange=f'bytes={start}-{end-1}',
Bucket=dest_bucket,
Key=dest_key,
UploadId=upload_id,
PartNumber=part_id,
)
with lock:
parts[i] = dict(
PartNumber=part_id,
ETag=part['CopyPartResult']['ETag'],
ChecksumSHA256=part['CopyPartResult']['ChecksumSHA256'],
)
remaining -= 1
done = remaining == 0
ctx.progress(end - start)
if done:
resp = s3_client.complete_multipart_upload(
Bucket=dest_bucket,
Key=dest_key,
UploadId=upload_id,
MultipartUpload={'Parts': parts},
)
version_id = resp.get('VersionId') # Absent in unversioned buckets.
checksum, _ = resp['ChecksumSHA256'].split('-', 1)
ctx.done(PhysicalKey(dest_bucket, dest_key, version_id), checksum)
for i, start in enumerate(chunk_offsets):
end = min(start + chunksize, size)
ctx.run(upload_part, i, start, end)
def _calculate_local_checksum(path: str, size: int):
chunksize = get_checksum_chunksize(size)
part_hashes = []
for start in range(0, size, chunksize):
end = min(start + chunksize, size)
part_hashes.append(_calculate_local_part_checksum(path, start, end - start))
return _make_checksum_from_parts(part_hashes)
def _reuse_remote_file(ctx: WorkerContext, size: int, src_path: str, dest_bucket: str, dest_path: str):
# Optimization: check if the remote file already exists and has the right ETag,
# and skip the upload.
if size < UPLOAD_ETAG_OPTIMIZATION_THRESHOLD:
return None
try:
params = dict(Bucket=dest_bucket, Key=dest_path)
s3_client = ctx.s3_client_provider.find_correct_client(S3Api.HEAD_OBJECT, dest_bucket, params)
resp = s3_client.head_object(**params, ChecksumMode="ENABLED")
except ClientError:
# Destination doesn't exist, so fall through to the normal upload.
pass
except S3NoValidClientError:
# S3ClientProvider can't currently distinguish between a user that has PUT but not LIST permissions and a
# user that has no permissions. If we can't find a valid client, proceed to the upload stage anyway.
pass
else:
dest_size = resp["ContentLength"]
if dest_size != size:
return None
# TODO: we could check hashes of parts, to finish faster
s3_checksum = resp.get("ChecksumSHA256")
if s3_checksum is not None:
if "-" in s3_checksum:
checksum, num_parts_str = s3_checksum.split("-", 1)
num_parts = int(num_parts_str)
else:
checksum = _simple_s3_to_quilt_checksum(s3_checksum)
num_parts = None
expected_num_parts = math.ceil(size / get_checksum_chunksize(size)) if is_mpu(size) else None
if num_parts == expected_num_parts and checksum == _calculate_local_checksum(src_path, size):
return resp.get("VersionId"), checksum
elif resp.get("ServerSideEncryption") != "aws:kms" and resp["ETag"] == _calculate_etag(src_path):
return resp.get("VersionId"), _calculate_local_checksum(src_path, size)
return None
def _upload_or_reuse_file(ctx: WorkerContext, size: int, src_path: str, dest_bucket: str, dest_path: str):
result = _reuse_remote_file(ctx, size, src_path, dest_bucket, dest_path)
if result is not None:
dest_version_id, checksum = result
ctx.progress(size)
ctx.done(PhysicalKey(dest_bucket, dest_path, dest_version_id), checksum)
return # Optimization succeeded.
# If the optimization didn't happen, do the normal upload.
_upload_file(ctx, size, src_path, dest_bucket, dest_path)
def _copy_file_list_last_retry(retry_state):
return retry_state.fn(
*retry_state.args,
**{**retry_state.kwargs, 'exceptions_to_ignore': ()},
)
@retry(stop=stop_after_attempt(MAX_COPY_FILE_LIST_RETRIES - 1),
wait=wait_exponential(multiplier=1, min=1, max=10),
retry=retry_if_not_result(all),
retry_error_callback=_copy_file_list_last_retry)
def _copy_file_list_internal(file_list, results, message, callback, exceptions_to_ignore=(ClientError,)):
"""
Takes a list of tuples (src, dest, size) and copies the data in parallel.
`results` is the list where results will be stored.
Returns versioned URLs for S3 destinations and regular file URLs for files.
"""
if not file_list:
return []
logger.debug('copy files: started')
assert len(file_list) == len(results)
total_size = sum(size for (_, _, size), result in zip(file_list, results) if result is None)
lock = Lock()
futures = deque()
future_to_idx = {}
idx_to_futures = defaultdict(list)
stopped = False
s3_client_provider = S3ClientProvider() # Share provider across threads to reduce redundant public bucket checks
with tqdm(desc=message, total=total_size, unit='B', unit_scale=True, disable=DISABLE_TQDM) as progress, \
ThreadPoolExecutor(MAX_CONCURRENCY) as executor:
def progress_callback(bytes_transferred):
if stopped:
raise Exception("Interrupted")
with lock:
progress.update(bytes_transferred)
def run_task(idx, func, *args):
future = executor.submit(func, *args)
with lock:
futures.append(future)
future_to_idx[future] = idx
idx_to_futures[idx].append(future)
def worker(idx, src, dest, size):
if stopped:
raise Exception("Interrupted")
def done_callback(value, checksum):
assert value is not None
with lock:
assert results[idx] is None
results[idx] = (value, checksum)
if callback is not None:
callback(src, dest, size)
ctx = WorkerContext(s3_client_provider=s3_client_provider,
progress=progress_callback,
done=done_callback,
run=functools.partial(run_task, idx))
if dest.version_id:
raise ValueError("Cannot set VersionId on destination")
if src.is_local():
if dest.is_local():
_copy_local_file(ctx, size, src.path, dest.path)
else:
if dest.version_id:
raise ValueError("Cannot set VersionId on destination")
_upload_or_reuse_file(ctx, size, src.path, dest.bucket, dest.path)
else:
if dest.is_local():
_download_file(ctx, size, src.bucket, src.path, src.version_id, dest.path)
else:
_copy_remote_file(ctx, size, src.bucket, src.path, src.version_id,
dest.bucket, dest.path)
try:
for idx, (args, result) in enumerate(zip(file_list, results)):
if result is not None:
continue
run_task(idx, worker, idx, *args)
# ThreadPoolExecutor does not appear to have a way to just wait for everything to complete.
# Shutting it down will cause it to wait - but will prevent any new tasks from starting.
# So, manually wait for all tasks to complete.
# This will also raise any exception that happened in a worker thread.
while True:
with lock:
if not futures:
break
future = futures.popleft()
if future.cancelled():
continue
try:
future.result()
except exceptions_to_ignore:
with lock:
idx = future_to_idx[future]
futures_to_cancel = idx_to_futures[idx]
for f in futures_to_cancel:
f.cancel()
futures_to_cancel.clear()
finally:
# Make sure all tasks exit quickly if the main thread exits before they're done.
stopped = True
logger.debug('copy files: finished')
return results
def _calculate_etag(file_path):
"""
Attempts to calculate a local file's ETag the way S3 does:
- Normal uploads: MD5 of the file
- Multi-part uploads: MD5 of the (binary) MD5s of the parts, dash, number of parts
We can't know how the file was actually uploaded - but we're assuming it was done using
the default settings, which we get from `s3_transfer_config`.
"""
size = pathlib.Path(file_path).stat().st_size
with open(file_path, 'rb') as fd:
if not is_mpu(size):
contents = fd.read()
etag = hashlib.md5(contents).hexdigest()
else:
chunksize = get_checksum_chunksize(size)
hashes = []
for contents in read_file_chunks(fd, chunksize):
hashes.append(hashlib.md5(contents).digest())
etag = '%s-%d' % (hashlib.md5(b''.join(hashes)).hexdigest(), len(hashes))
return '"%s"' % etag
def delete_object(bucket, key):
s3_client = S3ClientProvider().get_standard_client(bucke)
s3_client.head_object(Bucket=bucket, Key=key) # Make sure it exists
s3_client.delete_object(Bucket=bucket, Key=key) # Actually delete it
def list_object_versions(bucket, prefix, recursive=True):
if prefix and not prefix.endswith('/'):
raise ValueError("Prefix must end with /")
list_obj_params = dict(
Bucket=bucket,
Prefix=prefix
)
if not recursive:
# Treat '/' as a directory separator and only return one level of files instead of everything.
list_obj_params.update(Delimiter='/')
# TODO: make this a generator?
versions = []
delete_markers = []
prefixes = []
s3_client = S3ClientProvider().find_correct_client(S3Api.LIST_OBJECT_VERSIONS, bucket, list_obj_params)
paginator = s3_client.get_paginator('list_object_versions')
for response in paginator.paginate(**list_obj_params):
versions += response.get('Versions', [])
delete_markers += response.get('DeleteMarkers', [])
prefixes += response.get('CommonPrefixes', [])
if recursive:
return versions, delete_markers
else:
return prefixes, versions, delete_markers
def list_objects(bucket, prefix, recursive=True):
if prefix and not prefix.endswith('/'):
raise ValueError("Prefix must end with /")
objects = []
prefixes = []
list_obj_params = dict(Bucket=bucket,
Prefix=prefix)
if not recursive:
# Treat '/' as a directory separator and only return one level of files instead of everything.
list_obj_params.update(Delimiter='/')
s3_client = S3ClientProvider().find_correct_client(S3Api.LIST_OBJECTS_V2, bucket, list_obj_params)
paginator = s3_client.get_paginator('list_objects_v2')
for response in paginator.paginate(**list_obj_params):
objects += response.get('Contents', [])
prefixes += response.get('CommonPrefixes', [])
if recursive:
return objects
else:
return prefixes, objects
def _looks_like_dir(pk: PhysicalKey):
return pk.basename() == ''
def list_url(src: PhysicalKey):
if src.is_local():
src_file = pathlib.Path(src.path)
for f in src_file.rglob('*'):
try:
if f.is_file():
size = f.stat().st_size
yield f.relative_to(src_file).as_posix(), size
except FileNotFoundError:
# If a file does not exist, is it really a file?
pass
else:
if src.version_id is not None:
raise ValueError(f"Directories cannot have version IDs: {src}")
src_path = src.path
if not _looks_like_dir(src):
src_path += '/'
list_obj_params = dict(Bucket=src.bucket, Prefix=src_path)
s3_client = S3ClientProvider().find_correct_client(S3Api.LIST_OBJECTS_V2, src.bucket, list_obj_params)
paginator = s3_client.get_paginator('list_objects_v2')
for response in paginator.paginate(**list_obj_params):
for obj in response.get('Contents', []):
key = obj['Key']
if not key.startswith(src_path):
raise ValueError("Unexpected key: %r" % key)
yield key[len(src_path):], obj['Size']
def delete_url(src: PhysicalKey):
"""Deletes the given URL.
Follows S3 semantics even for local files:
- If the URL does not exist, it's a no-op.
- If it's a non-empty directory, it's also a no-op.
"""
if src.is_local():
src_file = pathlib.Path(src.path)
try:
if src_file.is_dir():
try:
src_file.rmdir()
except OSError:
# Ignore non-empty directories, for consistency with S3
pass
else:
src_file.unlink()
except FileNotFoundError:
pass
else:
s3_client = S3ClientProvider().get_standard_client(src.bucket)
s3_client.delete_object(Bucket=src.bucket, Key=src.path)
def copy_file_list(file_list, message=None, callback=None):
"""
Takes a list of tuples (src, dest, size) and copies them in parallel.
URLs must be regular files, not directories.
Returns versioned URLs for S3 destinations and regular file URLs for files.
"""
for src, dest, _ in file_list:
if _looks_like_dir(src) or _looks_like_dir(dest):
raise ValueError("Directories are not allowed")
return _copy_file_list_internal(file_list, [None] * len(file_list), message, callback)
def copy_file(src: PhysicalKey, dest: PhysicalKey, size=None, message=None, callback=None):
"""
Copies a single file or directory.
If src is a file, dest can be a file or a directory.
If src is a directory, dest must be a directory.
"""
def sanity_check(rel_path):
for part in rel_path.split('/'):
if part in ('', '.', '..'):
raise ValueError("Invalid relative path: %r" % rel_path)
url_list = []
if _looks_like_dir(src):
if not _looks_like_dir(dest):
raise ValueError("Destination path must end in /")
if size is not None:
raise ValueError("`size` does not make sense for directories")
for rel_path, size in list_url(src):
sanity_check(rel_path)
url_list.append((src.join(rel_path), dest.join(rel_path), size))
if not url_list:
raise QuiltException("No objects to download.")
else:
if _looks_like_dir(dest):
dest = dest.join(src.basename())
if size is None:
size, version_id = get_size_and_version(src)
if src.version_id is None:
src = PhysicalKey(src.bucket, src.path, version_id)
url_list.append((src, dest, size))
_copy_file_list_internal(url_list, [None] * len(url_list), message, callback)
def put_bytes(data: bytes, dest: PhysicalKey):
if _looks_like_dir(dest):
raise ValueError("Invalid path: %r" % dest.path)
if dest.is_local():
dest_file = pathlib.Path(dest.path)
dest_file.parent.mkdir(parents=True, exist_ok=True)
dest_file.write_bytes(data)
else:
if dest.version_id is not None:
raise ValueError("Cannot set VersionId on destination")
s3_client = S3ClientProvider().get_standard_client(dest.bucket)
s3_client.put_object(
Bucket=dest.bucket,
Key=dest.path,
Body=data,
)
def _local_get_bytes(pk: PhysicalKey):
return pathlib.Path(pk.path).read_bytes()
def _s3_query_object(pk: PhysicalKey, *, head=False):
params = dict(Bucket=pk.bucket, Key=pk.path)
if pk.version_id is not None:
params.update(VersionId=pk.version_id)
s3_client = S3ClientProvider().find_correct_client(
S3Api.HEAD_OBJECT if head else S3Api.GET_OBJECT, pk.bucket, params)
return (s3_client.head_object if head else s3_client.get_object)(**params)
def get_bytes(src: PhysicalKey):
if src.is_local():
return _local_get_bytes(src)
return _s3_query_object(src)['Body'].read()
def get_bytes_and_effective_pk(src: PhysicalKey) -> Tuple[bytes, PhysicalKey]:
if src.is_local():
return _local_get_bytes(src), src
resp = _s3_query_object(src)
return resp['Body'].read(), PhysicalKey(src.bucket, src.path, resp.get('VersionId'))
def get_size_and_version(src: PhysicalKey):
"""
Gets size and version for the object at a given URL.
Returns:
size, version(str)
"""
if _looks_like_dir(src):
raise QuiltException("Invalid path: %r; cannot be a directory" % src.path)
version = None
if src.is_local():
src_file = pathlib.Path(src.path)
if not src_file.is_file():
raise QuiltException("Not a file: %r" % str(src_file))
size = src_file.stat().st_size
else:
resp = _s3_query_object(src, head=True)
size = resp['ContentLength']
version = resp.get('VersionId')
return size, version
def calculate_checksum(src_list: List[PhysicalKey], sizes: List[int]) -> List[bytes]:
assert len(src_list) == len(sizes)
if not src_list:
return []