derp.storage – File Storage¶
Storage wrapper for S3-compatible object storage.
Example usage:
from storage import Storage
- async with Storage(
bucket_name=”my-bucket”, endpoint_url=”https://s3.amazonaws.com”, aws_access_key_id=”key”, aws_secret_access_key=”secret”,
- ) as storage:
# Upload a file await storage.upload_file(“path/to/local/file.txt”, “remote/file.txt”)
# Fetch a file content = await storage.fetch_file(“remote/file.txt”)
- exception derp.storage.StorageAccessDeniedError[source]¶
Bases:
StorageErrorRaised when S3 rejects a call with AccessDenied / 403.
- exception derp.storage.StorageBackendError[source]¶
Bases:
StorageErrorWraps an unrecognised backend failure (network, 5xx, unknown 4xx).
Always chains the original exception via
raise ... from excso the backend-specific type and traceback are preserved for debugging. Thecodeattribute carries the S3 error code where available.
- exception derp.storage.StorageBucketNotFoundError[source]¶
Bases:
StorageErrorRaised when an operation targets a missing bucket.
- class derp.storage.StorageClient[source]¶
Bases:
objectS3-compatible storage client for uploading and fetching files.
Example:
config = StorageConfig( endpoint_url="https://s3.amazonaws.com", access_key_id="key", secret_access_key="secret", ) storage = StorageClient(config) await storage.connect() await storage.upload_file("local.txt", "remote.txt") await storage.disconnect()
- __init__(config)[source]¶
Initialize Storage client.
- Parameters:
config (StorageConfig) – Storage configuration.
- get_url(*, bucket, key)[source]¶
Gets the URL for a file in S3 (public or private).
- Parameters:
- Returns:
URL for the file.
- Raises:
ValueError – If no public endpoint URL is configured for this bucket and endpoint_url is not configured.
- Return type:
- async upload_file(*, bucket, key, data, content_type=None, metadata=None, extra_args=None)[source]¶
Upload a file to S3.
- Parameters:
bucket (str) – Name of the S3 bucket.
key (str) – S3 object key (path in bucket).
data (bytes) – Bytes to upload.
content_type (str | None) – MIME type of the file.
metadata (dict[str, str] | None) – Metadata to attach to the object.
extra_args (dict[str, Any] | None) – Additional arguments to pass to put_object.
- Return type:
None
Example:
await storage.upload_file( bucket="my-bucket", key="remote/file.txt", data=b"Hello, World!", content_type="text/plain", metadata={"author": "user123"}, )
- async fetch_file(*, bucket, key)[source]¶
Fetch a file from S3.
- Parameters:
- Returns:
File content as bytes.
- Raises:
StorageObjectNotFoundError – If
keydoes not exist.StorageBucketNotFoundError – If
bucketdoes not exist.StorageAccessDeniedError – If the backend denies the request.
StorageBackendError – For any other backend failure.
- Return type:
Example
- content = await storage.fetch_file(
bucket=”my-bucket”, key=”remote/file.txt”
)
- async delete_file(*, bucket, key)[source]¶
Delete a file from S3.
S3 deletes are idempotent: calling this for a missing key succeeds silently rather than raising.
- async delete_files(*, bucket, keys)[source]¶
Delete multiple files from S3 in a single request.
- Parameters:
- Returns:
List of keys that were successfully deleted.
- Raises:
StoragePartialDeleteError – If S3 reports per-key failures. The exception carries both the failed entries and the keys that succeeded in the same batch.
- Return type:
- async copy_file(*, src_bucket, src_key, dst_bucket=None, dst_key)[source]¶
Copy an object between keys or buckets (server-side).
- Parameters:
- Raises:
StorageObjectNotFoundError – If the source object does not exist.
StorageBucketNotFoundError – If either bucket does not exist.
- Return type:
None
- async file_exists(*, bucket, key)[source]¶
Check if a file exists in S3.
Returns
Falsefor a missing key; only true backend errors (denied, network, 5xx) raise.
- async list_files(*, bucket, prefix='', max_keys=None)[source]¶
List files in S3 bucket.
- Parameters:
- Returns:
List of object keys.
- Return type:
Example
files = await storage.list_files(bucket=”my-bucket”, prefix=”folder/”)
- async signed_download_url(*, bucket, key, expires_in=3600, response_content_disposition=None, response_content_type=None)[source]¶
Generate a presigned URL for downloading (GET) an object.
- Parameters:
bucket (str) – Name of the S3 bucket.
key (str) – S3 object key (path in bucket).
expires_in (int) – URL expiry in seconds (default 3600).
response_content_disposition (str | None) – Value for the
Content-Dispositionresponse header the storage backend returns for this URL, e.g.'attachment; filename="report.pdf"'to force a download. The value is signed into the URL, so it can’t be tampered with.response_content_type (str | None) – Value for the
Content-Typeresponse header the storage backend returns for this URL.
- Returns:
Presigned URL string.
- Return type:
- async signed_upload_url(*, bucket, key, expires_in=3600, content_type=None)[source]¶
Generate a presigned URL for uploading (PUT) an object.
- async list_objects(*, bucket, prefix='', max_keys=1000)[source]¶
List objects and common prefixes in a bucket.
Uses
Delimiter='/'for folder-like browsing.- Parameters:
- Returns:
Dict with ‘objects’ (list of object metadata dicts) and ‘prefixes’ (list of prefix strings representing folders).
- Return type:
- class derp.storage.StorageConfig[source]¶
Bases:
_StrictModelStorage configuration.
- model_config = {'extra': 'forbid'}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- exception derp.storage.StorageError[source]¶
Bases:
ExceptionBase exception for all storage errors.
- exception derp.storage.StorageNotConnectedError[source]¶
Bases:
StorageErrorRaised when storage client is used before connect().
- exception derp.storage.StorageObjectNotFoundError[source]¶
Bases:
StorageErrorRaised when an S3 object lookup targets a missing key.
The bucket and key are exposed as attributes for logging.
- exception derp.storage.StoragePartialDeleteError[source]¶
Bases:
StorageErrorRaised when
delete_objectsreports per-key failures.The
errorsattribute is a list of{"key", "code", "message"}dicts mirroring the S3 response. Thedeletedattribute lists keys that succeeded in the same batch.