object-storage

Crates.ioobject-storage
lib.rsobject-storage
version0.1.6
sourcesrc
created_at2023-02-14 02:28:13.434029
updated_at2023-02-25 09:45:10.44716
descriptionThis library is used to manipulate oss or cos
homepage
repositoryhttps://github.com/joeywang321/object-storage
max_upload_size
id784479
size17,084
Dane Wang (danewalters)

documentation

https://docs.rs/object-storage

README

object storage

The goal of this library is to make a library that can use both "Aliyun-oss" and "Tencent Cloud-cos", simplifying the steps of use and allowing development to focus on business logic.

oss

oss bucket list

use object_storage::oss::Auth;

#[tokio::main]
async fn main() {
    let auth = Auth::new("your aliyun oss AccessKeyId", "your aliyun oss AccessKeySecret").await;
    let buck_list = auth.list_bucket().await.unwarp();
    println!("{:#?}", buck_list);
}

oss put object

Simple general upload of a single file

use object_storage::oss::{Auth,Bucket};

#[tokio::main]
async fn main() {
    let auth = Auth::new("your aliyun oss AccessKeyId", "your aliyun oss AccessKeySecret").await;
    let bucket = Bucket::new(auth, "your endpoint", "your bucket name").await.unwrap();
    let object_url = bucket.read_put_object("path").await.unwarp();
}

tokio

Uploading multiple files at the same time using tokio

use object_storage::oss::{Auth, Bucket};
use std::sync::Arc;
use tokio::sync::Mutex;

#[tokio::main]
async fn main() {
    let auth = Auth::new("your aliyun oss AccessKeyId", "your aliyun oss AccessKeySecret").await;
    let bucket = Arc::new(Mutex::new(Bucket::new(auth, "your endpoint", "your bucket name").await.unwrap()));

    let mut handles = Vec::new();

    handles.push(tokio::spawn({
        let bucket = bucket.clone();
        async move {
            let guard = bucket.lock().await;
            guard.read_put_object("/Users/joeywang/Downloads/1.jpg").await
        }
    }));
    handles.push(tokio::spawn({
        let bucket = bucket.clone();
        async move {
            let guard = bucket.lock().await;
            guard.read_put_object("/Users/joeywang/Downloads/2.jpg").await
        }
    }));
    handles.push(tokio::spawn({
        let bucket = bucket.clone();
        async move {
            let guard = bucket.lock().await;
            guard.read_put_object("/Users/joeywang/Downloads/3.jpg").await
        }
    }));
    handles.push(tokio::spawn({
        let bucket = bucket.clone();
        async move {
            let guard = bucket.lock().await;
            guard.read_put_object("/Users/joeywang/Downloads/4.png").await
        }
    }));
    handles.push(tokio::spawn({
        let bucket = bucket.clone();
        async move {
            let guard = bucket.lock().await;
            guard.read_put_object("/Users/joeywang/Downloads/5.png").await
        }
    }));
    handles.push(tokio::spawn({
        let bucket = bucket.clone();
        async move {
            let guard = bucket.lock().await;
            guard.read_put_object("/Users/joeywang/Downloads/6.png").await
        }
    }));
    handles.push(tokio::spawn({
        let bucket = bucket.clone();
        async move {
            let guard = bucket.lock().await;
            guard.read_put_object("/Users/joeywang/Downloads/7.jpg").await
        }
    }));
    handles.push(tokio::spawn({
        let bucket = bucket.clone();
        async move {
            let guard = bucket.lock().await;
            guard.read_put_object("/Users/joeywang/Downloads/8.jpg").await
        }
    }));

    for handle in handles {
        match handle.await {
            Ok(result) => {
                match result {
                    Ok(url) => println!("Upload succeeded: {}", url),
                    Err(e) => eprintln!("Upload failed: {}", e),
                }
            },
            Err(e) => eprintln!("Failed to spawn task: {}", e),
        }
    }
}
Commit count: 9

cargo fmt