Skip to content

Instantly share code, notes, and snippets.

@Dirga36
Created July 8, 2026 08:35
Show Gist options
  • Select an option

  • Save Dirga36/1b7a12394beac909219aa955f949179d to your computer and use it in GitHub Desktop.

Select an option

Save Dirga36/1b7a12394beac909219aa955f949179d to your computer and use it in GitHub Desktop.
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