Azblob

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

This example has native support for Azure Storage Simulator Azurite. All value will fall back to Azurite default settings.

We can start a mock services like:

docker run -p 10000:10000 mcr.microsoft.com/azure-storage/azurite
az storage container create --name test --connection-string "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;"

Then start our azblob examples

OPENDAL_AZBLOB_CONTAINER=test cargo run --example azblob

Example

Via Environment

All config could be passed via environment:

  • OPENDAL_AZBLOB_ROOT: root path, default: /
  • OPENDAL_AZBLOB_CONTAINER: container name
  • OPENDAL_AZBLOB_ENDPOINT: endpoint of your container
  • OPENDAL_AZBLOB_ACCOUNT_NAME: account name
  • OPENDAL_AZBLOB_ACCOUNT_KEY: account key
use anyhow::Result;
use opendal::Operator;
use opendal::Scheme;

#[tokio::main]
async fn main() -> Result<()> {
    // Init Operator from env.
    let op = Operator::from_env(Scheme::Azblob)?;
}

Via Builder

//! Example for initiating a azblob backend.

use std::env;

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

#[tokio::main]
async fn main() -> Result<()> {
    if env::var("RUST_LOG").is_err() {
        env::set_var("RUST_LOG", "debug");
    }
    env_logger::init();

    println!(
        r#"OpenDAL azblob example.

Available Environment Values:

- OPENDAL_AZBLOB_ROOT: root path, default: /
- OPENDAL_AZBLOB_CONTAINER: container name
- OPENDAL_AZBLOB_ENDPOINT: endpoint of your container
- OPENDAL_AZBLOB_ACCOUNT_NAME: account name
- OPENDAL_AZBLOB_ACCOUNT_KEY: account key

    "#
    );

    // Create fs backend builder.
    let mut builder: Builder = azblob::Builder::default();
    // Set the root, all operations will happen under this root.
    //
    // NOTE: the root must be absolute path.
    builder.root(&env::var("OPENDAL_AZBLOB_ROOT").unwrap_or_else(|_| "/".to_string()));
    // Set the container name
    builder.container(
        &env::var("OPENDAL_AZBLOB_CONTAINER").expect("env OPENDAL_AZBLOB_CONTAINER not set"),
    );
    // Set the endpoint
    //
    // For examples:
    // - "http://127.0.0.1:10000/devstoreaccount1"
    // - "https://accountname.blob.core.windows.net"
    builder.endpoint(
        &env::var("OPENDAL_AZBLOB_ENDPOINT")
            .unwrap_or_else(|_| "http://127.0.0.1:10000/devstoreaccount1".to_string()),
    );
    // Set the account_name and account_key.
    builder.account_name(
        &env::var("OPENDAL_AZBLOB_ACCOUNT_NAME").unwrap_or_else(|_| "devstoreaccount1".to_string()),
    );
    builder.account_key(
        &env::var("OPENDAL_AZBLOB_ACCOUNT_KEY")
            .unwrap_or_else(|_| "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==".to_string()),
    );

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

    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?;
    info!(
        "read file successful, content: {}",
        String::from_utf8_lossy(&content)
    );

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

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

    Ok(())
}