ЛР6: наблюдаемость, SLO и диагностика отказов
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import hashlib
|
||||
import boto3
|
||||
from botocore.config import Config
|
||||
from botocore.exceptions import ClientError
|
||||
from .config import secret
|
||||
|
||||
|
||||
class Storage:
|
||||
def __init__(self, settings):
|
||||
self.settings = settings
|
||||
self.client = boto3.client(
|
||||
"s3",
|
||||
endpoint_url=settings.endpoint,
|
||||
region_name="us-east-1",
|
||||
aws_access_key_id=secret("s3_access_key_id"),
|
||||
aws_secret_access_key=secret("s3_secret_access_key"),
|
||||
config=Config(
|
||||
connect_timeout=3,
|
||||
read_timeout=10,
|
||||
retries={"max_attempts": 1},
|
||||
s3={"addressing_style": "path"},
|
||||
),
|
||||
)
|
||||
|
||||
def read_original(self, job):
|
||||
key = job["staging_key"] or job["object_key"]
|
||||
try:
|
||||
data = self._read(key)
|
||||
except ClientError as error:
|
||||
if error.response["Error"]["Code"] != "NoSuchKey" or key == job["object_key"]:
|
||||
raise
|
||||
data = self._read(job["object_key"])
|
||||
if (
|
||||
len(data) > self.settings.max_bytes
|
||||
or len(data) != job["size_bytes"]
|
||||
or hashlib.sha256(data).hexdigest() != job["checksum"]
|
||||
):
|
||||
raise ValueError("invalid_staging")
|
||||
return data
|
||||
|
||||
def _read(self, key):
|
||||
response = self.client.get_object(Bucket=self.settings.bucket, Key=key)
|
||||
with response["Body"] as stream:
|
||||
return stream.read(self.settings.max_bytes + 1)
|
||||
|
||||
def write_result(self, key, data, content_type):
|
||||
self.client.put_object(
|
||||
Bucket=self.settings.bucket, Key=key, Body=data, ContentType=content_type
|
||||
)
|
||||
return {
|
||||
"object_key": key,
|
||||
"content_type": content_type,
|
||||
"size_bytes": len(data),
|
||||
"sha256": hashlib.sha256(data).hexdigest(),
|
||||
}
|
||||
|
||||
def remove_staging(self, job, repository):
|
||||
if not job["staging_key"]:
|
||||
return
|
||||
if not repository.clear_staging(job):
|
||||
return
|
||||
self.client.delete_object(Bucket=self.settings.bucket, Key=job["staging_key"])
|
||||
Reference in New Issue
Block a user