Use GCS as backend

This page provides some examples for using Google Cloud Storage as backend.


Via Environment Variables

All config could be passed via environment variables:

  • OPENDAL_GCS_BUCKET bucket used for storing data, required
  • OPENDAL_GCS_ROOT working directory inside the bucket, default is "/"
  • OPENDAL_GCS_CREDENTIAL base64 OAUTH2 token used for authentication, required
use anyhow::Result;
use opendal::Operator;
use opendal::Scheme;

async fn main() -> Result<()> {
    // init operator from env vars
    let _op = Operator::from_env(Scheme::Gcs)?;

Via Builder

//! Example for initiating a Google Cloud Storage backend.
use std::env;

use anyhow::Result;
use log::info;
use opendal::services::gcs::Builder;
use opendal::Operator;

async fn main() -> Result<()> {
    if env::var("RUST_LOG").is_err() {
        env::set_var("RUST_LOG", "debug");

    OpenDAL GCS example.

    Available Environment Variables:
    - OPENDAL_GCS_ENDPOINT: endpoint to GCS or GCS alike services, default is "", optional
    - OPENDAL_GCS_BUCKET: bucket name, required
    - OPENDAL_GCS_ROOT: working directory of OpenDAL, default is "/", optional
    - OPENDAL_GCS_CREDENTIAL: OAUTH2 token for authentication, required

    // create builder
    let mut builder = Builder::default();

    // set the endpoint for GCS or GCS alike services
    builder.endpoint(&env::var("OPENDAL_GCS_ENDPOINT").unwrap_or_else(|_| "".to_string()));
    // set the bucket used for OpenDAL service
    builder.bucket(&env::var("OPENDAL_GCS_BUCKET").expect("env OPENDAL_GCS_BUCKET not set"));
    // set the working directory for OpenDAL service
    // could be considered as a fixed prefix for all paths you past into the backend
    builder.root(&env::var("OPENDAL_GCS_ROOT").unwrap_or_else(|_| "".to_string()));
    // OAUTH2 base64 credentials
        &env::var("OPENDAL_GCS_CREDENTIAL").expect("env OPENDAL_GCS_CREDENTIAL not set"),

    let op = Operator::new(;
    info!("operator: {:?}", op);

    let path = uuid::Uuid::new_v4().to_string();

    // Create an object handle to start operation on object.
    info!("try to write file: {}", &path);
    op.object(&path).write("Hello, world!").await?;
    info!("write file successful!");

    info!("try to read file: {}", &path);
    let content = op.object(&path).read().await?;
        "read file successful, content: {}",

    info!("try to get file metadata: {}", &path);
    let meta = op.object(&path).metadata().await?;
        "get file metadata successful, size: {}B",

    info!("try to delete file: {}", &path);
    info!("delete file successful");