Files
mtusi2026lab5/src/storage.py
T

31 lines
1.1 KiB
Python

"""Shared dependencies. No raw credentials are printed or exposed through HTTP."""
import os
from pathlib import Path
import boto3
from botocore.config import Config
import psycopg
from psycopg.rows import dict_row
BUCKET = os.getenv('S3_BUCKET', 'messenger-attachments')
PREFIX = 'results/'
STORAGE_LOCK = 440004
def setting(name, default=''):
path = os.getenv(name + '_FILE')
return Path(path).read_text().strip() if path else os.getenv(name, default)
def db():
return psycopg.connect(setting('DATABASE_URL'), row_factory=dict_row,
connect_timeout=3, options='-c statement_timeout=15000 -c lock_timeout=10000')
def s3():
return boto3.client('s3', endpoint_url=os.getenv('S3_ENDPOINT', 'http://s3:9000'),
region_name=os.getenv('S3_REGION', 'us-east-1'),
aws_access_key_id=setting('S3_ACCESS_KEY_ID'),
aws_secret_access_key=setting('S3_SECRET_ACCESS_KEY'),
config=Config(connect_timeout=3, read_timeout=10, max_pool_connections=16,
retries={'total_max_attempts': 1}, s3={'addressing_style': 'path'}))