"""Docstring.""" # AWS CLI sync is very fast when copying single dir # this is a PoC that applies to many files not in the same subdir import asyncio import hashlib import sys import time import aioboto3 async def main(): """Entrypoint.""" start = time.perf_counter() bucket = 'dev-pdumoulin' prefix = sys.argv[1] session = aioboto3.Session() async with session.client('s3') as client: keys = await list_bucket(client, bucket, prefix) results = await asyncio.gather( *[ download(client, bucket, key) for key in keys ] ) for result in results: print(result) print(f'Total time: {time.perf_counter() - start}') async def list_bucket(client, bucket, prefix): """S3 list all objects.""" keys = set() paginator = client.get_paginator('list_objects') async for page in paginator.paginate(Bucket=bucket, Prefix=prefix): for content in page['Contents']: key = content['Key'] if not key.endswith('/'): keys.add(key) return keys async def download(client, bucket, path): """S3 download operation.""" chunk_size = 1024 * 64 start = time.perf_counter() print('Downloading: ', bucket, path) md5_hash = hashlib.md5() file_data = await client.get_object(Bucket=bucket, Key=path) async for chunk in file_data['Body'].iter_chunks(chunk_size=chunk_size): md5_hash.update(chunk) print('Finished: ', bucket, path, f'{time.perf_counter() - start}') return (path, md5_hash.hexdigest(), file_data['ContentLength']) if __name__ == '__main__': asyncio.run(main())