add level 3

This commit is contained in:
Матвей
2026-10-26 10:19:05 +03:00
parent 3b67201e5e
commit df15ab0967
16 changed files with 438 additions and 25 deletions
+14 -4
View File
@@ -69,6 +69,13 @@ class S3Store:
await self.call("put_object", Bucket=self.settings.bucket, Key=key,
Body=file, ContentLength=size, ContentType=content_type)
async def copy(self, source_key: str, destination_key: str, content_type: str) -> None:
await self.call(
"copy_object", Bucket=self.settings.bucket, Key=destination_key,
CopySource={"Bucket": self.settings.bucket, "Key": source_key},
ContentType=content_type, MetadataDirective="REPLACE",
)
async def list(self, prefix: str) -> dict[str, object]:
"""Return every object under a controlled prefix, handling S3 pagination."""
objects, _ = await self.list_with_pages(prefix)
@@ -95,8 +102,11 @@ class S3Store:
await self.call("delete_object", Bucket=self.settings.bucket, Key=key)
async def download(self, attachment):
response = await self.call("get_object", Bucket=self.settings.bucket,
Key=attachment["object_key"])
return await self.download_key(attachment["original_key"], attachment["size_bytes"], attachment["sha256"])
async def download_key(self, key: str, expected_size: int | None = None,
expected_sha256: str | None = None) -> bytes:
response = await self.call("get_object", Bucket=self.settings.bucket, Key=key)
body = response["Body"]
try:
data = await run_in_threadpool(body.read, self.settings.max_bytes + 1)
@@ -104,7 +114,7 @@ class S3Store:
raise ApiError(503, "unavailable", "S3 недоступно") from error
finally:
body.close()
if (len(data) != attachment["size_bytes"] or
sha256(data).hexdigest() != attachment["sha256"]):
if ((expected_size is not None and len(data) != expected_size) or
(expected_sha256 is not None and sha256(data).hexdigest() != expected_sha256)):
raise ApiError(503, "unavailable", "Оригинал недоступен")
return data