Created
July 8, 2026 08:35
-
-
Save Dirga36/1b7a12394beac909219aa955f949179d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| import random | |
| import boto3 | |
| from botocore.exceptions import ClientError | |
| s3 = boto3.client("s3") | |
| def upload_with_retry(local_path: str, bucket: str, s3_key: str, max_retries: int = 5) -> bool: | |
| for attempt in range(max_retries): | |
| try: | |
| s3.upload_file(local_path, bucket, s3_key) | |
| return True | |
| except ClientError as error: | |
| wait_time = (2 ** attempt) + random.uniform(0, 1) | |
| print(f"Attempt {attempt + 1} failed: {error}. Retrying in {wait_time:.1f}s") | |
| time.sleep(wait_time) | |
| print(f"Failed to upload {local_path} after {max_retries} attempts") | |
| return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment