minio-rs/examples/append_object.rs
Henk-Jan Lebbink 58d9203153
refactored all functions (#145)
* refactored stat_object

refactored select_object_content

refactor get_presigned_object_url

refactor get_presigned_policy_form_data

refactored upload-part-copy

* fixed object.unwrap

* update region

* made client Arc

* made client Arc

* update client

* update tests

* update segmented_bytes

* bench updated

* cleanup version handling

* cleanup of headers: multimap

* added inner in Client

* updated clients: added Into<String>in API

* Separated http_client and shared client items in Client
2025-04-23 10:18:18 -07:00

86 lines
2.8 KiB
Rust

// 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, create_client_on_localhost};
use minio::s3::Client;
use minio::s3::response::{AppendObjectResponse, StatObjectResponse};
use minio::s3::segmented_bytes::SegmentedBytes;
use minio::s3::types::S3Api;
use rand::Rng;
use rand::distributions::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 client: Client = create_client_on_localhost()?;
if !client.is_minio_express() {
println!("Need (MinIO) Express mode to run this example");
return Ok(());
}
let bucket_name: &str = "append-test-bucket";
create_bucket_if_not_exists(bucket_name, &client).await?;
let object_name: &str = "append-test-object";
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_name, object_name, data, offset_bytes)
.send()
.await?;
offset_bytes += data_size;
if resp.object_size != offset_bytes {
panic!(
"from the append_object: size mismatch: expected {}, got {}",
resp.object_size, offset_bytes
)
}
//println!("Append response: {:#?}", resp);
let resp: StatObjectResponse = client.stat_object(bucket_name, object_name).send().await?;
if resp.size != offset_bytes {
panic!(
"from the stat_Object: size mismatch: expected {}, got {}",
resp.size, offset_bytes
)
}
println!("{}/{}", i, n_segments);
//println!("Stat response: {:#?}", resp);
}
Ok(())
}
fn random_string(len: usize) -> String {
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(len)
.map(char::from)
.collect()
}