"""Private S3-compatible object storage client.""" import boto3 from botocore.config import Config as BotoConfig from botocore.exceptions import BotoCoreError, ClientError class Storage: def __init__(self, endpoint, region, bucket, access_key_id, secret_access_key): self.bucket = bucket self.client = boto3.client( "s3", endpoint_url=endpoint, region_name=region, aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key, config=BotoConfig( signature_version="s3v4", s3={"addressing_style": "path"}, connect_timeout=2, read_timeout=5, # Fail fast: a stalled storage call must not hold an API thread # for the whole client timeout, and retries are the client's job. retries={"max_attempts": 1, "mode": "standard"}, ), ) def ready(self): try: self.client.head_bucket(Bucket=self.bucket) return True except (BotoCoreError, ClientError): return False def put(self, key, data, content_type): self.client.put_object( Bucket=self.bucket, Key=key, Body=data, ContentType=content_type, ContentLength=len(data), ) def get(self, key): return self.client.get_object(Bucket=self.bucket, Key=key) def delete(self, key): self.client.delete_object(Bucket=self.bucket, Key=key) def is_missing_object(error): if not isinstance(error, ClientError): return False code = str(error.response.get("Error", {}).get("Code", "")) status = error.response.get("ResponseMetadata", {}).get("HTTPStatusCode") return code in ("NoSuchKey", "NotFound", "404") or status == 404