-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(file): 将 File trait 改名为 Files,另外新增 File trait
File trait 用于单个文件的类型,在进行上传等操作时,不需要指定路径 Files trait 用于文件集合的类型,在进行操作时,需要指定具体的文件路径,例如: use aliyun_oss_client::file::Files; client.delete_object(ObjectPath::new("abc.jpg").unwrap()); FileAs trait 用于比较便捷的操作:例如: use aliyun_oss_client::file::FileAs; client.delete_object_as("abc.jpg"); break change
- Loading branch information
Showing
12 changed files
with
587 additions
and
302 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use std::{fs, path::Path}; | ||
|
||
use aliyun_oss_client::{ | ||
config::ObjectPath, | ||
file::{File, FileError, Files}, | ||
BucketName, Client, EndPoint, KeyId, KeySecret, | ||
}; | ||
|
||
struct MyObject { | ||
path: ObjectPath, | ||
} | ||
|
||
impl MyObject { | ||
const KEY_ID: KeyId = KeyId::from_static("xxxxxxxxxxxxxxxx"); | ||
const KEY_SECRET: KeySecret = KeySecret::from_static("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); | ||
const END_POINT: EndPoint = EndPoint::CnShanghai; | ||
const BUCKET: BucketName = unsafe { BucketName::from_static2("xxxxxx") }; | ||
|
||
fn new(path: &Path) -> Result<MyObject, FileError> { | ||
Ok(MyObject { | ||
path: path.try_into()?, | ||
}) | ||
} | ||
} | ||
|
||
impl File for MyObject { | ||
type Client = Client; | ||
fn get_path(&self) -> ObjectPath { | ||
self.path.clone() | ||
} | ||
|
||
fn oss_client(&self) -> Self::Client { | ||
Client::new( | ||
Self::KEY_ID, | ||
Self::KEY_SECRET, | ||
Self::END_POINT, | ||
Self::BUCKET, | ||
) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), FileError> { | ||
for entry in fs::read_dir("examples")? { | ||
let path = entry?.path(); | ||
let path = path.as_path(); | ||
|
||
if !path.is_file() { | ||
continue; | ||
} | ||
|
||
let obj = MyObject::new(path)?; | ||
let content = fs::read(path)?; | ||
|
||
let res = obj.put_oss(content, Client::DEFAULT_CONTENT_TYPE).await?; | ||
|
||
println!("result status: {}", res.status()); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use std::fs; | ||
|
||
use aliyun_oss_client::builder::{BuilderError, RequestBuilder}; | ||
use aliyun_oss_client::file::{AlignBuilder, FileError, Files}; | ||
use aliyun_oss_client::types::CanonicalizedResource; | ||
use aliyun_oss_client::{BucketName, Client, EndPoint, HeaderName, HeaderValue, Method}; | ||
use reqwest::Url; | ||
|
||
struct MyClient; | ||
|
||
#[derive(Debug)] | ||
struct MyError(String); | ||
|
||
impl From<FileError> for MyError { | ||
fn from(value: FileError) -> Self { | ||
Self(value.to_string()) | ||
} | ||
} | ||
|
||
struct MyPath(String); | ||
|
||
impl AlignBuilder for MyClient { | ||
fn builder_with_header<H: IntoIterator<Item = (HeaderName, HeaderValue)>>( | ||
&self, | ||
method: Method, | ||
url: Url, | ||
resource: CanonicalizedResource, | ||
headers: H, | ||
) -> Result<RequestBuilder, BuilderError> { | ||
dotenv::dotenv().ok(); | ||
Client::from_env()?.builder_with_header(method, url, resource, headers) | ||
} | ||
} | ||
|
||
impl Files for MyClient { | ||
type Err = MyError; | ||
type Path = MyPath; | ||
fn get_url(&self, path: Self::Path) -> Result<(Url, CanonicalizedResource), Self::Err> { | ||
use aliyun_oss_client::config::OssFullUrl; | ||
|
||
dotenv::dotenv().ok(); | ||
let bucket = std::env::var("ALIYUN_BUCKET").unwrap(); | ||
|
||
let end_point = EndPoint::CnShanghai; | ||
let bucket = BucketName::new(bucket).unwrap(); | ||
|
||
let resource = format!("/{}/{}", bucket, path.0); | ||
|
||
let p = path | ||
.0 | ||
.try_into() | ||
.map_err(|_| MyError("路径格式错误".to_string()))?; | ||
let url = Url::from_oss(&end_point, &bucket, &p); | ||
|
||
Ok((url, CanonicalizedResource::new(resource))) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let client = MyClient {}; | ||
|
||
let file = fs::read("rustfmt.toml").unwrap(); | ||
let res = client | ||
.put_content_base(file, "application/json", MyPath("rustfmt.toml".to_string())) | ||
.await; | ||
|
||
println!("{res:?}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use quote::ToTokens; | ||
use syn::{ | ||
parse::{Parse, ParseStream, Result}, | ||
parse_quote, | ||
visit_mut::{self, VisitMut}, | ||
TraitItemMethod, WhereClause, | ||
}; | ||
|
||
pub(crate) struct GenWhere(TraitItemMethod); | ||
|
||
impl Parse for GenWhere { | ||
fn parse(input: ParseStream) -> Result<Self> { | ||
Ok(Self(input.parse()?)) | ||
} | ||
} | ||
|
||
impl ToTokens for GenWhere { | ||
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { | ||
let mut item = self.0.clone(); | ||
AppendWhere.visit_trait_item_method_mut(&mut item); | ||
|
||
item.to_tokens(tokens); | ||
} | ||
} | ||
|
||
struct AppendWhere; | ||
|
||
impl VisitMut for AppendWhere { | ||
fn visit_trait_item_method_mut(&mut self, item: &mut TraitItemMethod) { | ||
visit_mut::visit_trait_item_method_mut(self, item); | ||
} | ||
|
||
fn visit_where_clause_mut(&mut self, i: &mut WhereClause) { | ||
i.predicates | ||
.push(parse_quote! {OP: TryInto<ObjectPath> + Send + Sync}); | ||
i.predicates | ||
.push(parse_quote! {<OP as TryInto<ObjectPath>>::Error: Into<Self::Error>}); | ||
|
||
visit_mut::visit_where_clause_mut(self, i); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.