Skip to content

Commit 973080f

Browse files
authored
fix: load S3 credentials and pass to openers in compatibility endpoint (#131)
resolves #130
1 parent 4d131bd commit 973080f

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

tests/test_compatibility.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,4 @@ def test_both_fail(self, mock_xarray, mock_rasterio, mock_get_umm):
289289
evaluate_concept_compatibility("C1234-TEST", mock_request, mock_auth)
290290

291291
assert exc_info.value.status_code == 400
292-
assert "cannot parse concept_id" in exc_info.value.detail
292+
assert "Could not open a sample granule" in exc_info.value.detail

titiler/cmr/compatibility.py

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
from typing import Any, Dict, List, Literal, Optional
44

55
import numpy as np
6+
import rasterio
67
from fastapi import HTTPException
78
from pydantic import BaseModel
9+
from rasterio.session import AWSSession
810
from rio_tiler.constants import WEB_MERCATOR_TMS
911
from rio_tiler.io.rasterio import Reader
1012
from rio_tiler.models import Info
1113
from starlette.requests import Request
14+
from titiler.xarray.io import Reader as XarrayReader
1215

1316
from titiler.cmr.backend import CMRBackend
1417
from titiler.cmr.dependencies import ConceptID
1518
from titiler.cmr.logger import logger
1619
from titiler.cmr.reader import xarray_open_dataset
1720
from titiler.cmr.settings import AuthSettings
1821
from titiler.cmr.utils import get_concept_id_umm
19-
from titiler.xarray.io import Reader as XarrayReader
2022

2123

2224
class VariableInfo(BaseModel):
@@ -201,9 +203,28 @@ def evaluate_xarray_compatibility(
201203
if not assets:
202204
raise ValueError("No assets found for XarrayReader")
203205

204-
with xarray_open_dataset(assets[0]["url"], auth=auth) as ds:
206+
asset = assets[0]
207+
208+
s3_credentials = (
209+
src_dst.get_s3_credentials(endpoint)
210+
if src_dst.get_s3_credentials
211+
and (endpoint := asset.get("s3_credentials_url", None))
212+
else None
213+
)
214+
215+
with xarray_open_dataset(
216+
asset["url"],
217+
auth=auth,
218+
s3_credentials={
219+
"key": s3_credentials["accessKeyId"],
220+
"secret": s3_credentials["secretAccessKey"],
221+
"token": s3_credentials["sessionToken"],
222+
}
223+
if s3_credentials
224+
else None,
225+
) as ds:
205226
result = extract_xarray_metadata(ds)
206-
result["example_assets"] = assets[0]["url"]
227+
result["example_assets"] = asset["url"]
207228
return result
208229

209230

@@ -250,11 +271,33 @@ def evaluate_rasterio_compatibility(
250271
if not assets:
251272
raise ValueError("No assets found for MultiFilesBandsReader")
252273

274+
s3_credentials = (
275+
src_dst.get_s3_credentials(endpoint)
276+
if src_dst.get_s3_credentials
277+
and (endpoint := assets[0].get("s3_credentials_url", None))
278+
else None
279+
)
280+
253281
example_assets: Dict[str, str] = assets[0]["url"]
254282

255-
with src_dst.reader(
256-
input=list(example_assets.values())[0], tms=src_dst.tms
257-
) as _src_dst:
283+
session = (
284+
AWSSession(
285+
aws_access_key_id=s3_credentials["accessKeyId"],
286+
aws_secret_access_key=s3_credentials["secretAccessKey"],
287+
aws_session_token=s3_credentials["sessionToken"],
288+
)
289+
if s3_credentials
290+
else None
291+
)
292+
with (
293+
rasterio.Env(
294+
session,
295+
),
296+
src_dst.reader(
297+
input=list(example_assets.values())[0],
298+
tms=src_dst.tms,
299+
) as _src_dst,
300+
):
258301
info = _src_dst.info()
259302

260303
return {
@@ -298,6 +341,7 @@ def evaluate_concept_compatibility(
298341
**result,
299342
)
300343
except (ValueError, HTTPException, OSError, KeyError) as e:
344+
xarray_error = e
301345
logger.warning(f"XarrayReader failed: {e}")
302346

303347
# Fall back to rasterio
@@ -309,7 +353,13 @@ def evaluate_concept_compatibility(
309353
**result,
310354
)
311355
except (ValueError, HTTPException, OSError, KeyError) as e:
356+
rasterio_error = e
312357
logger.warning(f"MultiFilesBandsReader failed: {e}")
313358

314359
# Both failed
315-
raise HTTPException(400, f"cannot parse concept_id {concept_id}")
360+
raise HTTPException(
361+
400,
362+
f"Could not open a sample granule for concept_id {concept_id} "
363+
"with either the rasterio or xarray backends.\n\n "
364+
f"xarray error: {xarray_error} \n\n rasterio_error: {rasterio_error}",
365+
)

0 commit comments

Comments
 (0)