datadouble-s3 0.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,151 @@
1
+ # Copyright (c) 2026- Tight Line LLC
2
+ # SPDX-License-Identifier: MIT
3
+
4
+ """The S3 storage adapter for Datadouble.
5
+
6
+ Spec settings::
7
+
8
+ store:
9
+ - s3:
10
+ bucket: golden-images
11
+ prefix: shop # optional
12
+ region: us-east-1 # optional
13
+ endpoint_url: https://s3.internal.example # optional, S3-compatible services
14
+
15
+ Credentials come from the usual AWS sources and never appear in the spec.
16
+ """
17
+
18
+ from collections.abc import Mapping
19
+ from pathlib import Path
20
+ from typing import TYPE_CHECKING
21
+
22
+ import boto3
23
+ from botocore.config import Config
24
+ from botocore.exceptions import BotoCoreError, ClientError
25
+ from pydantic import BaseModel, ConfigDict, Field, JsonValue
26
+
27
+ from datadouble.settings import parse_settings, remote
28
+ from datadouble.storage import Store, StoreError, check_key
29
+
30
+ if TYPE_CHECKING:
31
+ # Typing stubs, installed only for development; the runtime image lacks them.
32
+ from mypy_boto3_s3 import S3Client
33
+
34
+ _MISSING = frozenset({"NoSuchKey", "404", "NotFound"})
35
+
36
+
37
+ class S3Settings(BaseModel):
38
+ """The settings of one ``s3`` store."""
39
+
40
+ model_config = ConfigDict(strict=True, frozen=True, extra="forbid")
41
+
42
+ bucket: str = Field(min_length=3, max_length=63)
43
+ prefix: str = ""
44
+ region: str | None = None
45
+ endpoint_url: str | None = None
46
+
47
+
48
+ def _failure(error: Exception, action: str) -> StoreError:
49
+ """A store error naming the S3 error code, not the request or credentials."""
50
+ if isinstance(error, ClientError):
51
+ code = error.response.get("Error", {}).get("Code", "unknown")
52
+ return StoreError(f"{action} failed: S3 error {code}")
53
+ return StoreError(f"{action} failed: {type(error).__name__}")
54
+
55
+
56
+ def _code(error: ClientError) -> str:
57
+ return str(error.response.get("Error", {}).get("Code", ""))
58
+
59
+
60
+ class S3Store:
61
+ """Images under one bucket and key prefix."""
62
+
63
+ def __init__(self, settings: S3Settings, client: "S3Client") -> None:
64
+ """Use ``client`` for every request."""
65
+ self.settings = settings
66
+ self.client = client
67
+ self.prefix = settings.prefix.strip("/")
68
+
69
+ def _key(self, key: str) -> str:
70
+ check_key(key)
71
+ return f"{self.prefix}/{key}" if self.prefix else key
72
+
73
+ def describe(self) -> str:
74
+ """``s3://bucket/prefix``."""
75
+ location = f"s3://{self.settings.bucket}"
76
+ return f"{location}/{self.prefix}" if self.prefix else location
77
+
78
+ def put_file(self, key: str, source: Path) -> None:
79
+ """Upload a file; large files go up in parts, and only appear when complete."""
80
+ try:
81
+ self.client.upload_file(str(source), self.settings.bucket, self._key(key))
82
+ except (BotoCoreError, ClientError) as error:
83
+ raise _failure(error, f"uploading {key}") from None
84
+
85
+ def put_bytes(self, key: str, data: bytes) -> None:
86
+ """Write a small object."""
87
+ try:
88
+ self.client.put_object(Bucket=self.settings.bucket, Key=self._key(key), Body=data)
89
+ except (BotoCoreError, ClientError) as error:
90
+ raise _failure(error, f"writing {key}") from None
91
+
92
+ def get_bytes(self, key: str) -> bytes | None:
93
+ """Read a small object, or None if it does not exist."""
94
+ try:
95
+ response = self.client.get_object(Bucket=self.settings.bucket, Key=self._key(key))
96
+ except ClientError as error:
97
+ if _code(error) in _MISSING:
98
+ return None
99
+ raise _failure(error, f"reading {key}") from None
100
+ except BotoCoreError as error:
101
+ raise _failure(error, f"reading {key}") from None
102
+ return response["Body"].read()
103
+
104
+ def get_file(self, key: str, destination: Path) -> bool:
105
+ """Download an object; False if it does not exist."""
106
+ try:
107
+ self.client.download_file(self.settings.bucket, self._key(key), str(destination))
108
+ except ClientError as error:
109
+ if _code(error) in _MISSING:
110
+ return False
111
+ raise _failure(error, f"downloading {key}") from None
112
+ except BotoCoreError as error:
113
+ raise _failure(error, f"downloading {key}") from None
114
+ return True
115
+
116
+ def delete(self, key: str) -> None:
117
+ """Remove an object; S3 does not mind if it does not exist."""
118
+ try:
119
+ self.client.delete_object(Bucket=self.settings.bucket, Key=self._key(key))
120
+ except (BotoCoreError, ClientError) as error:
121
+ raise _failure(error, f"deleting {key}") from None
122
+
123
+ def list(self, prefix: str) -> list[str]:
124
+ """Keys starting with ``prefix``, relative to the store's own prefix."""
125
+ root = f"{self.prefix}/" if self.prefix else ""
126
+ keys: list[str] = []
127
+ try:
128
+ for page in self.client.get_paginator("list_objects_v2").paginate(
129
+ Bucket=self.settings.bucket, Prefix=root + prefix
130
+ ):
131
+ keys.extend(item.get("Key", "")[len(root) :] for item in page.get("Contents", []))
132
+ except (BotoCoreError, ClientError) as error:
133
+ raise _failure(error, f"listing {prefix or '/'}") from None
134
+ return sorted(keys)
135
+
136
+
137
+ @remote("prefix")
138
+ def s3_store(settings: Mapping[str, JsonValue]) -> Store:
139
+ """The entry point named in ``datadouble.stores``.
140
+
141
+ ``prefix`` is a key prefix in the bucket, never a path on the job's machine.
142
+ """
143
+ parsed = parse_settings(S3Settings, settings, "s3 store")
144
+ # boto3-stubs overloads client() for every AWS service; only S3's stubs are installed.
145
+ client: S3Client = boto3.client( # pyright: ignore[reportUnknownMemberType] - see above
146
+ "s3",
147
+ region_name=parsed.region,
148
+ endpoint_url=parsed.endpoint_url,
149
+ config=Config(s3={"addressing_style": "path"} if parsed.endpoint_url else {}),
150
+ )
151
+ return S3Store(parsed, client)
datadouble_s3/py.typed ADDED
File without changes
@@ -0,0 +1,42 @@
1
+ Metadata-Version: 2.5
2
+ Name: datadouble-s3
3
+ Version: 0.0.3
4
+ Summary: S3 storage adapter for Datadouble
5
+ Project-URL: Homepage, https://datadouble.tightlinesoftware.com/
6
+ Project-URL: Source, https://github.com/datadouble/datadouble
7
+ Author: Tight Line LLC
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Classifier: Development Status :: 2 - Pre-Alpha
11
+ Classifier: Programming Language :: Python :: 3 :: Only
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Programming Language :: Python :: 3.14
14
+ Classifier: Programming Language :: Python :: 3.15
15
+ Classifier: Topic :: System :: Archiving
16
+ Classifier: Typing :: Typed
17
+ Requires-Python: >=3.13
18
+ Requires-Dist: boto3>=1.44.13
19
+ Requires-Dist: datadouble
20
+ Description-Content-Type: text/markdown
21
+
22
+ # datadouble-s3
23
+
24
+ The S3 storage adapter for [Datadouble](https://datadouble.tightlinesoftware.com/):
25
+ golden images and their image summaries in an S3 bucket, or any service with
26
+ an S3-compatible API.
27
+
28
+ ```yaml
29
+ store:
30
+ - s3:
31
+ bucket: golden-images
32
+ prefix: shop # optional
33
+ region: us-east-1 # optional
34
+ endpoint_url: https://s3.internal.example # optional, for S3-compatible services
35
+ ```
36
+
37
+ Credentials come from the usual AWS sources (environment, shared files, an
38
+ instance or workload role); they never appear in the spec.
39
+
40
+ `prefix` is a key prefix inside the bucket, not a directory. Even a prefix
41
+ that starts with `/` is never mounted into the jobs `ci init` writes
42
+ ([which settings become mounts](../../docs/ci-cd.md#which-settings-become-mounts)).
@@ -0,0 +1,7 @@
1
+ datadouble_s3/__init__.py,sha256=ypfwJIIJO3FTqHr-1dkCOQ48wcCCF08sY-FVZc2ZcwU,5852
2
+ datadouble_s3/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ datadouble_s3-0.0.3.dist-info/METADATA,sha256=qXp6IwHgmHpV8CWudedDhhfWWz-5zhL3I78cG3FWNQQ,1538
4
+ datadouble_s3-0.0.3.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
5
+ datadouble_s3-0.0.3.dist-info/entry_points.txt,sha256=Bljrug0bU9hyrYE-a-SibBXhXXhi9r46J82mTukRaxs,48
6
+ datadouble_s3-0.0.3.dist-info/licenses/LICENSE,sha256=2LcT-SSMiSa7vrHsdC41sUWNmkcwZqIYdYBaSVmqqT8,1072
7
+ datadouble_s3-0.0.3.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [datadouble.stores]
2
+ s3 = datadouble_s3:s3_store
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026- Tight Line LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.