aliyun OSS 的一个客户端
最开始的时候,是作为一个 rust 练手项目,渐渐的现在越来越完善了,包含了 rust 的 struct,enum, async, trait features 等特性, 以及自定义 error 类,同时也包含完整的的测试用例
toml
[dependencies]
aliyun-oss-client = "^0.9"
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
let client = aliyun_oss_client::Client::new("key1".into(), "secret1".into(), "qingdao".into(), "my-bucket".into());
或者
rust
let client = aliyun_oss_client::client("key1", "secret1", "qingdao", "my-bucket");
在阿里云的 ECS 上请求 OSS 接口,使用内网 API 有更高的效率,只需要在 ECS 上设置 ALIYUN_OSS_INTERNAL
环境变量为 true
即可
```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();
use aliyun_oss_client::file::File;
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();
use aliyun_oss_client::file::File;
client.delete_object("examples/bg2015071010.png").await;
```
如需使用,需要启用
blocking
特征
```ignore // dotenv 是用于获取配置信息的,可以不使用 extern crate dotenv; use dotenv::dotenv; use std::env;
// 需要提供四个配置信息 use aliyunossclient::BucketName; let bucket = BucketName::new("bbb").unwrap(); // 获取客户端实例 let client = aliyunossclient::ClientRc::new("key1".into(),"secret1".into(),"qingdao".into(), 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 mut result = client.getbucketinfo().unwrap().getobjectlist(query).unwrap();
println!("object list : {:?}", result);
// 翻页功能 获取下一页数据 println!("next object list: {:?}", result.next()); ```
```ignore
use aliyunossclient::file::blocking::File; 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 aliyunossclient::file::blocking::File; client.delete_object("examples/bg2015071010.png"); ```
bash
rustup run nightly cargo bench