cogeotiff/rio-tiler

Can't read file from private Google Cloud Storage Bucket

Closed this issue · 2 comments

Hello,
I'm trying to read a Cloud Optimized Geotiff file from a private GCS bucket using the googleapis link:

`
URL = "https://storage.googleapis.com/storage/v1/b/test-oneoftwo-prove-riotiler/o/dem_my_COG.tif?alt=media"
from rio_tiler.io import Reader

@router.get("/")
async def get_tiles():

 with Reader(URL) as cog:
     stats = cog.statistics()
     vmin = stats["b1"].model_dump()["min"]
     vmax = stats["b1"].model_dump()["max"]
     print(cog.statistics())

return {response: 'ok'} 

`

Since the bucket is private I need to access to that file using something like API Key provided by gcloud and setting the API Key as a Bearer token to the authorization header like "Authorization: Bearer [TOKEN]", otherwise I get:

httpx.HTTPStatusError: Client error '401 Unauthorized' for url 'https://storage.googleapis.com/storage/v1/b/test-riotiler/o/dem_COG.tif?alt=media'

Is there a way to pass headers to the Reader() or just authenticating to googleapis in any other way?

Thank you

🤔 the httpx error message doesn't really make sense because rio_tiler.io.Reader doesn't use httpx

about using private bucket in GCP, you can have a look at https://gdal.org/user/virtual_file_systems.html#vsigs-google-cloud-storage-files and then use https://github.com/rasterio/rasterio/blob/f424fce16e885d932bb0c9a02dbeb7d27e77b720/rasterio/session.py#L410

import rasterio
from rasterio.session import GSSession
from rio_tiler.io import Reader
with rasterio.Env(session= GSSession()):
     with Reader(URL) as cog:
         ...

🤔 the httpx error message doesn't really make sense because rio_tiler.io.Reader doesn't use httpx

about using private bucket in GCP, you can have a look at https://gdal.org/user/virtual_file_systems.html#vsigs-google-cloud-storage-files and then use https://github.com/rasterio/rasterio/blob/f424fce16e885d932bb0c9a02dbeb7d27e77b720/rasterio/session.py#L410

import rasterio
from rasterio.session import GSSession
from rio_tiler.io import Reader
with rasterio.Env(session= GSSession()):
     with Reader(URL) as cog:
         ...

Thank you @vincentsarago this was really helpful! Actually the HTTPX error came from another trial I previously did with riotiler STACRead so do not consider it.

Also, since I found out that GDAL doc may not be extremely helpful at a first glance I put my code with comment here, hoping it would be helpful for other:

   import rasterio
   from rasterio.session import GSSession
   from rio_tiler.io import Reader

   URL="gs://bucket-name/file-name.tif" # Use 'gs://... ' notation
   
    with rasterio.Env(session=GSSession()):

      # GSSession use the Google application-default-credential standard path
      # Then if you are locally developing it would be helpful just like any other implementation
      # of gcloud api 

        with Reader(URL) as cog:
            stats = cog.statistics()
            print(stats)