Skip to content

Commit

Permalink
feat(services/oss): reuse allow_anonymous
Browse files Browse the repository at this point in the history
Signed-off-by: suyanhanx <[email protected]>
  • Loading branch information
suyanhanx committed Apr 11, 2023
1 parent b8c29fd commit 4445c7e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions core/src/services/oss/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ impl Builder for OssBuilder {
endpoint,
host,
presign_endpoint,
allow_anonymous: self.allow_anonymous,
signer,
loader,
client,
Expand Down
29 changes: 21 additions & 8 deletions core/src/services/oss/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub struct OssCore {
pub endpoint: String,
pub presign_endpoint: String,

pub allow_anonymous: bool,

pub client: HttpClient,
pub loader: AliyunLoader,
pub signer: AliyunOssSigner,
Expand All @@ -62,30 +64,41 @@ impl Debug for OssCore {
}

impl OssCore {
async fn load_credential(&self) -> Result<AliyunCredential> {
async fn load_credential(&self) -> Result<Option<AliyunCredential>> {
if self.allow_anonymous {
return Ok(None);
}

let cred = self
.loader
.load()
.await
.map_err(new_request_credential_error)?;

if let Some(cred) = cred {
Ok(cred)
Ok(Some(cred))
} else {
Err(Error::new(
ErrorKind::ConfigInvalid,
"no valid credential found",
))
Ok(None)
}
}

pub async fn sign<T>(&self, req: &mut Request<T>) -> Result<()> {
let cred = self.load_credential().await?;
let cred = if let Some(cred) = self.load_credential().await? {
cred
} else {
return Ok(());
};

self.signer.sign(req, &cred).map_err(new_request_sign_error)
}

pub async fn sign_query<T>(&self, req: &mut Request<T>, duration: Duration) -> Result<()> {
let cred = self.load_credential().await?;
let cred = if let Some(cred) = self.load_credential().await? {
cred
} else {
return Ok(());
};

self.signer
.sign_query(req, duration, &cred)
.map_err(new_request_sign_error)
Expand Down

0 comments on commit 4445c7e

Please sign in to comment.