|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 4 | + |
| 5 | +"""Upload a file to S3 with exponential backoff retry and optional optimistic locking.""" |
| 6 | + |
| 7 | +import argparse |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +import time |
| 11 | + |
| 12 | + |
| 13 | +def head_etag(bucket: str, key: str) -> str | None: |
| 14 | + """Fetch the current ETag for an object, or None if it doesn't exist.""" |
| 15 | + result = subprocess.run( |
| 16 | + [ |
| 17 | + "aws", |
| 18 | + "s3api", |
| 19 | + "head-object", |
| 20 | + "--bucket", |
| 21 | + bucket, |
| 22 | + "--key", |
| 23 | + key, |
| 24 | + "--query", |
| 25 | + "ETag", |
| 26 | + "--output", |
| 27 | + "text", |
| 28 | + ], |
| 29 | + capture_output=True, |
| 30 | + text=True, |
| 31 | + ) |
| 32 | + if result.returncode != 0: |
| 33 | + return None |
| 34 | + etag = result.stdout.strip() |
| 35 | + if not etag or etag == "null": |
| 36 | + return None |
| 37 | + return etag |
| 38 | + |
| 39 | + |
| 40 | +def put_object( |
| 41 | + bucket: str, |
| 42 | + key: str, |
| 43 | + body: str, |
| 44 | + checksum_algorithm: str | None, |
| 45 | + if_match: str | None, |
| 46 | +) -> bool: |
| 47 | + """Upload an object, returning True on success.""" |
| 48 | + cmd = [ |
| 49 | + "aws", |
| 50 | + "s3api", |
| 51 | + "put-object", |
| 52 | + "--bucket", |
| 53 | + bucket, |
| 54 | + "--key", |
| 55 | + key, |
| 56 | + "--body", |
| 57 | + body, |
| 58 | + ] |
| 59 | + if checksum_algorithm: |
| 60 | + cmd.extend(["--checksum-algorithm", checksum_algorithm]) |
| 61 | + if if_match: |
| 62 | + cmd.extend(["--if-match", if_match]) |
| 63 | + |
| 64 | + result = subprocess.run(cmd) |
| 65 | + return result.returncode == 0 |
| 66 | + |
| 67 | + |
| 68 | +def main(): |
| 69 | + parser = argparse.ArgumentParser(description="Upload a file to S3 with retry and optional optimistic locking") |
| 70 | + parser.add_argument("--bucket", required=True, help="S3 bucket name") |
| 71 | + parser.add_argument("--key", required=True, help="S3 object key") |
| 72 | + parser.add_argument("--body", required=True, help="Local file to upload") |
| 73 | + parser.add_argument("--checksum-algorithm", help="Checksum algorithm (e.g. CRC32)") |
| 74 | + parser.add_argument( |
| 75 | + "--optimistic-lock", |
| 76 | + action="store_true", |
| 77 | + help="Use ETag-based optimistic locking (re-fetches ETag on each retry)", |
| 78 | + ) |
| 79 | + parser.add_argument("--max-retries", type=int, default=5, help="Maximum number of retries") |
| 80 | + args = parser.parse_args() |
| 81 | + |
| 82 | + for attempt in range(1, args.max_retries + 1): |
| 83 | + if_match = None |
| 84 | + if args.optimistic_lock: |
| 85 | + if_match = head_etag(args.bucket, args.key) |
| 86 | + # New object, no ETag to match — just upload without locking |
| 87 | + # (this handles the first-ever upload case) |
| 88 | + |
| 89 | + if put_object(args.bucket, args.key, args.body, args.checksum_algorithm, if_match): |
| 90 | + print("Upload successful.") |
| 91 | + return |
| 92 | + |
| 93 | + if attempt == args.max_retries: |
| 94 | + break |
| 95 | + |
| 96 | + delay = min(2**attempt, 30) |
| 97 | + print( |
| 98 | + f"S3 upload failed (attempt {attempt}/{args.max_retries}), retrying in {delay}s...", |
| 99 | + file=sys.stderr, |
| 100 | + ) |
| 101 | + time.sleep(delay) |
| 102 | + |
| 103 | + print( |
| 104 | + f"S3 upload failed after {args.max_retries} attempts", |
| 105 | + file=sys.stderr, |
| 106 | + ) |
| 107 | + sys.exit(1) |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + main() |
0 commit comments