Skip to content

Commit

Permalink
feat(services/cos): Add support for loading from env (#2271)
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo authored May 19, 2023
1 parent 25c3160 commit 191c4e5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
31 changes: 25 additions & 6 deletions core/src/services/cos/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ pub struct CosBuilder {
secret_key: Option<String>,
bucket: Option<String>,
http_client: Option<HttpClient>,

disable_config_load: bool,
}

impl Debug for CosBuilder {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 191c4e5

Please sign in to comment.