Use fs as backend

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

We can run this example via:

cargo run --example fs


Via Environment

All config could be passed via environment:

  • OPENDAL_FS_ROOT: root path, default: /tmp
use anyhow::Result;
use opendal::Operator;
use opendal::Scheme;

async fn main() -> Result<()> {
    // Init Operator from env.
    let op = Operator::from_env(Scheme::Fs)?;

Via Builder

//! Example for initiating a fs backend.

use std::env;

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

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

        r#"OpenDAL fs Example.

Available Environment Values:

- OPENDAL_FS_ROOT: root path, default: /tmp

    // Create fs backend builder.
    let mut builder: Builder = fs::Builder::default();
    // Set the root for fs, all operations will happen under this root.
    // NOTE: the root must be absolute path.
    builder.root(&env::var("OPENDAL_FS_ROOT").unwrap_or_else(|_| "/tmp".to_string()));

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