Skip to content

Commit 9a4962e

Browse files
Refactor S3 JSON metadata handling: replace direct S3 file read with a dedicated read_s3_json_metadata function for improved clarity and reusability.
1 parent 3c284c6 commit 9a4962e

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

eopf_geozarr/conversion/geozarr.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -695,12 +695,7 @@ def create_geozarr_compliant_multiscales(
695695
# Handle S3 vs local paths for JSON metadata
696696
if s3_utils.is_s3_path(output_path):
697697
# For S3, use s3fs with proper configuration
698-
import s3fs
699-
storage_options = s3_utils.get_s3_storage_options(zarr_json_path)
700-
fs = s3fs.S3FileSystem(**storage_options)
701-
702-
with fs.open(zarr_json_path, "r") as f:
703-
zarr_json = json.load(f)
698+
zarr_json = s3_utils.read_s3_json_metadata(zarr_json_path)
704699

705700
zarr_json["attributes"]["multiscales"] = {
706701
"tile_matrix_set": tile_matrix_set,

eopf_geozarr/conversion/s3_utils.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,44 @@ def write_s3_json_metadata(s3_path: str, metadata: Dict[str, Any], **s3_kwargs)
149149
with fs.open(s3_path, "w") as f:
150150
f.write(json_content)
151151

152+
def read_s3_json_metadata(s3_path: str, **s3_kwargs) -> Dict[str, Any]:
153+
"""
154+
Read JSON metadata from S3.
155+
156+
Parameters
157+
----------
158+
s3_path : str
159+
S3 path for the JSON file
160+
**s3_kwargs
161+
Additional keyword arguments for s3fs.S3FileSystem
162+
163+
Returns
164+
-------
165+
dict
166+
Parsed JSON metadata
167+
"""
168+
# Set up default S3 configuration
169+
default_s3_kwargs = {
170+
"anon": False,
171+
"use_ssl": True,
172+
"asynchronous": False, # Force synchronous mode
173+
"client_kwargs": {
174+
"region_name": os.environ.get("AWS_DEFAULT_REGION", "us-east-1")
175+
}
176+
}
177+
178+
# Add custom endpoint support (e.g., for OVH Cloud)
179+
if "AWS_S3_ENDPOINT" in os.environ:
180+
default_s3_kwargs["endpoint_url"] = os.environ["AWS_S3_ENDPOINT"]
181+
default_s3_kwargs["client_kwargs"]["endpoint_url"] = os.environ["AWS_S3_ENDPOINT"]
182+
183+
s3_config = {**default_s3_kwargs, **s3_kwargs}
184+
fs = s3fs.S3FileSystem(**s3_config)
185+
186+
with fs.open(s3_path, "r") as f:
187+
content = f.read()
188+
189+
return json.loads(content)
152190

153191
def s3_path_exists(s3_path: str, **s3_kwargs) -> bool:
154192
"""

0 commit comments

Comments
 (0)