Use rocksdb as backend

These docs provide a detailed examples for using rocksdb as backend.

We can run this example via:

OPENDAL_ROCKSDB_DATADIR=/tmp/rocksdb cargo run --example=rocksdb --features=services-rocksdb


Via Environment

All config could be passed via environment:

  • OPENDAL_ROCKSDB_DATADIR the path to the rocksdb data directory (required)
  • OPENDAL_ROCKSDB_ROOT working directory of opendal, default is /
use anyhow::Result;
use opendal::Object;
use opendal::Operator;
use opendal::Scheme;

async fn main() -> Result<()> {
    let op = Operator::from_env(Scheme::Rocksdb)?;

    // create an object handler to start operation on rocksdb!
    let _op: Object = op.object("hello_rocksdb!");


Via Builder

//! example for initiating a Rocksdb backend

use std::env;

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

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

        Available Environment Variables:

        - OPENDAL_ROCKSDB_DATADIR: the path to the rocksdb data directory (required)
        - OPENDAL_ROCKSDB_ROOT:    working directory of opendal, default is "/"

    // Create rocksdb backend builder
    let mut builder: Builder = rocksdb::Builder::default();

    // Set the root, all operations will happen under this directory, or prefix, more accurately.
    // NOTE: the root must be absolute path
    builder.root(&env::var("OPENDAL_ROCKSDB_ROOT").unwrap_or_else(|_| "/".to_string()));

    // Set the path to the rocksdb data directory
        &env::var("OPENDAL_ROCKSDB_DATADIR").expect("env OPENDAL_ROCKSDB_DATADIR is not set"),

    // `Accessor` provides the low level APIs, we will use `Operator` normally.
    let op: Operator = Operator::new(;

    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");