Skip to content

Description:

  • List objects from google cloud blob storage

Resources:

This notebook reads its settings from environment variables. Set them before starting Jupyter:

  • GOOGLE_APPLICATION_CREDENTIALS: path to a Google Cloud service-account JSON key file
  • GCS_PROJECT: Google Cloud project ID
  • GCS_BUCKET: name of a Cloud Storage bucket in that project

Example 2 reads an object named example.jpeg from the bucket.

Listing objects in a bucket

import os
import gcsfs
gcsfs_creds = os.environ["GOOGLE_APPLICATION_CREDENTIALS"] # path to a service-account JSON key file
gcsfs_project = os.environ["GCS_PROJECT"]
gcsfs_bucket = os.environ["GCS_BUCKET"]
fs = gcsfs.GCSFileSystem(project=gcsfs_project, token=gcsfs_creds)
fs.ls(gcsfs_bucket)
# Note: the above is equivalent to fsspec.filesystem('gs', ...), which returns the same GCSFileSystem:
# from fsspec import filesystem
# fs = filesystem("gs", project=gcsfs_project, token=gcsfs_creds)
# fs.ls(gcsfs_bucket)

Read an object from google cloud blob storage

import os
import gcsfs
gcsfs_creds = os.environ["GOOGLE_APPLICATION_CREDENTIALS"] # path to a service-account JSON key file
gcsfs_project = os.environ["GCS_PROJECT"]
gcsfs_bucket = os.environ["GCS_BUCKET"]
fs = gcsfs.GCSFileSystem(project=gcsfs_project, token=gcsfs_creds)
with fs.open(f'{gcsfs_bucket}/example.jpeg', 'rb') as f:
print(f.read())

ganymede is made by Eugo