Skip to content

Commit 4df68b5

Browse files
committed
feat: get_obstore_store function for creating Obstore store with Open PC credentials
1 parent 0c1d784 commit 4df68b5

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

planetary_computer/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
)
1414
from planetary_computer.settings import set_subscription_key
1515
from planetary_computer._adlfs import get_adlfs_filesystem, get_container_client
16+
from planetary_computer._obstore import get_obstore_store
1617

1718
from planetary_computer.version import __version__
1819

1920
__all__ = [
2021
"get_adlfs_filesystem",
2122
"get_container_client",
23+
"get_obstore_store",
2224
"set_subscription_key",
2325
"sign_asset",
2426
"sign_assets",

planetary_computer/_obstore.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
from planetary_computer.sas import get_token
6+
7+
if TYPE_CHECKING:
8+
import sys
9+
10+
from obstore.store import (
11+
AzureStore,
12+
AzureConfig,
13+
ClientConfig,
14+
RetryConfig,
15+
AzureSASToken,
16+
)
17+
18+
if sys.version_info >= (3, 11):
19+
from typing import Unpack
20+
else:
21+
from typing_extensions import Unpack
22+
23+
24+
def get_obstore_store( # type: ignore[misc] # overlap with kwargs
25+
account_name: str,
26+
container_name: str,
27+
*,
28+
prefix: str | None = None,
29+
config: AzureConfig | None = None,
30+
client_options: ClientConfig | None = None,
31+
retry_config: RetryConfig | None = None,
32+
**kwargs: Unpack[AzureConfig], # type: ignore # noqa: PGH003 (container_name key overlaps with positional arg)
33+
) -> AzureStore:
34+
try:
35+
import obstore
36+
except ImportError as e:
37+
raise ImportError(
38+
"'planetary_computer.get_obstore_store' requires "
39+
"the optional dependency 'obstore'."
40+
) from e
41+
42+
def credential_provider() -> AzureSASToken:
43+
token = get_token(account_name, container_name)
44+
return {
45+
"sas_token": token.token,
46+
"expires_at": token.expiry,
47+
}
48+
49+
return obstore.store.AzureStore(
50+
account_name=account_name,
51+
container_name=container_name,
52+
prefix=prefix,
53+
config=config,
54+
client_options=client_options,
55+
retry_config=retry_config,
56+
credential_provider=credential_provider,
57+
**kwargs, # type: ignore # (container_name key overlaps with positional arg)
58+
)

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ dev = [
3434
"pytest",
3535
"responses",
3636
]
37+
obstore = ["obstore"]
3738

3839
[project.scripts]
3940
planetarycomputer = "planetary_computer.scripts.cli:app"
@@ -42,4 +43,4 @@ planetarycomputer = "planetary_computer.scripts.cli:app"
4243
include-package-data = false
4344

4445
[tool.setuptools.dynamic]
45-
version = {attr = "planetary_computer.version.__version__"}
46+
version = {attr = "planetary_computer.version.__version__"}

0 commit comments

Comments
 (0)