aliyun-oss-client

aliyun OSS 的一个客户端

最开始的时候,是作为一个 rust 练手项目,渐渐的现在越来越完善了,包含了 rust 的 struct,enum, async, trait features 等特性, 以及自定义 error 类,同时也包含完整的的测试用例

使用方法

  1. 在自己的项目里添加如下依赖项 (项目遵循语义化版本规则,请放心使用)

toml [dependencies] aliyun-oss-client = "^0.8"

  1. 打开你需要使用 oss 的文件,在里面添加如下内容,即可使用:

rust use std::env::set_var; set_var("ALIYUN_KEY_ID", "foo1"); set_var("ALIYUN_KEY_SECRET", "foo2"); set_var("ALIYUN_ENDPOINT", "qingdao"); set_var("ALIYUN_BUCKET", "foo4"); let client = aliyun_oss_client::Client::from_env();

或者

rust use aliyun_oss_client::BucketName; let bucket = BucketName::new("bbb").unwrap(); let client = aliyun_oss_client::Client::new("key1".into(),"secret1".into(),"qingdao".try_into().unwrap(), bucket);

异步

查询所有的 bucket 信息

```rust

#[tokio::main]

async fn main(){

# use std::env::set_var;
# set_var("ALIYUN_KEY_ID", "foo1");
# set_var("ALIYUN_KEY_SECRET", "foo2");
# set_var("ALIYUN_ENDPOINT", "qingdao");
# set_var("ALIYUN_BUCKET", "foo4");
# let client = aliyun_oss_client::Client::from_env().unwrap();
client.get_bucket_list().await;

}

```

获取 bucket 信息

```rust

#[tokio::main]

async fn main(){

# use std::env::set_var;
# set_var("ALIYUN_KEY_ID", "foo1");
# set_var("ALIYUN_KEY_SECRET", "foo2");
# set_var("ALIYUN_ENDPOINT", "qingdao");
# set_var("ALIYUN_BUCKET", "foo4");
# let client = aliyun_oss_client::Client::from_env().unwrap();
let response = client.get_bucket_info().await;
println!("bucket info: {:?}", response);

}

```

查询当前 bucket 中的 object 列表

```rust

#[tokio::main]

async fn main(){

# use std::env::set_var;
# set_var("ALIYUN_KEY_ID", "foo1");
# set_var("ALIYUN_KEY_SECRET", "foo2");
# set_var("ALIYUN_ENDPOINT", "qingdao");
# set_var("ALIYUN_BUCKET", "foo4");
# let client = aliyun_oss_client::Client::from_env().unwrap();
use aliyun_oss_client::Query;
let query = Query::new();
let response = client.get_object_list(query).await;
println!("objects list: {:?}", response);

}

```

也可以使用 bucket struct 查询 object 列表

```ignore

#[tokio::main]

async fn main(){

# use std::env::set_var;
# set_var("ALIYUN_KEY_ID", "foo1");
# set_var("ALIYUN_KEY_SECRET", "foo2");
# set_var("ALIYUN_ENDPOINT", "qingdao");
# set_var("ALIYUN_BUCKET", "foo4");
# let client = aliyun_oss_client::Client::from_env().unwrap();
use aliyun_oss_client::Query;
let mut query = Query::new();
query.insert("max-keys", "5");
query.insert("prefix", "babel");

let result = client.get_bucket_info().await.unwrap().get_object_list(query).await;

println!("object list : {:?}", result);

}

```

上传文件

```rust

#[tokio::main]

async fn main(){

# use std::env::set_var;
# set_var("ALIYUN_KEY_ID", "foo1");
# set_var("ALIYUN_KEY_SECRET", "foo2");
# set_var("ALIYUN_ENDPOINT", "qingdao");
# set_var("ALIYUN_BUCKET", "foo4");
# let client = aliyun_oss_client::Client::from_env().unwrap();
client
    .put_file("examples/bg2015071010.png", "examples/bg2015071010.png")
    .await;

// or 上传文件内容
let file_content = std::fs::read("examples/bg2015071010.png").unwrap();
client
    .put_content(file_content, "examples/bg2015071010.png", |_| {
        Some("image/png")
    })
    .await;

// or 自定义上传文件 Content-Type
let file_content = std::fs::read("examples/bg2015071010.png").unwrap();
client
    .put_content_base(file_content, "image/png", "examples/bg2015071010.png")
    .await;

}

```

删除文件

```rust

#[tokio::main]

async fn main(){

# use std::env::set_var;
# set_var("ALIYUN_KEY_ID", "foo1");
# set_var("ALIYUN_KEY_SECRET", "foo2");
# set_var("ALIYUN_ENDPOINT", "qingdao");
# set_var("ALIYUN_BUCKET", "foo4");
# let client = aliyun_oss_client::Client::from_env().unwrap();
client.delete_object("examples/bg2015071010.png").await;

}

```

同步(阻塞模式)

如需使用,需要启用 blocking 特征

获取 client

```ignore // dotenv 是用于获取配置信息的,可以不使用 extern crate dotenv; use dotenv::dotenv; use std::env;

// 需要提供四个配置信息 let keyid = env::var("ALIYUNKEYID").unwrap(); let keysecret = env::var("ALIYUNKEYSECRET").unwrap(); let endpoint = aliyunossclient::EndPoint::default(); let bucket = env::var("ALIYUN_BUCKET").unwrap();

// 获取客户端实例 let client = aliyunossclient::ClientRc::new(keyid,keysecret, endpoint, bucket); ```

查询所有的 bucket 信息

```ignore

use std::env::set_var;

setvar("ALIYUNKEY_ID", "foo1");

setvar("ALIYUNKEY_SECRET", "foo2");

setvar("ALIYUNENDPOINT", "qingdao");

setvar("ALIYUNBUCKET", "foo4");

let client = aliyunossclient::ClientRc::from_env().unwrap();

let response = client.getbucketlist(); println!("buckets list: {:?}", response); ```

获取 bucket 信息

```ignore

use std::env::set_var;

setvar("ALIYUNKEY_ID", "foo1");

setvar("ALIYUNKEY_SECRET", "foo2");

setvar("ALIYUNENDPOINT", "qingdao");

setvar("ALIYUNBUCKET", "foo4");

let client = aliyunossclient::ClientRc::from_env().unwrap();

let response = client.getbucketinfo(); println!("bucket info: {:?}", response); ```

查询当前 bucket 中的 object 列表

```ignore

use std::env::set_var;

setvar("ALIYUNKEY_ID", "foo1");

setvar("ALIYUNKEY_SECRET", "foo2");

setvar("ALIYUNENDPOINT", "qingdao");

setvar("ALIYUNBUCKET", "foo4");

let client = aliyunossclient::ClientRc::from_env().unwrap();

use aliyunossclient::Query; let query = Query::new(); let response = client.getobjectlist(query); println!("objects list: {:?}", response); ```

也可以使用 bucket struct 查询 object 列表

```ignore

use std::env::set_var;

setvar("ALIYUNKEY_ID", "foo1");

setvar("ALIYUNKEY_SECRET", "foo2");

setvar("ALIYUNENDPOINT", "qingdao");

setvar("ALIYUNBUCKET", "foo4");

let client = aliyunossclient::ClientRc::from_env().unwrap();

use aliyunossclient::Query; let mut query = Query::new(); query.insert("max-keys", "5"); query.insert("prefix", "babel");

let result = client.getbucketinfo().unwrap().getobjectlist(query).unwrap();

println!("object list : {:?}", result);

// 翻页功能 获取下一页数据 println!("next object list: {:?}", result.next()); ```

上传文件

```ignore

use std::env::set_var;

setvar("ALIYUNKEY_ID", "foo1");

setvar("ALIYUNKEY_SECRET", "foo2");

setvar("ALIYUNENDPOINT", "qingdao");

setvar("ALIYUNBUCKET", "foo4");

let client = aliyunossclient::ClientRc::from_env().unwrap();

client .put_file("examples/bg2015071010.png", "examples/bg2015071010.png");

// or 上传文件内容 let filecontent = std::fs::read("examples/bg2015071010.png").unwrap(); client .putcontent(filecontent, "examples/bg2015071010.png", || { Some("image/png") });

// or 自定义上传文件 Content-Type let filecontent = std::fs::read("examples/bg2015071010.png").unwrap(); client .putcontentbase(filecontent, "image/png", "examples/bg2015071010.png"); ```

删除文件

```ignore

use std::env::set_var;

setvar("ALIYUNKEY_ID", "foo1");

setvar("ALIYUNKEY_SECRET", "foo2");

setvar("ALIYUNENDPOINT", "qingdao");

setvar("ALIYUNBUCKET", "foo4");

let client = aliyunossclient::ClientRc::from_env().unwrap();

client.delete_object("examples/bg2015071010.png"); ```

官方 client 对比