From 3d4454251604d3d501cd41b877906ec94bb06434 Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Fri, 19 May 2023 17:35:23 +0800 Subject: [PATCH] feat(services/cos): Add support for loading from env Signed-off-by: Xuanwo --- Cargo.lock | 4 ++-- core/Cargo.toml | 2 +- core/src/services/cos/backend.rs | 31 +++++++++++++++++++++++++------ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f05e454749dd..b2df511659db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3714,9 +3714,9 @@ checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" [[package]] name = "reqsign" -version = "0.11.0" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ef945b588044afa4ee3a87d3cbfadd8c47304e8a6fe17f7b9976aa215a47a5f" +checksum = "b04f5fccb94d61c154f0d8520ec42e79afdc145f4b1a392faa269874995fda66" dependencies = [ "anyhow", "async-trait", diff --git a/core/Cargo.toml b/core/Cargo.toml index e8b23005bc40..c5ace15e3676 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -202,7 +202,7 @@ redis = { version = "0.22", features = [ "tokio-comp", "connection-manager", ], optional = true } -reqsign = { version = "0.11.0", default-features = false, optional = true } +reqsign = { version = "0.12.0", default-features = false, optional = true } reqwest = { version = "0.11.13", features = [ "stream", ], default-features = false } diff --git a/core/src/services/cos/backend.rs b/core/src/services/cos/backend.rs index 5ca4e4189019..e9165824b965 100644 --- a/core/src/services/cos/backend.rs +++ b/core/src/services/cos/backend.rs @@ -102,6 +102,8 @@ pub struct CosBuilder { secret_key: Option, bucket: Option, http_client: Option, + + disable_config_load: bool, } impl Debug for CosBuilder { @@ -175,6 +177,17 @@ impl CosBuilder { self } + /// Disable config load so that opendal will not load config from + /// environment. + /// + /// For examples: + /// + /// - envs like `TENCENTCLOUD_SECRET_ID` + pub fn disable_config_load(&mut self) -> &mut Self { + self.disable_config_load = true; + self + } + /// Specify the http client that used by this service. /// /// # Notes @@ -247,13 +260,19 @@ impl Builder for CosBuilder { })? }; - let config = TencentCosConfig { - access_key_id: self.secret_id.take(), - secret_access_key: self.secret_key.take(), - security_token: None, - }; + let mut cfg = TencentCosConfig::default(); + if !self.disable_config_load { + cfg = cfg.from_env(); + } + + if let Some(v) = self.secret_id.take() { + cfg.secret_id = Some(v); + } + if let Some(v) = self.secret_key.take() { + cfg.secret_key = Some(v); + } - let cred_loader = TencentCosCredentialLoader::new(config); + let cred_loader = TencentCosCredentialLoader::new(client.client(), cfg); let signer = TencentCosSigner::new();