-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpt5_s3_tool.py
More file actions
executable file
·1331 lines (1145 loc) · 43 KB
/
Copy pathpt5_s3_tool.py
File metadata and controls
executable file
·1331 lines (1145 loc) · 43 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
#!/usr/bin/env python3
"""
PT5 S3 Tool - A tool for copying files between local and S3 locations.
This script is specifically designed for the GCOOS ORION project to manage and
transfer IFCB (Imaging FlowCytobot) data files to/from Amazon S3. IFCB is an
automated submersible flow cytometer that provides continuous, high-resolution
measurements of phytoplankton and microzooplankton abundance and composition.
Features:
- AWS credentials validation
- Support for IFCB data file transfers
- Recursive directory operations
- Colorized console output
- Concurrent file transfers (up to 32 workers)
- Connection pool optimization (100 connections)
- Automatic retry on failures (3 attempts)
- Batched file submission (1000 files per batch)
- Pre-computed S3 keys for improved performance
- Overall progress tracking with tqdm
- Detailed summary report with:
* Total files processed
* Total data transferred
* Transfer duration
* Average transfer rate
* Files processed per second
- Dry-run mode for testing
- Environment variable configuration (automatically used when no args provided)
- Detailed logging
- File filtering options
- Fast bulk deletion of S3 objects
- Simplified S3 URI handling
Environment Variables:
- IFCB_DATA_DIR: Default source directory for uploads
- AWS_UPLOAD_URL: Default S3 destination (s3://bucket/prefix)
Note: Environment variables are only used as defaults when no command-line
arguments are provided. Command-line arguments always take precedence.
Usage Examples:
# Download from S3 to local
python pt5_s3_tool.py --source s3://bucket-name/prefix \
--destination /local/path
# Upload from local to S3
python pt5_s3_tool.py --source /path/to/files \
--destination s3://bucket-name/prefix
# Copy with recursive option
python pt5_s3_tool.py --source s3://bucket-name/prefix \
--destination /local/path --recursive
# Copy with file filter
python pt5_s3_tool.py --source s3://bucket-name/prefix \
--destination /local/path --filter '*.jpg'
# Dry run to preview changes
python pt5_s3_tool.py --source s3://bucket-name/prefix \
--destination /local/path --dry-run
# Delete files from S3 (using --source)
python pt5_s3_tool.py --source s3://bucket-name/prefix \
--delete [--recursive] [--filter '*.jpg']
# Delete files from S3 (using --destination)
python pt5_s3_tool.py --destination s3://bucket-name/prefix \
--delete [--recursive] [--filter '*.jpg']
# Dry run delete
python pt5_s3_tool.py --source s3://bucket-name/prefix \
--delete --dry-run
# Using environment variables (no arguments needed)
python pt5_s3_tool.py
System Requirements:
- Python 3.6 or higher
- Sufficient system resources for concurrent processing
- Recommended: 4+ CPU cores and 8GB+ RAM for large file sets
Author: robertdcurrier@tamu.edu
"""
import argparse
import logging
import os
import sys
import time
from typing import Optional, List, Tuple, Dict
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor, as_completed, Future
import boto3
from botocore.exceptions import ClientError, NoCredentialsError
from botocore.config import Config
from colorama import Fore, Style, init
from tqdm import tqdm
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Initialize colorama for cross-platform colored output
init(autoreset=True)
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger(__name__)
# Suppress boto3's INFO messages about finding credentials
logging.getLogger('boto3.credentials').setLevel(logging.WARNING)
logging.getLogger('botocore.credentials').setLevel(logging.WARNING)
def validate_aws_credentials() -> bool:
"""Validate AWS credentials by making a simple API call."""
try:
session = boto3.Session()
s3 = session.client('s3')
s3.list_buckets()
logger.info(f"{Fore.GREEN}AWS credentials validated{Style.RESET_ALL}")
return True
except Exception as e:
logger.error(
f"{Fore.RED}Error: AWS credentials validation failed: "
f"{str(e)}{Style.RESET_ALL}"
)
return False
def get_default_source() -> Optional[str]:
"""Get the default source directory from environment variables."""
ifcb_dir = os.getenv('IFCB_DATA_DIR')
if ifcb_dir and os.path.exists(ifcb_dir):
return ifcb_dir
return None
def get_default_bucket() -> Optional[str]:
"""Get the default bucket from AWS_UPLOAD_URL environment variable."""
upload_url = os.getenv('AWS_UPLOAD_URL', '')
if upload_url.startswith('s3://'):
# Extract bucket from s3://bucket/path format
parts = upload_url[5:].split('/')
return parts[0]
return None
def get_default_prefix() -> str:
"""Get the default prefix from AWS_UPLOAD_URL environment variable."""
upload_url = os.getenv('AWS_UPLOAD_URL', '')
if upload_url.startswith('s3://'):
# Extract path after bucket
parts = upload_url[5:].split('/')
if len(parts) > 1:
return '/'.join(parts[1:])
return ''
def setup_argparse() -> argparse.ArgumentParser:
"""Set up and return the argument parser with all required options."""
default_source = get_default_source()
default_bucket = get_default_bucket()
default_prefix = get_default_prefix()
# Construct default destination if both bucket and prefix are available
default_destination = None
if default_bucket:
default_destination = f"s3://{default_bucket}"
if default_prefix:
default_destination += f"/{default_prefix}"
parser = argparse.ArgumentParser(
description='Copy files between local and S3 locations'
)
# Source/destination arguments
parser.add_argument(
'--source',
help='Source location (local path or s3://bucket/prefix)',
default=default_source
)
parser.add_argument(
'--destination',
help='Destination location (local path or s3://bucket/prefix)',
default=default_destination
)
# Common options
parser.add_argument(
'--recursive',
action='store_true',
help='Process directories recursively'
)
parser.add_argument(
'--dry-run',
action='store_true',
help='Show what would be transferred without actually transferring'
)
parser.add_argument(
'--verbose',
action='store_true',
help='Enable verbose logging'
)
parser.add_argument(
'--validate',
action='store_true',
help='Only validate AWS credentials and exit'
)
# Download specific options
parser.add_argument(
'--overwrite',
action='store_true',
help='Overwrite existing files when downloading'
)
parser.add_argument(
'--filter',
help='Filter pattern for files to process (e.g., "*.png")'
)
# Delete option
parser.add_argument(
'--delete',
action='store_true',
help='Delete files at the S3 destination (only works with S3 URLs)'
)
return parser
def validate_args(args: argparse.Namespace) -> bool:
"""Validate command line arguments based on operation mode."""
# Parse source if it's in s3:// format
if args.source and args.source.startswith('s3://'):
# Extract bucket and prefix from s3://bucket/prefix format
s3_path = args.source[5:] # Remove 's3://'
parts = s3_path.split('/', 1) # Split at first '/'
# Set bucket and prefix from source
args.bucket = parts[0]
args.prefix = parts[1] if len(parts) > 1 else ''
logger.info(
f"{Fore.CYAN}Using source: {args.source} "
f"(bucket: {args.bucket}, prefix: {args.prefix}){Style.RESET_ALL}"
)
# Parse destination if it's in s3:// format
elif args.destination and args.destination.startswith('s3://'):
# Extract bucket and prefix from s3://bucket/prefix format
s3_path = args.destination[5:] # Remove 's3://'
parts = s3_path.split('/', 1) # Split at first '/'
# Set bucket and prefix from destination
args.bucket = parts[0]
args.prefix = parts[1] if len(parts) > 1 else ''
logger.info(
f"{Fore.CYAN}Using destination: {args.destination} "
f"(bucket: {args.bucket}, prefix: {args.prefix}){Style.RESET_ALL}"
)
else:
logger.error(
f"{Fore.RED}Error: Either source or destination must be an S3 URL "
f"(s3://bucket/prefix){Style.RESET_ALL}"
)
return False
# Common validation for all operations
if not args.bucket:
logger.error(
f"{Fore.RED}Error: S3 bucket not specified. "
f"Use s3://bucket/prefix format for S3 locations{Style.RESET_ALL}"
)
return False
# Validate local paths
if args.source and not args.source.startswith('s3://'):
if not os.path.exists(args.source):
logger.error(
f"{Fore.RED}Error: Source path does not exist: "
f"{args.source}{Style.RESET_ALL}"
)
return False
if args.destination and not args.destination.startswith('s3://'):
try:
os.makedirs(args.destination, exist_ok=True)
logger.info(
f"{Fore.CYAN}Created destination directory: "
f"{args.destination}{Style.RESET_ALL}"
)
except Exception as e:
logger.error(
f"{Fore.RED}Error creating destination directory: "
f"{args.destination} - {str(e)}{Style.RESET_ALL}"
)
return False
return True
def get_files_to_upload(
source: str,
recursive: bool = False
) -> List[Tuple[str, str]]:
"""
Get list of files to upload and their S3 keys.
Args:
source: Source file or directory path
recursive: Whether to include subdirectories
Returns:
List of tuples containing (local_path, s3_key)
"""
source_path = Path(source)
if not source_path.exists():
raise FileNotFoundError(f"Source path does not exist: {source}")
if source_path.is_file():
return [(str(source_path), source_path.name)]
else:
files = []
pattern = "**/*" if recursive else "*"
for file_path in source_path.glob(pattern):
if file_path.is_file():
# Get relative path for S3 key
rel_path = file_path.relative_to(source_path)
files.append((str(file_path), str(rel_path)))
return files
def upload_file(s3_client: boto3.client, local_path: str, bucket: str,
s3_key: str, dry_run: bool = False) -> bool:
"""
Upload a single file to S3.
Args:
s3_client: Boto3 S3 client
local_path: Local file path
bucket: S3 bucket name
s3_key: S3 key (path in bucket)
dry_run: Whether to simulate upload
Returns:
bool: True if upload successful, False otherwise
"""
try:
if dry_run:
logger.info(f"{Fore.CYAN}Would upload: {local_path} -> "
f"s3://{bucket}/{s3_key}{Style.RESET_ALL}")
return True
logger.debug(
f"{Fore.CYAN}Starting upload of {local_path}{Style.RESET_ALL}"
)
with open(local_path, 'rb') as f:
s3_client.upload_fileobj(f, bucket, s3_key)
logger.debug(
f"{Fore.GREEN}Completed upload of {local_path}{Style.RESET_ALL}"
)
return True
except Exception as e:
logger.error(f"{Fore.RED}Error uploading {local_path}: {str(e)}"
f"{Style.RESET_ALL}")
return False
def format_size(size_bytes: int) -> str:
"""Format size in bytes to human readable string."""
for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
if size_bytes < 1024.0:
return f"{size_bytes:.2f} {unit}"
size_bytes /= 1024.0
return f"{size_bytes:.2f} PB"
def print_summary_report(
total_files: int,
total_size: int,
start_time: float,
operation: str = "transfer"
) -> None:
"""
Print a summary report of the file transfer process.
Args:
total_files: Total number of files processed
total_size: Total size of files in bytes
start_time: Start time of process
operation: Operation type (upload, download, etc.)
"""
end_time = time.time()
duration = end_time - start_time
avg_rate = total_size / duration if duration > 0 else 0
files_per_second = total_files / duration if duration > 0 else 0
logger.info(
f"\n{Fore.GREEN}{operation.capitalize()} Summary "
f"Report:{Style.RESET_ALL}"
)
logger.info(f"{Fore.CYAN}Total Files:{Style.RESET_ALL} {total_files}")
logger.info(
f"{Fore.CYAN}Total Size:{Style.RESET_ALL} {format_size(total_size)}"
)
logger.info(
f"{Fore.CYAN}Duration:{Style.RESET_ALL} {duration:.2f} seconds"
)
logger.info(
f"{Fore.CYAN}Average Rate:{Style.RESET_ALL} {format_size(avg_rate)}/s"
)
logger.info(
f"{Fore.CYAN}Files/Second:{Style.RESET_ALL} {files_per_second:.2f}"
)
def configure_s3_client() -> boto3.client:
"""Configure and return an S3 client with optimized settings."""
session = boto3.Session()
config = Config(
max_pool_connections=100, # Increase connection pool size
retries={'max_attempts': 3}, # Add retry configuration
connect_timeout=5, # Connection timeout in seconds
read_timeout=60, # Read timeout in seconds
tcp_keepalive=True # Enable TCP keepalive
)
return session.client('s3', config=config)
def prepare_upload_tasks(
files: List[Tuple[str, str]],
prefix: str
) -> List[Tuple[str, str]]:
"""Prepare upload tasks by combining prefix with S3 keys."""
upload_tasks = []
for local_path, s3_key in files:
full_s3_key = os.path.join(prefix, s3_key).replace('\\', '/')
upload_tasks.append((local_path, full_s3_key))
return upload_tasks
def process_dry_run_upload(
files: List[Tuple[str, str]],
args: argparse.Namespace
) -> bool:
"""Process dry run for upload operation."""
total_size = sum(os.path.getsize(local_path) for local_path, _ in files)
logger.info(
f"{Fore.CYAN}Dry run - would upload {len(files)} files "
f"({format_size(total_size)}) from {args.source} "
f"to s3://{args.bucket}/{args.prefix}{Style.RESET_ALL}"
)
return True
def submit_upload_tasks(
executor: ThreadPoolExecutor,
upload_tasks: List[Tuple[str, str]],
s3_client: boto3.client,
args: argparse.Namespace,
total_files: int
) -> Dict[Future, str]:
"""Submit upload tasks to the executor and return futures mapping."""
future_to_file = {}
# Show progress during file submission
with tqdm(
total=total_files,
desc="Submitting files",
unit="file"
) as submit_pbar:
# Submit files in batches of 1000
batch_size = 1000
for i in range(0, len(upload_tasks), batch_size):
batch = upload_tasks[i:i + batch_size]
# Submit batch of files
futures = [
executor.submit(
upload_file, s3_client, local_path, args.bucket,
s3_key, args.dry_run
) for local_path, s3_key in batch
]
# Map futures to file paths
for future, (local_path, _) in zip(futures, batch):
future_to_file[future] = local_path
submit_pbar.update(len(batch))
return future_to_file
def process_upload_results(
future_to_file: Dict[Future, str],
total_files: int
) -> Tuple[bool, int]:
"""Process upload results and return success status and total size."""
success = True
total_size = 0
# Create progress bar for upload completion
with tqdm(
total=total_files,
desc="Uploading files",
unit="file"
) as pbar:
for future in as_completed(future_to_file):
file_path = future_to_file[future]
try:
if not future.result():
success = False
# Add file size to total
file_size = os.path.getsize(file_path)
total_size += file_size
pbar.update(1) # Update progress after each file
logger.debug(
f"{Fore.GREEN}Successfully uploaded {file_path}"
f"{Style.RESET_ALL}"
)
except Exception as e:
logger.error(
f"{Fore.RED}Error uploading {file_path}: {str(e)}"
f"{Style.RESET_ALL}"
)
success = False
pbar.update(1) # Update progress even on error
return success, total_size
def upload_files(
args: argparse.Namespace
) -> bool:
"""
Upload files to S3 based on command line arguments.
Args:
args: Parsed command line arguments
Returns:
bool: True if all uploads successful, False otherwise
"""
try:
# Configure S3 client
s3_client = configure_s3_client()
# Get files to upload
files = get_files_to_upload(args.source, args.recursive)
if not files:
logger.warning(
f"{Fore.YELLOW}No files found to upload{Style.RESET_ALL}"
)
return True
# Handle dry run
if args.dry_run:
return process_dry_run_upload(files, args)
# Log initial file count
total_files = len(files)
logger.info(
f"{Fore.CYAN}Found {total_files} files to upload{Style.RESET_ALL}"
)
# Set up concurrent execution
max_workers = min(32, total_files) # Limit to 32 concurrent uploads
logger.info(
f"{Fore.CYAN}Using {max_workers} concurrent workers"
f"{Style.RESET_ALL}"
)
# Initialize statistics and prepare tasks
start_time = time.time()
upload_tasks = prepare_upload_tasks(files, args.prefix)
logger.info(
f"{Fore.CYAN}Starting ThreadPoolExecutor with {max_workers} "
f"workers{Style.RESET_ALL}"
)
# Execute uploads concurrently
with ThreadPoolExecutor(max_workers=max_workers) as executor:
# Submit tasks
future_to_file = submit_upload_tasks(
executor, upload_tasks, s3_client, args, total_files
)
logger.info(
f"{Fore.CYAN}All uploads submitted, waiting for completion"
f"{Style.RESET_ALL}"
)
# Process results
success, total_size = process_upload_results(
future_to_file, total_files
)
# Print summary report
print_summary_report(total_files, total_size, start_time, "upload")
return success
except Exception as e:
logger.error(
f"{Fore.RED}Error during upload process: {str(e)}"
f"{Style.RESET_ALL}"
)
return False
def list_s3_objects(
s3_client: boto3.client,
bucket: str,
prefix: str,
recursive: bool = False,
filter_pattern: str = None
) -> List[dict]:
"""
List objects in an S3 bucket with the given prefix.
Args:
s3_client: Boto3 S3 client
bucket: S3 bucket name
prefix: S3 key prefix
recursive: Whether to list objects recursively
filter_pattern: Optional pattern to filter objects
Returns:
List of S3 object dictionaries
"""
try:
# If not recursive and prefix doesn't end with '/', add it
if not recursive and prefix and not prefix.endswith('/'):
prefix = prefix + '/'
# List objects in the bucket with the given prefix
paginator = s3_client.get_paginator('list_objects_v2')
page_iterator = paginator.paginate(
Bucket=bucket,
Prefix=prefix,
Delimiter='' if recursive else '/'
)
objects = []
for page in page_iterator:
# Add objects
if 'Contents' in page:
for obj in page['Contents']:
# Skip "directory" objects (keys ending with '/')
if obj['Key'].endswith('/'):
continue
# Apply filter if specified
if filter_pattern:
import fnmatch
filename = os.path.basename(obj['Key'])
if not fnmatch.fnmatch(filename, filter_pattern):
continue
objects.append(obj)
# Add common prefixes (directories) if not recursive
if not recursive and 'CommonPrefixes' in page:
for prefix_obj in page['CommonPrefixes']:
logger.debug(
f"{Fore.CYAN}Found directory: "
f"{prefix_obj['Prefix']}{Style.RESET_ALL}"
)
return objects
except Exception as e:
logger.error(
f"{Fore.RED}Error listing objects in bucket {bucket}: "
f"{str(e)}{Style.RESET_ALL}"
)
return []
def parse_s3_source(source: str) -> Tuple[str, str]:
"""Parse an S3 URI into bucket and prefix components."""
if not source.startswith('s3://'):
return None, None
# Extract bucket and prefix from s3://bucket/prefix format
s3_path = source[5:] # Remove 's3://'
parts = s3_path.split('/', 1) # Split at first '/'
# Set bucket and prefix from source
bucket = parts[0]
prefix = parts[1] if len(parts) > 1 else ''
# Remove trailing slash from prefix if present
if prefix.endswith('/'):
prefix = prefix[:-1]
return bucket, prefix
def prepare_download_tasks(
objects: List[dict],
prefix: str,
destination: str
) -> List[Tuple[str, str, int]]:
"""Prepare download tasks by mapping S3 keys to local paths."""
download_tasks = []
for obj in objects:
s3_key = obj['Key']
# Remove prefix from key to create relative path
rel_path = s3_key
if prefix and s3_key.startswith(prefix):
rel_path = s3_key[len(prefix):]
if rel_path.startswith('/'):
rel_path = rel_path[1:]
local_path = os.path.join(destination, rel_path)
download_tasks.append((s3_key, local_path, obj['Size']))
return download_tasks
def process_dry_run_download(
objects: List[dict],
prefix: str,
args: argparse.Namespace
) -> bool:
"""Process dry run for download operation."""
total_size = sum(obj['Size'] for obj in objects)
logger.info(
f"{Fore.CYAN}Dry run - would download {len(objects)} files "
f"({format_size(total_size)}) from s3://{args.bucket}/{args.prefix} "
f"to {args.destination}{Style.RESET_ALL}"
)
return True
def download_file(
s3_client: boto3.client,
bucket: str,
s3_key: str,
local_path: str,
dry_run: bool = False,
overwrite: bool = False
) -> bool:
"""
Download a single file from S3.
Args:
s3_client: Boto3 S3 client
bucket: S3 bucket name
s3_key: S3 key (path in bucket)
local_path: Local file path
dry_run: Whether to simulate download
overwrite: Whether to overwrite existing files
Returns:
bool: True if download successful, False otherwise
"""
try:
if os.path.exists(local_path) and not overwrite:
logger.debug(
f"{Fore.YELLOW}Skipping existing file: {local_path}"
f"{Style.RESET_ALL}"
)
return True
if dry_run:
logger.info(
f"{Fore.CYAN}Would download: s3://{bucket}/{s3_key} -> "
f"{local_path}{Style.RESET_ALL}"
)
return True
# Create directory if it doesn't exist
os.makedirs(
os.path.dirname(os.path.abspath(local_path)),
exist_ok=True
)
logger.debug(
f"{Fore.CYAN}Starting download of {s3_key}{Style.RESET_ALL}"
)
s3_client.download_file(bucket, s3_key, local_path)
logger.debug(
f"{Fore.GREEN}Completed download of {s3_key}{Style.RESET_ALL}"
)
return True
except Exception as e:
logger.error(
f"{Fore.RED}Error downloading {s3_key}: {str(e)}"
f"{Style.RESET_ALL}"
)
return False
def submit_download_tasks(
executor: ThreadPoolExecutor,
download_tasks: List[Tuple[str, str, int]],
s3_client: boto3.client,
args: argparse.Namespace,
total_files: int
) -> Dict[Future, Tuple[str, str, int]]:
"""Submit download tasks to the executor and return futures mapping."""
future_to_file = {}
# Show progress during file submission
with tqdm(
total=total_files,
desc="Submitting files",
unit="file"
) as submit_pbar:
# Submit files in batches of 1000
batch_size = 1000
for i in range(0, len(download_tasks), batch_size):
batch = download_tasks[i:i + batch_size]
# Submit batch of files
futures = [
executor.submit(
download_file,
s3_client,
args.bucket,
s3_key,
local_path,
args.dry_run,
args.overwrite
) for s3_key, local_path, size in batch
]
# Map futures to file info
for future, (s3_key, local_path, size) in zip(
futures, batch
):
future_to_file[future] = (s3_key, local_path, size)
submit_pbar.update(len(batch))
return future_to_file
def process_download_results(
future_to_file: Dict[Future, Tuple[str, str, int]],
total_files: int
) -> Tuple[bool, int]:
"""Process download results and return success status and total size."""
success = True
total_size = 0
# Create progress bar for download completion
with tqdm(
total=total_files,
desc="Downloading files",
unit="file"
) as pbar:
for future in as_completed(future_to_file):
s3_key, local_path, size = future_to_file[future]
try:
if not future.result():
success = False
# Add file size to total
total_size += size
pbar.update(1) # Update progress after each file
logger.debug(
f"{Fore.GREEN}Successfully downloaded {s3_key}"
f"{Style.RESET_ALL}"
)
except Exception as e:
logger.error(
f"{Fore.RED}Error downloading {s3_key}: {str(e)}"
f"{Style.RESET_ALL}"
)
success = False
pbar.update(1) # Update progress even on error
return success, total_size
def download_files(
args: argparse.Namespace
) -> bool:
"""
Download files from S3 based on command line arguments.
Args:
args: Parsed command line arguments
Returns:
bool: True if all downloads successful, False otherwise
"""
try:
# Parse source if it's in s3:// format
if args.source and args.source.startswith('s3://'):
args.bucket, args.prefix = parse_s3_source(args.source)
logger.info(
f"{Fore.CYAN}Using source: {args.source} "
f"(bucket: {args.bucket}, prefix: {args.prefix})"
f"{Style.RESET_ALL}"
)
# Configure S3 client
s3_client = configure_s3_client()
# List objects to download
logger.info(
f"{Fore.CYAN}Listing objects in bucket {args.bucket} "
f"with prefix {args.prefix}{Style.RESET_ALL}"
)
objects = list_s3_objects(
s3_client,
args.bucket,
args.prefix, # Use the parsed prefix, not the full S3 URL
args.recursive,
args.filter
)
if not objects:
logger.warning(
f"{Fore.YELLOW}No files found to download{Style.RESET_ALL}"
)
return True
# Log initial file count
total_files = len(objects)
logger.info(
f"{Fore.CYAN}Found {total_files} files to download"
f"{Style.RESET_ALL}"
)
# Handle dry run
if args.dry_run:
return process_dry_run_download(objects, args.prefix, args)
# Set up concurrent execution
max_workers = min(32, total_files) # Limit to 32 concurrent downloads
logger.info(
f"{Fore.CYAN}Using {max_workers} concurrent workers"
f"{Style.RESET_ALL}"
)
# Initialize statistics and prepare tasks
start_time = time.time()
download_tasks = prepare_download_tasks(
objects, args.prefix, args.destination
)
logger.info(
f"{Fore.CYAN}Starting ThreadPoolExecutor with {max_workers} "
f"workers{Style.RESET_ALL}"
)
# Execute downloads concurrently
with ThreadPoolExecutor(max_workers=max_workers) as executor:
# Submit tasks
future_to_file = submit_download_tasks(
executor, download_tasks, s3_client, args, total_files
)
logger.info(
f"{Fore.CYAN}All downloads submitted, waiting for completion"
f"{Style.RESET_ALL}"
)
# Process results
success, total_size = process_download_results(
future_to_file, total_files
)
# Print summary report
print_summary_report(total_files, total_size, start_time, "download")
return success
except Exception as e:
logger.error(
f"{Fore.RED}Error during download process: {str(e)}"
f"{Style.RESET_ALL}"
)
return False
def list_bucket_contents(
args: argparse.Namespace
) -> bool:
"""
List contents of an S3 bucket based on command line arguments.
Args:
args: Parsed command line arguments
Returns:
bool: True if listing successful, False otherwise
"""
try:
# Parse source if it's in s3:// format
if args.source and args.source.startswith('s3://'):
# Extract bucket and prefix from s3://bucket/prefix format
s3_path = args.source[5:] # Remove 's3://'
parts = s3_path.split('/', 1) # Split at first '/'
# Set bucket and prefix from source
args.bucket = parts[0]
args.prefix = parts[1] if len(parts) > 1 else ''
logger.info(
f"{Fore.CYAN}Using source: {args.source} "
f"(bucket: {args.bucket}, prefix: {args.prefix})"
f"{Style.RESET_ALL}"
)
# Configure S3 client
session = boto3.Session()
s3_client = session.client('s3')
# Use source as prefix if provided, otherwise use prefix
prefix = args.source if args.source else args.prefix
logger.info(
f"{Fore.CYAN}Listing objects in bucket {args.bucket} "
f"with prefix {prefix}{Style.RESET_ALL}"
)
objects = list_s3_objects(