37 lines
1.6 KiB
Python
37 lines
1.6 KiB
Python
"""S3 operations with bounded timeouts and immutable server-generated keys."""
|
|
import boto3
|
|
from botocore.config import Config
|
|
from botocore.exceptions import ClientError
|
|
from src.config import setting
|
|
|
|
def client():
|
|
return boto3.client('s3', endpoint_url=setting('S3_ENDPOINT','http://s3:9000'),
|
|
aws_access_key_id=setting('S3_ACCESS_KEY'), aws_secret_access_key=setting('S3_SECRET_KEY'),
|
|
region_name='us-east-1', config=Config(connect_timeout=2,read_timeout=5,
|
|
retries={'total_max_attempts':1},s3={'addressing_style':'path'}))
|
|
|
|
BUCKET = setting('S3_BUCKET','attachments')
|
|
|
|
|
|
class ObjectConflict(Exception):
|
|
"""Неизменяемый ключ уже содержит другие данные."""
|
|
|
|
|
|
def put_immutable(s3, key, stream, size, content_type, digest):
|
|
try:
|
|
s3.put_object(Bucket=BUCKET, Key=key, Body=stream, ContentLength=size,
|
|
ContentType=content_type, Metadata={'sha256': digest}, IfNoneMatch='*')
|
|
except ClientError as exc:
|
|
if exc.response['ResponseMetadata']['HTTPStatusCode'] != 412:
|
|
raise
|
|
existing = s3.head_object(Bucket=BUCKET, Key=key)
|
|
if (existing['ContentLength'], existing['ContentType'], existing['Metadata'].get('sha256')) != (
|
|
size, content_type, digest):
|
|
raise ObjectConflict(key) from None
|
|
|
|
|
|
def object_lock(conn, key, wait=True):
|
|
fn = 'pg_advisory_xact_lock' if wait else 'pg_try_advisory_xact_lock'
|
|
row = conn.execute('SELECT '+fn+'(hashtextextended(%s,42)) AS locked',(key,)).fetchone()
|
|
return wait or row['locked']
|