目录

Volcengine Object Storage(TOS) Rust SDK

```rust use async_trait::async_trait; use futures_core::future::BoxFuture; use std::env; use std::future::Future; use std::time::Duration; use tokio::runtime::Handle; use ve_tos_rust_sdk::common::init_tracing_log; use ve_tos_rust_sdk::asynchronous::bucket::BucketAPI; use ve_tos_rust_sdk::asynchronous::tos; use ve_tos_rust_sdk::asynchronous::tos::AsyncRuntime; use ve_tos_rust_sdk::bucket::ListBucketsInput;

[derive(Debug, Default)]

pub struct TokioRuntime {}

[async_trait]

impl AsyncRuntime for TokioRuntime { type JoinError = tokio::task::JoinError; async fn sleep(&self, duration: Duration) { tokio::time::sleep(duration).await; }

fn spawn<'a, F>(&self, future: F) -> BoxFuture<'a, Result<F::Output, Self::JoinError>>
where
    F: Future + Send + 'static,
    F::Output: Send + 'static,
{
    Box::pin(Handle::current().spawn(future))
}

fn block_on<F: Future>(&self, future: F) -> F::Output {
    Handle::current().block_on(future)
}

}

[tokio::main]

async fn main() { // init log let _guard = init_tracing_log(“info”, “temp/logs”, “app.log”);

let ak = env::var("TOS_ACCESS_KEY").unwrap_or("".to_string());
let sk = env::var("TOS_SECRET_KEY").unwrap_or("".to_string());
let endpoint = "https://tos-cn-beijing.volces.com";
let region = "cn-beijing";
let client = tos::builder::<TokioRuntime>()
    .connection_timeout(3000)
    .request_timeout(10000)
    .max_retry_count(3)
    .ak(ak)
    .sk(sk)
    .region(region)
    .endpoint(endpoint)
    .build().unwrap();

let output = client.list_buckets(&ListBucketsInput::new()).await;
match output {
    Ok(output) => {
        println!("request_id: {}", output.request_id());
        for bucket in output.buckets() {
            println!("{}", bucket.name());
        }
    }
    Err(err) => {
        match err {
            TosError::TosClientError { message, .. } => {
                println!("message: {}", message);
            }
            TosError::TosServerError { request_id, ec, .. } => {
                println!("request_id: {}, ec: {}", request_id, ec);
            }
        }
    }
}

}

关于
587.0 KB
邀请码