Files
mtusi2026lab5/src/storage.py
T
2026-10-26 18:05:06 +03:00

61 lines
2.5 KiB
Python

"""S3-клиент с ограниченными credentials; ключи назначаются приложением."""
import boto3
from botocore.config import Config
from botocore.exceptions import ClientError
from src.config import S3_ENDPOINT, S3_REGION, S3_BUCKET, S3_ACCESS_KEY, S3_SECRET_KEY
PREFIX = 'originals/'
class ObjectStore:
def __init__(self):
self.client = boto3.client(
's3', endpoint_url=S3_ENDPOINT, region_name=S3_REGION,
aws_access_key_id=S3_ACCESS_KEY, aws_secret_access_key=S3_SECRET_KEY,
config=Config(connect_timeout=2, read_timeout=5, retries={'total_max_attempts': 1},
s3={'addressing_style': 'path'}, request_checksum_calculation='when_required',
response_checksum_validation='when_required'))
def ready(self):
self.client.list_objects_v2(Bucket=S3_BUCKET, Prefix=PREFIX, MaxKeys=1)
def put(self, file, key, size, mime, sha256, content_md5):
file.seek(0)
self.client.put_object(Bucket=S3_BUCKET, Key=key, Body=file, ContentLength=size,
ContentType=mime, Metadata={'sha256': sha256}, ContentMD5=content_md5)
def copy(self, source, target):
self.client.copy_object(Bucket=S3_BUCKET,Key=target,CopySource={'Bucket':S3_BUCKET,'Key':source})
def head(self, key):
try:
return self.client.head_object(Bucket=S3_BUCKET, Key=key)
except ClientError as exc:
if exc.response['Error']['Code'] in {'404', 'NoSuchKey', 'NotFound'}:
return None
raise
def get(self, key):
return self.client.get_object(Bucket=S3_BUCKET, Key=key)
def delete(self, key):
self.client.delete_object(Bucket=S3_BUCKET, Key=key)
def objects(self, prefix=PREFIX, counters=None):
pages = self.client.get_paginator('list_objects_v2')
for page in pages.paginate(Bucket=S3_BUCKET, Prefix=prefix):
if counters is not None:
counters['list_requests'] += 1
yield from page.get('Contents', [])
def lock_attachment(db, attachment_id, wait=True):
function = 'pg_advisory_lock' if wait else 'pg_try_advisory_lock'
return db.execute(f'SELECT {function}(hashtextextended(%s,0)) AS locked',
('attachment:' + str(attachment_id),)).fetchone()['locked']
def unlock_attachment(db, attachment_id):
db.execute('SELECT pg_advisory_unlock(hashtextextended(%s,0))',
('attachment:' + str(attachment_id),))