backends
blobmap.backends ¶
Turning a URL into an obstore store, with useful errors when it fails.
obstore reads most S3 settings from the environment, but with none of them
set it assumes AWS and looks for instance credentials on the EC2 metadata
address. Off EC2 that hangs for the full retry budget and then fails with a
Rust backtrace that does not mention credentials at all, so this module
translates the common failures into something actionable.
StoreUnreachable ¶
Bases: RuntimeError
A store could not be opened or listed.
S3Options
dataclass
¶
S3Options(
endpoint: str | None = None,
region: str | None = None,
anonymous: bool = False,
allow_http: bool | None = None,
virtual_hosted: bool | None = None,
)
Connection settings that override the environment.
Every field defaults to None, meaning "leave it to obstore", which
falls back to the usual AWS_* environment variables. Only set fields are
passed through, so these compose with an existing environment rather than
replacing it.
Attributes:
| Name | Type | Description |
|---|---|---|
endpoint |
str | None
|
S3 endpoint URL. Required for anything that is not AWS.
Also settable as |
region |
str | None
|
Region name. |
anonymous |
bool
|
Skip credentials and request signing entirely. Use for
public buckets, and note this is also the quickest way to avoid
the instance metadata lookup. Also |
allow_http |
bool | None
|
Permit a plain |
virtual_hosted |
bool | None
|
Use |
config ¶
config() -> dict[str, Any]
Build the keyword arguments for obstore.store.from_url.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Only the options that were actually set, so unset ones continue |
dict[str, Any]
|
to come from the environment. |
Source code in src/blobmap/backends.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
is_local ¶
is_local(url: str | Path) -> bool
Whether a URL or path refers to the local filesystem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str | Path
|
A URL or a bare path. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True for |
Example
all(is_local(u) for u in ("/foo", "foo", "~/foo", "file:///foo")) True any(is_local(u) for u in ("s3://b", "memory://")) False
Source code in src/blobmap/backends.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
local_path ¶
local_path(url: str) -> Path
Get the absolute filesystem path a local URL or bare path refers to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
A |
required |
Returns:
| Type | Description |
|---|---|
Path
|
An absolute, expanded path. Not resolved through symlinks, so a |
Path
|
manifest directory that is a symlink stays where the user put it. |
Example
local_path("file:///srv/blobmap") PosixPath('/srv/blobmap') local_path("/srv/blobmap") PosixPath('/srv/blobmap') local_path("relative/dir").is_absolute() True
Source code in src/blobmap/backends.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
normalise ¶
normalise(url: str | Path) -> str
Turn a bare path into a URL, leaving real URLs alone.
obstore rejects a bare path with "relative URL without a base", which
says nothing useful. Accepting /foo/bar and foo/bar is what people
actually type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str | Path
|
A URL or a bare path. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A URL |
Example
normalise("/srv/blobmap") 'file:///srv/blobmap' normalise("s3://cmip6") 's3://cmip6'
Source code in src/blobmap/backends.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
open_store ¶
open_store(
url: str | Path,
options: S3Options | None = None,
*,
create: bool = False
) -> Store
Open a store from a URL or a bare filesystem path.
Bare paths are accepted and made absolute, so foo/bar, /foo/bar and
file:///foo/bar all work.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str | Path
|
A URL such as |
required |
options
|
S3Options | None
|
S3 settings that override the environment. Ignored for non-S3 schemes. |
None
|
create
|
bool
|
Create the directory if it is local and missing. Pass this for the manifest store, which blobmap owns and writes to. Do not pass it for the data store: silently creating a mistyped path would make a scan report nothing found, which looks exactly like an empty bucket. |
False
|
Returns:
| Type | Description |
|---|---|
Store
|
A storage handle. |
Raises:
| Type | Description |
|---|---|
StoreUnreachable
|
If the path does not exist and |
Example
type(open_store("memory://")).name 'MemoryStore'
Source code in src/blobmap/backends.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
diagnose ¶
diagnose(url: str, exc: BaseException) -> str
Turn a backend error into something a human can act on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The store URL that failed. |
required |
exc
|
BaseException
|
The exception raised. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A message naming the likely cause and the setting that fixes it. |
Example
"credentials" in diagnose("s3://x", RuntimeError(IMDS_HOST)) True
Source code in src/blobmap/backends.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |