import os import time import boto3 from botocore.config import Config client = boto3.client( 's3', endpoint_url=os.getenv('S3_ENDPOINT', 'http://s3:8333'), region_name='us-east-1', aws_access_key_id=os.environ['S3_ACCESS_KEY_ID'], aws_secret_access_key=os.environ['S3_SECRET_ACCESS_KEY'], config=Config(connect_timeout=3, read_timeout=5, retries={'max_attempts': 1}), ) bucket = os.getenv('S3_BUCKET', 'messenger-attachments') for attempt in range(60): try: buckets = [x['Name'] for x in client.list_buckets().get('Buckets', [])] if bucket not in buckets: client.create_bucket(Bucket=bucket) print('Private S3 bucket ready:', bucket) break except Exception as exc: print('Waiting for S3:', str(exc), flush=True) if attempt == 59: raise time.sleep(2)