forked from mtusi2026/mtusi2026lab4
25 lines
954 B
Python
25 lines
954 B
Python
"""Bounded S3 client and image validation shared by the API and cleaner."""
|
|
import os
|
|
import warnings
|
|
from pathlib import Path
|
|
import boto3
|
|
from botocore.config import Config
|
|
from botocore.exceptions import BotoCoreError as StorageError, ClientError
|
|
from PIL import Image, UnidentifiedImageError
|
|
|
|
MAX_BYTES = 10_485_760
|
|
MAX_PIXELS = 40_000_000
|
|
BUCKET = os.environ.get('S3_BUCKET', 'messenger-attachments')
|
|
Image.MAX_IMAGE_PIXELS = MAX_PIXELS
|
|
|
|
def secret(name):
|
|
return Path(os.environ[name + '_FILE']).read_text().strip()
|
|
|
|
client = boto3.client('s3', endpoint_url=os.environ.get('S3_ENDPOINT', 'http://s3:9000'),
|
|
region_name='us-east-1', aws_access_key_id=secret('S3_ACCESS_KEY_ID'),
|
|
aws_secret_access_key=secret('S3_SECRET_ACCESS_KEY'),
|
|
config=Config(connect_timeout=2, read_timeout=10, retries={'total_max_attempts': 1},
|
|
s3={'addressing_style': 'path'}))
|
|
|
|
from images import validate_image, InvalidImage, TooManyPixels
|