# first suppress noisy warnings
import warnings
import re
# Suppress the specific GCP user credential quota project warning
warnings.filterwarnings("ignore",
="Your application has authenticated using end user credentials from Google Cloud SDK without a quota project.",
message=UserWarning,
category="google.auth._default"
module )
Accessing Google Cloud Storage
📘 Learning Objectives
- Access a file on Google Cloud Storage (GCS)
- Plot it.
- Access when authentication is needed.
Access a public file
You need to know the bucket url and prefix that with gcs
.
import xarray as xr
import fsspec
= "gcs://nmfs_odp_nwfsc/CB/nwm_daily_means/wr1718/streamflow/netcdf/daily_mean_2018.nc"
url = fsspec.filesystem("gcs", anon=True)
fs = fs.open(url, mode="rb")
f = xr.open_dataset(f) ds
# Plot
"streamflow"].isel(feature_id=1).plot(); ds[
# close the file when done
f.close()
Accessing a private bucket
Run the following from the terminal. This will save your authentication key to
~/.config/gcloud/application_default_credentials.json
gcloud auth application-default login
# Now import fsspec
import xarray as xr
# Use authenticated GCS access (anon=False)
= fsspec.filesystem("gcs", anon=False)
fs
# Path to the private object in GCS
= "gcs://nmfs_odp_nwfsc/CB/nwm_daily_means/wr1718/streamflow/netcdf/daily_mean_2018.nc"
url
# Open and read with xarray
with fs.open(url, mode="rb") as f:
= xr.open_dataset(f) ds
/srv/conda/envs/notebook/lib/python3.12/site-packages/google/auth/_default.py:76: UserWarning: Your application has authenticated using end user credentials from Google Cloud SDK without a quota project. You might receive a "quota exceeded" or "API not enabled" error. See the following page for troubleshooting: https://cloud.google.com/docs/authentication/adc-troubleshooting/user-creds.
warnings.warn(_CLOUD_SDK_CREDENTIALS_WARNING)