aliyun OSS 的一个客户端
最开始的时候,是作为一个 rust 练手项目,渐渐的现在越来越完善了,包含了 rust 的 struct,enum, async, trait features 等特性, 以及自定义 error 类,同时也包含完整的的测试用例
toml
[dependencies]
aliyun-oss-client = "^0.8"
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);
```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().unwrap();
client.get_bucket_list().await;
```
```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().unwrap();
let response = client.get_bucket_info().await;
println!("bucket info: {:?}", response);
```
```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().unwrap();
use aliyun_oss_client::Query;
let query = Query::new();
let response = client.get_object_list(query).await;
println!("objects list: {:?}", response);
```
```ignore
# 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
# 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
# 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
特征
```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); ```
```ignore
let response = client.getbucketlist(); println!("buckets list: {:?}", response); ```
```ignore
let response = client.getbucketinfo(); println!("bucket info: {:?}", response); ```
```ignore
use aliyunossclient::Query; let query = Query::new(); let response = client.getobjectlist(query); println!("objects list: {:?}", response); ```
```ignore
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
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
client.delete_object("examples/bg2015071010.png"); ```