Skip to content

Google Cloud Storage

Getting Started

To use Google Cloud Storage as a data source, you will need to install the google-cloud-storage Python package. You can install this package using pip:

pip install google-cloud-storage

Authentication

The easiest way to authenticate with Google Cloud Storage is to use Application Default Credentials. If you are running marimo on Google Cloud and your resource has a service account attached, then Application Default Credentials will automatically be used. If you are running marimo locally, you can authenticate with Application Default Credentials by running the following command:

gcloud auth application-default login

Service Account Key File

To authenticate with Google Cloud Storage, you will need to create a service account and download the service account key file. You can create a service account and download the key file by following the instructions here.

Once you have downloaded the key file, you can authenticate with Google Cloud Storage by setting the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of the key file:

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key/file.json

Reading Data

To read data from Google Cloud Storage, you will need to create a StorageClient object. You can then use this object to read data from Google Cloud Storage.

# Cell 1 - Load libraries
import marimo as mo
from google.cloud import storage

# Cell 2 - Load buckets
client = storage.Client()
buckets = client.list_buckets()

# Cell 3 - Select bucket
selected_bucket = mo.ui.dropdown(
    label="Select bucket", options=[b.name for b in buckets]
)
selected_bucket

# Cell 4 - Load files
files = list(bucket.list_blobs())
items = [
    {
        "Name": f.name,
        "Updated": f.updated.strftime("%h %d, %Y"),
        "Size": f.size,
    }
    for f in files
]
file_table = mo.ui.table(items, selection="single")
file_table if items else mo.md("No files found").callout()

Example

Check out our full example using Google Cloud Storage here

Or run it yourself:

marimo run https://raw.githubusercontent.com/marimo-team/marimo/main/examples/cloud/gcp/google_cloud_storage.py