Реализовать устойчивый приём вложений, брокер и обработку миниатюр

This commit is contained in:
2026-11-20 06:15:06 +03:00
parent 5894da9fd1
commit a0ec26d96d
19 changed files with 738 additions and 26 deletions
+19
View File
@@ -1,6 +1,7 @@
"""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():
@@ -11,6 +12,24 @@ def client():
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()