Struct aliyun_oss_client::client::Client
source · #[non_exhaustive]pub struct Client<M = ClientWithMiddleware> { /* private fields */ }
Expand description
§构造请求的客户端结构体
Implementations§
source§impl Client
impl Client
sourcepub async fn get_bucket_list(self) -> Result<ListBuckets, ExtractListError>
pub async fn get_bucket_list(self) -> Result<ListBuckets, ExtractListError>
从 OSS 获取 bucket 列表
sourcepub async fn base_bucket_list<List, Item, E, ItemErr>(
&self,
list: &mut List
) -> Result<(), ExtractListError>where
List: RefineBucketList<Item, E, ItemErr> + InitObject<Item>,
Item: RefineBucket<ItemErr>,
E: ListError,
ItemErr: Error + 'static,
pub async fn base_bucket_list<List, Item, E, ItemErr>(
&self,
list: &mut List
) -> Result<(), ExtractListError>where
List: RefineBucketList<Item, E, ItemErr> + InitObject<Item>,
Item: RefineBucket<ItemErr>,
E: ListError,
ItemErr: Error + 'static,
从 OSS 获取 bucket 列表,并存入自定义类型中
sourcepub async fn get_bucket_info(self) -> Result<Bucket, ExtractItemError>
pub async fn get_bucket_info(self) -> Result<Bucket, ExtractItemError>
从 OSS 上获取默认的 bucket 信息
sourcepub async fn base_bucket_info<Bucket, E>(
&self,
bucket: &mut Bucket
) -> Result<(), ExtractItemError>where
Bucket: RefineBucket<E>,
E: Error + 'static,
pub async fn base_bucket_info<Bucket, E>(
&self,
bucket: &mut Bucket
) -> Result<(), ExtractItemError>where
Bucket: RefineBucket<E>,
E: Error + 'static,
§从 OSS 上获取 bucket 的信息,并存入自定义的类型中
默认获取 Client 中的默认 bucket 信息,如需获取其他 bucket,可调用 set_bucket
更改后调用
也可以调用 set_endpoint
更改可用区
source§impl Client<ClientWithMiddleware>
impl Client<ClientWithMiddleware>
sourcepub fn get_bucket_list(self) -> Result<ListBuckets<RcPointer>, ExtractListError>
pub fn get_bucket_list(self) -> Result<ListBuckets<RcPointer>, ExtractListError>
获取 bucket 列表
sourcepub fn base_bucket_list<List, Item, E, ItemErr>(
&self,
list: &mut List
) -> Result<(), ExtractListError>where
List: RefineBucketList<Item, E, ItemErr> + InitObject<Item>,
Item: RefineBucket<ItemErr>,
E: ListError,
ItemErr: Error + 'static,
pub fn base_bucket_list<List, Item, E, ItemErr>(
&self,
list: &mut List
) -> Result<(), ExtractListError>where
List: RefineBucketList<Item, E, ItemErr> + InitObject<Item>,
Item: RefineBucket<ItemErr>,
E: ListError,
ItemErr: Error + 'static,
获取 bucket 列表,可存储为自定义的类型
sourcepub fn get_bucket_info(self) -> Result<Bucket<RcPointer>, ExtractItemError>
pub fn get_bucket_info(self) -> Result<Bucket<RcPointer>, ExtractItemError>
获取当前的 bucket 的信息
sourcepub fn base_bucket_info<Bucket, E>(
&self,
bucket: &mut Bucket
) -> Result<(), ExtractItemError>where
Bucket: RefineBucket<E>,
E: Error + 'static,
pub fn base_bucket_info<Bucket, E>(
&self,
bucket: &mut Bucket
) -> Result<(), ExtractItemError>where
Bucket: RefineBucket<E>,
E: Error + 'static,
获取某一个 bucket 的信息,并存储到自定义的类型
source§impl<M: Default> Client<M>
impl<M: Default> Client<M>
sourcepub fn new(
access_key_id: KeyId,
access_key_secret: KeySecret,
endpoint: EndPoint,
bucket: BucketName
) -> Self
pub fn new( access_key_id: KeyId, access_key_secret: KeySecret, endpoint: EndPoint, bucket: BucketName ) -> Self
使用基本配置信息初始化 Client
sourcepub fn from_config(config: Config) -> Self
pub fn from_config(config: Config) -> Self
使用 Config
中的配置初始化 Client
sourcepub fn from_env() -> Result<Self, InvalidConfig>
pub fn from_env() -> Result<Self, InvalidConfig>
§通过环境变量初始化 Client
如果在 Aliyun ECS 上,可将环境变量 ALIYUN_OSS_INTERNAL
设置为 true
/ 1
/ yes
/ Y
,即可使用 internal 网络请求 OSS 接口
示例
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");
use aliyun_oss_client::builder::ClientWithMiddleware;
let client = Client::<ClientWithMiddleware>::from_env();
assert!(client.is_ok());
source§impl<M> Client<M>
impl<M> Client<M>
sourcepub fn get_bucket_base(&self) -> BucketBase
pub fn get_bucket_base(&self) -> BucketBase
返回默认可用区,默认 bucket 的 BucketBase
sourcepub fn get_bucket_url(&self) -> Url
pub fn get_bucket_url(&self) -> Url
获取默认的 bucket 的 url
sourcepub fn get_endpoint_url(&self) -> Url
pub fn get_endpoint_url(&self) -> Url
获取默认的可用区的 url
sourcepub fn set_bucket(&mut self, bucket: BucketName)
pub fn set_bucket(&mut self, bucket: BucketName)
更改默认 bucket
sourcepub fn set_endpoint(&mut self, endpoint: EndPoint)
pub fn set_endpoint(&mut self, endpoint: EndPoint)
更改默认 endpoint
sourcepub fn get_object_base<P>(
&self,
path: P
) -> Result<ObjectBase, InvalidObjectPath>
pub fn get_object_base<P>( &self, path: P ) -> Result<ObjectBase, InvalidObjectPath>
根据默认的 bucket,endpoint 和提供的文件路径,获取 ObjectBase
source§impl Client
impl Client
sourcepub async fn get_object_list<Q: IntoIterator<Item = (QueryKey, QueryValue)>>(
&self,
query: Q
) -> Result<ObjectList, ExtractListError>
pub async fn get_object_list<Q: IntoIterator<Item = (QueryKey, QueryValue)>>( &self, query: Q ) -> Result<ObjectList, ExtractListError>
查询默认 bucket 的文件列表
查询条件参数有多种方式,具体参考 get_object_list
文档
sourcepub async fn get_object_list2(
&self,
query: Query
) -> Result<ObjectList, ExtractListError>
pub async fn get_object_list2( &self, query: Query ) -> Result<ObjectList, ExtractListError>
查询默认 bucket 的文件列表
sourcepub async fn base_object_list<Q: IntoIterator<Item = (QueryKey, QueryValue)>, List, Item, E: ListError, ItemErr: Error + 'static>(
&self,
query: Q,
list: &mut List
) -> Result<(), ExtractListError>
pub async fn base_object_list<Q: IntoIterator<Item = (QueryKey, QueryValue)>, List, Item, E: ListError, ItemErr: Error + 'static>( &self, query: Q, list: &mut List ) -> Result<(), ExtractListError>
§可将 object 列表导出到外部类型(关注便捷性)
从 Client 中的默认 bucket 中获取,如需获取其他 bucket 的,可调用 set_bucket
更改后调用
也可以通过调用 set_endpoint
更改可用区
可以参考下面示例,或者项目中的 examples/custom.rs
§示例
use aliyun_oss_client::{
decode::{ListError, RefineObject, RefineObjectList},
object::{ExtractListError, InitObject},
Client,
};
use dotenv::dotenv;
use thiserror::Error;
#[derive(Debug)]
struct MyFile {
key: String,
#[allow(dead_code)]
other: String,
}
impl RefineObject<MyError> for MyFile {
fn set_key(&mut self, key: &str) -> Result<(), MyError> {
self.key = key.to_string();
Ok(())
}
}
#[derive(Default, Debug)]
struct MyBucket {
name: String,
files: Vec<MyFile>,
}
impl RefineObjectList<MyFile, MyError> for MyBucket {
fn set_name(&mut self, name: &str) -> Result<(), MyError> {
self.name = name.to_string();
Ok(())
}
fn set_list(&mut self, list: Vec<MyFile>) -> Result<(), MyError> {
self.files = list;
Ok(())
}
}
#[derive(Debug, Error)]
#[error("my error")]
enum MyError {}
impl ListError for MyError {}
async fn run() -> Result<(), ExtractListError> {
dotenv().ok();
use aliyun_oss_client::BucketName;
let client = Client::from_env().unwrap();
// 除了设置Default 外,还可以做更多设置
let mut bucket = MyBucket {
name: "abc".to_string(),
files: Vec::with_capacity(20),
};
// 利用闭包对 MyFile 做一下初始化设置
impl InitObject<MyFile> for MyBucket {
fn init_object(&mut self) -> Option<MyFile> {
Some(MyFile {
key: String::default(),
other: "abc".to_string(),
})
}
}
client.base_object_list([], &mut bucket).await?;
println!("bucket: {:?}", bucket);
Ok(())
}
sourcepub async fn base_object_list2<List, Item, E: ListError, ItemErr: Error + 'static>(
&self,
query: &Query,
list: &mut List
) -> Result<(), ExtractListError>
pub async fn base_object_list2<List, Item, E: ListError, ItemErr: Error + 'static>( &self, query: &Query, list: &mut List ) -> Result<(), ExtractListError>
§可将 object 列表导出到外部类型(关注性能)
从 Client 中的默认 bucket 中获取,如需获取其他 bucket 的,可调用 set_bucket
更改后调用
也可以通过调用 set_endpoint
更改可用区
sourcepub async fn get_custom_object<Item, ItemErr>(
&self,
query: &Query
) -> Result<Objects<Item>, ExtractListError>
pub async fn get_custom_object<Item, ItemErr>( &self, query: &Query ) -> Result<Objects<Item>, ExtractListError>
§获取包含自定义类型的 object 集合
其包含在 ObjectList
对象中
source§impl Client<ClientWithMiddleware>
impl Client<ClientWithMiddleware>
sourcepub fn get_object_list<Q: IntoIterator<Item = (QueryKey, QueryValue)>>(
self,
query: Q
) -> Result<ObjectList<RcPointer>, ExtractListError>
pub fn get_object_list<Q: IntoIterator<Item = (QueryKey, QueryValue)>>( self, query: Q ) -> Result<ObjectList<RcPointer>, ExtractListError>
查询默认 bucket 的文件列表
查询条件参数有多种方式,具体参考 get_object_list
文档
sourcepub fn base_object_list<Q: IntoIterator<Item = (QueryKey, QueryValue)>, List, Item, F, E: ListError, ItemErr: Error + 'static>(
&self,
query: Q,
list: &mut List,
init_object: F
) -> Result<(), ExtractListError>where
List: RefineObjectList<Item, E, ItemErr>,
Item: RefineObject<ItemErr>,
F: Fn(&mut List) -> Option<Item>,
pub fn base_object_list<Q: IntoIterator<Item = (QueryKey, QueryValue)>, List, Item, F, E: ListError, ItemErr: Error + 'static>(
&self,
query: Q,
list: &mut List,
init_object: F
) -> Result<(), ExtractListError>where
List: RefineObjectList<Item, E, ItemErr>,
Item: RefineObject<ItemErr>,
F: Fn(&mut List) -> Option<Item>,
可将 object 列表导出到外部 struct
Trait Implementations§
source§impl AlignBuilder for Client<ClientWithMiddleware>
impl AlignBuilder for Client<ClientWithMiddleware>
source§fn builder_with_header<H: IntoIterator<Item = (HeaderName, HeaderValue)>>(
&self,
method: Method,
url: Url,
resource: CanonicalizedResource,
headers: H
) -> Result<RequestBuilder, BuilderError>
fn builder_with_header<H: IntoIterator<Item = (HeaderName, HeaderValue)>>( &self, method: Method, url: Url, resource: CanonicalizedResource, headers: H ) -> Result<RequestBuilder, BuilderError>
§构造自定义的接口请求方法
比如在上传完文件时,返回自己期望的数据,而不是仅返回 etag 信息
§例子是一个获取 object 元信息的接口
use aliyun_oss_client::{errors::OssError, file::AlignBuilder, Client, Method};
use dotenv::dotenv;
async fn run() -> Result<(), OssError> {
dotenv().ok();
let client = Client::from_env().unwrap();
let (url, resource) = client
.get_object_base("9AB932LY.jpeg")?
.get_url_resource([]);
let headers = vec![(
"If-Unmodified-Since".parse().unwrap(),
"Sat, 01 Jan 2022 18:01:01 GMT".parse().unwrap(),
)];
let builder = client.builder_with_header(Method::HEAD, url, resource, headers)?;
let response = builder.send().await?;
println!("status: {:?}", response.status());
Ok(())
}
§参数
- method 接口请求方式
- url 要请求的接口,包含 query 参数等信息
- resource 阿里云接口需要提供的统一的信息,
CanonicalizedResource
提供了 bucket ,object 等各种生成方式,如果无法满足 还可以自己用 trait 来自定义
§返回值
返回值是一个封装了 reqwest::Builder 构造器,RequestBuilder
, 提供两个方法 send
和 send_adjust_error
send
方法,直接返回reqwest::Response
send_adjust_error
方法,会对 api 返回结果进行处理,如果 HTTP 状态码正常(200>= && <300) 则,返回 Ok, 否则,会对返回的 xml 异常数据进行解析,返回 Err(OssService
)
source§impl AlignBuilder for Client<ClientWithMiddleware>
impl AlignBuilder for Client<ClientWithMiddleware>
source§fn builder_with_header<H: IntoIterator<Item = (HeaderName, HeaderValue)>>(
&self,
method: Method,
url: Url,
resource: CanonicalizedResource,
headers: H
) -> Result<BlockingRequestBuilder, BuilderError>
fn builder_with_header<H: IntoIterator<Item = (HeaderName, HeaderValue)>>( &self, method: Method, url: Url, resource: CanonicalizedResource, headers: H ) -> Result<BlockingRequestBuilder, BuilderError>
§向 OSS 发送请求的封装
参数包含请求的:
- method
- url
- headers (可选)
- CanonicalizedResource
返回值是一个 reqwest 的请求创建器 reqwest::blocking::RequestBuilder
返回后,可以再加请求参数,然后可选的进行发起请求
source§impl<M> AsMut<BucketName> for Client<M>
impl<M> AsMut<BucketName> for Client<M>
source§fn as_mut(&mut self) -> &mut BucketName
fn as_mut(&mut self) -> &mut BucketName
source§impl<M> AsRef<BucketName> for Client<M>
impl<M> AsRef<BucketName> for Client<M>
source§fn as_ref(&self) -> &BucketName
fn as_ref(&self) -> &BucketName
source§impl<M> From<Client<M>> for BucketBase
impl<M> From<Client<M>> for BucketBase
source§impl<M: PartialEq> PartialEq for Client<M>
impl<M: PartialEq> PartialEq for Client<M>
source§impl<M: Default> STS for Client<M>
impl<M: Default> STS for Client<M>
source§fn new_with_sts<ST>(
access_key_id: KeyId,
access_key_secret: KeySecret,
endpoint: EndPoint,
bucket: BucketName,
security_token: ST
) -> Result<Self, InvalidHeaderValue>
fn new_with_sts<ST>( access_key_id: KeyId, access_key_secret: KeySecret, endpoint: EndPoint, bucket: BucketName, security_token: ST ) -> Result<Self, InvalidHeaderValue>
Client
impl<M: Eq> Eq for Client<M>
impl<M> StructuralPartialEq for Client<M>
Auto Trait Implementations§
impl<M> Freeze for Client<M>where
M: Freeze,
impl<M> RefUnwindSafe for Client<M>where
M: RefUnwindSafe,
impl<M> Send for Client<M>where
M: Send,
impl<M> Sync for Client<M>where
M: Sync,
impl<M> Unpin for Client<M>where
M: Unpin,
impl<M> UnwindSafe for Client<M>where
M: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.