Реализовать устойчивый приём вложений, брокер и обработку миниатюр
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user