garage-sdk 0.1.1

Async Rust SDK for Garage S3-compatible storage with uploads and public URL generation
Documentation
//! Example usage of garage-sdk
//!
//! Run with:
//! ```bash
//! export GARAGE_ENDPOINT="https://s3.example.com"
//! export GARAGE_BUCKET="my-bucket"
//! export GARAGE_PUBLIC_URL="https://cdn.example.com"
//! export AWS_ACCESS_KEY_ID="your-access-key"
//! export AWS_SECRET_ACCESS_KEY="your-secret-access-key"
//! cargo run --features example
//! ```

use anyhow::{Context, Result};
use garage_sdk::{GarageUploader, UploaderConfig};
use tracing::{error, info};

#[tokio::main]
async fn main() -> Result<()> {
    // Initialize tracing for debug output (optional)
    tracing_subscriber::fmt::init();

    let config =
        UploaderConfig::from_env().context("failed to build uploader config from environment")?;
    let uploader = GarageUploader::new(config).context("failed to initialize uploader")?;

    // Example 1: Upload local file (optional)
    match std::env::var("GARAGE_EXAMPLE_FILE") {
        Ok(path) => {
            info!(path = %path, "uploading local file");
            match uploader.upload_from_path(&path).await {
                Ok(result) => {
                    info!(
                        public_url = %result.public_url,
                        key = %result.key,
                        size = result.size,
                        content_type = %result.content_type,
                        "uploaded local file"
                    );
                }
                Err(e) => {
                    error!(error = %e, "failed to upload local file");
                }
            }
        }
        Err(_) => {
            info!("set GARAGE_EXAMPLE_FILE to upload a local file");
        }
    }

    // Example 2: Upload from remote URL (optional)
    match std::env::var("GARAGE_EXAMPLE_URL") {
        Ok(remote_url) => {
            info!(url = %remote_url, "uploading from URL");
            match uploader.upload_from_url(&remote_url).await {
                Ok(result) => {
                    info!(
                        public_url = %result.public_url,
                        key = %result.key,
                        size = result.size,
                        content_type = %result.content_type,
                        "uploaded from URL"
                    );
                }
                Err(e) => {
                    error!(error = %e, "failed to upload from URL");
                }
            }
        }
        Err(_) => {
            info!("set GARAGE_EXAMPLE_URL to upload from a remote URL");
        }
    }

    // Example 3: Upload raw bytes
    info!("uploading raw bytes");
    let sample_json = r#"{"message": "Hello, World!"}"#;
    match uploader
        .upload_bytes(
            sample_json.as_bytes().to_vec(),
            "application/json",
            Some("json"),
        )
        .await
    {
        Ok(result) => {
            info!(
                public_url = %result.public_url,
                key = %result.key,
                size = result.size,
                "uploaded raw bytes"
            );
        }
        Err(e) => {
            error!(error = %e, "failed to upload bytes");
        }
    }

    Ok(())
}