minio 0.4.0

MinIO SDK for Amazon S3 compatible object storage access
Documentation
// MinIO Rust Library for Amazon S3 Compatible Cloud Storage
// Copyright 2025 MinIO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

mod common;

use crate::common::create_bucket_if_not_exists;
use minio::s3::MinioClient;
use minio::s3::creds::StaticProvider;
use minio::s3::http::BaseUrl;
use minio::s3::response::{AppendObjectResponse, StatObjectResponse};
use minio::s3::response_traits::HasObjectSize;
use minio::s3::segmented_bytes::SegmentedBytes;
use minio::s3::types::{BucketName, ObjectKey, S3Api};
use rand::Rng;
use rand::distr::Alphanumeric;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    env_logger::init(); // Note: set environment variable RUST_LOG="INFO" to log info and higher

    let base_url = "http://localhost:9000/".parse::<BaseUrl>()?;
    let static_provider = StaticProvider::new("minioadmin", "minioadmin", None);
    let client = MinioClient::new(base_url, Some(static_provider), None, None)?;

    if !client.is_minio_express().await {
        println!("Need (MinIO) Express mode to run this example");
        return Ok(());
    }

    let bucket = BucketName::new("append-test-bucket").unwrap();
    create_bucket_if_not_exists(bucket.as_str(), &client).await?;

    let object = ObjectKey::new("append-test-object").unwrap();

    let n_segments = 1000;
    let segment_size = 1024 * 1024; // 1 KB
    let mut offset_bytes = 0;

    for i in 0..n_segments {
        let rand_str: String = random_string(segment_size);

        let data_size = rand_str.len() as u64;
        let data: SegmentedBytes = SegmentedBytes::from(rand_str);

        let resp: AppendObjectResponse = client
            .append_object(&bucket, &object, data, offset_bytes)?
            .build()
            .send()
            .await?;

        offset_bytes += data_size;
        if resp.object_size() != offset_bytes {
            panic!(
                "from the append_object: size mismatch: expected {}, got {offset_bytes}",
                resp.object_size(),
            )
        }
        //println!("Append response: {resp:#?}");

        let resp: StatObjectResponse = client.stat_object(&bucket, &object)?.build().send().await?;
        if resp.size()? != offset_bytes {
            panic!(
                "from the stat_Object: size mismatch: expected {}, got {offset_bytes}",
                resp.size()?,
            )
        }
        println!("{i}/{n_segments}");
        //println!("Stat response: {resp:#?}");
    }

    Ok(())
}

fn random_string(len: usize) -> String {
    rand::rng()
        .sample_iter(&Alphanumeric)
        .take(len)
        .map(char::from)
        .collect()
}