qrunch.tools.storage.s3_persister

Contains Persisters.

Classes

S3Persister

Store serialized bytes in an existing S3 bucket without local temporary files.

class S3Persister

Bases: PersisterProtocol

Store serialized bytes in an existing S3 bucket without local temporary files.

Credentials are resolved through boto3’s normal credential chain, including environment variables, AWS profiles, and IAM roles. Initialization checks prefix access with ListObjectsV2; reading and writing additionally require ListBucket, GetObject, and PutObject permissions. Collision policies assume a single writer for each (key, unique_id) pair; checking and uploading are not atomic.

Keys ending in an underscore-delimited number (such as energy_001_) and unique identifiers containing or overlapping __eui_ are rejected because the legacy object naming scheme cannot distinguish them from revisions or key delimiters. Multipart uploads also require AbortMultipartUpload permission for cleanup.

New objects use by-key/<encoded-key>/ directories for key-scoped listings. Exact key/unique-id lookups merge scoped and legacy flat revisions, preferring scoped copies when revision numbers overlap. Key-only discovery requires migrating legacy objects into these directories; it never falls back to scanning the entire configured prefix.

__init__(bucket_name: str, region_name: str | None = None, prefix: str = '', extension: str = '.qdk', overwriting_policy: OverwritingPolicy = OverwritingPolicy.RENAME, padding_width: int = 3) → None

Initialize storage and validate access to the existing bucket.

Parameters:
  • bucket_name (str) – Name of the existing S3 bucket, not an S3 URI.

  • region_name (str | None) – AWS region, or None to use boto3’s configured region.

  • prefix (str) – Optional object-key prefix within the bucket.

  • extension (str) – Extension appended to stored object names.

  • overwriting_policy (OverwritingPolicy) – Policy for existing data.

  • padding_width (int) – Minimum width of numeric revision suffixes.

Return type:

None

has_data(key: str) → bool

Check whether any objects exist for a key.

Parameters:

key (str) – Key to find.

Return type:

bool

has_matching_data(key: str, unique_id: str) → bool

Check whether objects exist for a key and unique identifier.

Parameters:
  • key (str) – Key to find.

  • unique_id (str) – Unique identifier to find.

Return type:

bool

key_and_unique_ids() → set[KeyUniqueId]

Enumerate key and unique identifier pairs within the configured prefix.

Return type:

set[KeyUniqueId]

list_data_for_key(key: str) → list[str]

List unique identifiers stored for a key.

Parameters:

key (str) – Key to find.

Return type:

list[str]

read(key: str, unique_id: str) → Generator[bytes, None, None]

Lazily read matching revisions, newest first.

Parameters:
  • key (str) – Key under which the data is stored.

  • unique_id (str) – Unique identifier describing the data.

Return type:

Generator[bytes, None, None]

read_ranges(key: str, unique_id: str) → Generator[Callable[[int, int | None], bytes], None, None]

Yield readers for byte ranges of matching revisions, newest first.

Parameters:
  • key (str) – Key under which the data is stored.

  • unique_id (str) – Unique identifier describing the data.

Return type:

Generator[Callable[[int, int | None], bytes], None, None]

write(key: str, unique_id: str, serialized_data: bytes | memoryview) → None

Upload bytes using the configured collision policy.

Parameters:
  • key (str) – Key under which the data is stored.

  • unique_id (str) – Unique identifier describing the data.

  • serialized_data (bytes | memoryview) – Serialized bytes to upload.

Return type:

None