|
| 1 | +""" Verify Etags |
| 2 | +
|
| 3 | +This is just a little helper script to upload files to S3 using both single part |
| 4 | +and multipart uploads, and then fetch the Etag from S3 to verify that our etag |
| 5 | +calculation matches what S3 produces. |
| 6 | +
|
| 7 | +These values go into the test_etags.py unit tests to verify correctness of our etag calculations. |
| 8 | +
|
| 9 | +There is no reason to run this script other than to regenerate those values if the etag calculation |
| 10 | +algorithm changes. |
| 11 | +
|
| 12 | +""" |
| 13 | +import os |
| 14 | + |
| 15 | +import boto3 |
| 16 | +import questionary |
| 17 | +from botocore.exceptions import NoCredentialsError |
| 18 | + |
| 19 | +from rsxml.dotenv import parse_dotenv |
| 20 | + |
| 21 | + |
| 22 | +def upload_file(file_path: str, bucket_name: str, object_name=None, force_multipart=False): |
| 23 | + """Upload a file to an S3 bucket |
| 24 | +
|
| 25 | + :param file_path: File to upload |
| 26 | + :param bucket_name: Bucket to upload to |
| 27 | + :param object_name: S3 object name. If not specified then file_name is used |
| 28 | + :param force_multipart: If True, forces multipart upload even for small files |
| 29 | + :return: True if file was uploaded, else False |
| 30 | + """ |
| 31 | + |
| 32 | + # If S3 object_name was not specified, use file_name |
| 33 | + if object_name is None: |
| 34 | + object_name = os.path.basename(file_path) |
| 35 | + |
| 36 | + # Upload the file |
| 37 | + s3_client = boto3.client('s3') |
| 38 | + |
| 39 | + try: |
| 40 | + print(f"Uploading {file_path} to {bucket_name}/{object_name}...") |
| 41 | + |
| 42 | + if force_multipart: |
| 43 | + # Force multipart upload configuration |
| 44 | + # Set threshold to 0 to force multipart for any file size |
| 45 | + config = boto3.s3.transfer.TransferConfig( |
| 46 | + multipart_threshold=1, |
| 47 | + multipart_chunksize=50 * 1024 * 1024 # 50MB chunks to match rsxml defaults |
| 48 | + ) |
| 49 | + s3_client.upload_file(file_path, bucket_name, object_name, Config=config) |
| 50 | + else: |
| 51 | + # Standard upload (let boto3 decide, or force single part by setting high threshold) |
| 52 | + # To ensure single part for testing, we set a very high threshold |
| 53 | + config = boto3.s3.transfer.TransferConfig( |
| 54 | + multipart_threshold=10 * 1024 * 1024 * 1024 # 10GB threshold |
| 55 | + ) |
| 56 | + s3_client.upload_file(file_path, bucket_name, object_name, Config=config) |
| 57 | + |
| 58 | + # Fetch the object to get the ETag |
| 59 | + response = s3_client.head_object(Bucket=bucket_name, Key=object_name) |
| 60 | + remote_etag = response['ETag'] |
| 61 | + print(f"Upload Successful. Remote ETag: {remote_etag}") |
| 62 | + return remote_etag |
| 63 | + |
| 64 | + except NoCredentialsError: |
| 65 | + print("Credentials not available") |
| 66 | + return None |
| 67 | + except Exception as e: |
| 68 | + print(f"Upload failed: {e}") |
| 69 | + return None |
| 70 | + |
| 71 | + |
| 72 | +def main(): |
| 73 | + |
| 74 | + bucket_name = questionary.text("S3 Bucket name:").ask() |
| 75 | + s3_prefix = questionary.text("S3 Prefix (optional):", default="matt/s3etagtest").ask() |
| 76 | + if not bucket_name: |
| 77 | + return |
| 78 | + |
| 79 | + dir_path = questionary.path("Directory containing files to upload:").ask() |
| 80 | + if not dir_path: |
| 81 | + return |
| 82 | + |
| 83 | + if not os.path.exists(dir_path): |
| 84 | + print(f"Directory {dir_path} does not exist.") |
| 85 | + return |
| 86 | + |
| 87 | + files = [f for f in os.listdir(dir_path) if os.path.isfile(os.path.join(dir_path, f)) and not f.startswith('.')] |
| 88 | + |
| 89 | + print(f"Found {len(files)} files in {dir_path}") |
| 90 | + |
| 91 | + for filename in files: |
| 92 | + file_path = os.path.join(dir_path, filename) |
| 93 | + |
| 94 | + print(f"\n--- Processing {filename} ---") |
| 95 | + |
| 96 | + # 1. Upload as Single Part (Standard MD5) |
| 97 | + print("1. Uploading as Single Part...") |
| 98 | + etag_single = upload_file(file_path, bucket_name, s3_prefix + f"/single_part/{filename}", force_multipart=False) |
| 99 | + |
| 100 | + # 2. Upload as Multipart |
| 101 | + print("2. Uploading as Multipart...") |
| 102 | + etag_multipart = upload_file(file_path, bucket_name, s3_prefix + f"/multipart/{filename}", force_multipart=True) |
| 103 | + print(f"Summary for {filename}:") |
| 104 | + print(f" Single Part ETag: {etag_single}") |
| 105 | + print(f" Multipart ETag: {etag_multipart}") |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + main() |
0 commit comments