diff --git a/pyproject.toml b/pyproject.toml index 08abb4a..7164d43 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -61,6 +61,9 @@ debug = [ server = [ "uvicorn" ] +gcs = [ + "gcsfs" +] [project.urls] Homepage = "https://github.com/developmentseed/titiler-xarray" diff --git a/titiler/xarray/reader.py b/titiler/xarray/reader.py index a42a2f8..30fbabf 100644 --- a/titiler/xarray/reader.py +++ b/titiler/xarray/reader.py @@ -19,6 +19,11 @@ from titiler.xarray.redis_pool import get_redis from titiler.xarray.settings import ApiSettings +try: + import gcsfs +except ImportError: # pragma: nocover + gcsfs = None # type: ignore + api_settings = ApiSettings() cache_client = get_redis() @@ -27,7 +32,7 @@ def parse_protocol(src_path: str, reference: Optional[bool] = False): """ Parse protocol from path. """ - match = re.match(r"^(s3|https|http)", src_path) + match = re.match(r"^(s3|gs|https|http)", src_path) protocol = "file" if match: protocol = match.group(0) @@ -66,6 +71,14 @@ def get_filesystem( if xr_engine == "h5netcdf" else s3fs.S3Map(root=src_path, s3=s3_filesystem) ) + elif protocol == "gs": + assert gcsfs is not None, "'gcsfs' must be installed to read dataset stored in Google Cloud Storage" + gcs_filesystem = gcsfs.GCSFileSystem() + return ( + gcs_filesystem.open(src_path) + if xr_engine == "h5netcdf" + else gcs_filesystem.get_mapper(root=src_path) + ) elif protocol == "reference": reference_args = {"fo": src_path, "remote_options": {"anon": anon}} return fsspec.filesystem("reference", **reference_args).get_mapper("")