"""S3 access with least-privilege application credentials (never root credentials).""" import os from pathlib import Path import boto3 from botocore.config import Config def s3_client(): return boto3.client( 's3', endpoint_url=os.environ['S3_ENDPOINT'], region_name=os.getenv('S3_REGION','us-east-1'), aws_access_key_id=Path(os.environ['S3_ACCESS_KEY_ID_FILE']).read_text().strip(), aws_secret_access_key=Path(os.environ['S3_SECRET_ACCESS_KEY_FILE']).read_text().strip(), config=Config(connect_timeout=2,read_timeout=15,retries={'max_attempts':2}, s3={'addressing_style':'path'})) def bucket(): return os.getenv('S3_BUCKET','messenger-attachments')