Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(services/cos): Add support for loading from env #2271

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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