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

services/s3: Don't load_from_env if users already inputs #23

Merged
merged 1 commit into from
Feb 15, 2022
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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ repository = "https://github.com/datafuselabs/opendal"
keywords = ["storage", "data", "s3"]
categories = ["filesystem"]

[[bench]]
name = "s3"
harness = false

[dependencies]
async-compat = "0.2.1"
async-trait = "0.1.52"
Expand All @@ -30,3 +34,4 @@ uuid = { version = "0.8.2", features = ["serde", "v4"] }
anyhow = "1.0"
rand = "0.8.5"
sha2 = "0.10.1"
criterion = { version = "0.3", features = ["async", "async_tokio"] }
36 changes: 36 additions & 0 deletions benches/s3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2022 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use criterion::{criterion_group, criterion_main, Criterion};
use opendal::credential::Credential;

async fn init() {
opendal::services::s3::Backend::build()
.bucket("test")
.region("test")
.credential(Credential::hmac("test", "test"))
.finish()
.await
.unwrap();
}

fn criterion_benchmark(c: &mut Criterion) {
let runtime = tokio::runtime::Runtime::new().unwrap();

c.bench_function("s3_init", |b| {
b.to_async(&runtime).iter(|| init());
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
60 changes: 32 additions & 28 deletions src/services/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,47 @@ impl Builder {
String::new()
};

// Load config from environment, including:
// Config Loader will load config from environment.
//
// We will take user's input first if any. If there is no user input, we
// will fallback to the aws default load chain like the following:
//
// - Environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_REGION
// - The default credentials files located in ~/.aws/config and ~/.aws/credentials (location can vary per platform)
// - Web Identity Token credentials from the environment or container (including EKS)
// - ECS Container Credentials (IAM roles for tasks)
// - EC2 Instance Metadata Service (IAM Roles attached to instance)
let cfg = aws_config::load_from_env().await;

let mut cfg = AwsS3::config::Builder::from(&cfg);
//
// Please keep in mind that the config loader only detect region and credentials.
let mut cfg_loader = aws_config::ConfigLoader::default();

if let Some(region) = &self.region {
cfg = cfg.region(AwsS3::Region::new(Cow::from(region.clone())));
cfg_loader = cfg_loader.region(AwsS3::Region::new(Cow::from(region.clone())));
}

if let Some(cred) = &self.credential {
match cred {
Credential::HMAC {
access_key_id,
secret_access_key,
} => {
cfg_loader = cfg_loader.credentials_provider(AwsS3::Credentials::from_keys(
access_key_id,
secret_access_key,
None,
));
}
_ => {
return Err(Error::BackendConfigurationInvalid {
key: "credential".to_string(),
value: "".to_string(),
});
}
}
}

let mut cfg = AwsS3::config::Builder::from(&cfg_loader.load().await);

// Load users input first, if user not input, we will fallback to aws
// default load logic.
if let Some(endpoint) = &self.endpoint {
Expand Down Expand Up @@ -170,29 +197,6 @@ impl Builder {
cfg = cfg.endpoint_resolver(AwsS3::Endpoint::immutable(uri));
}

// Load users input first, if user not input, we will fallback to aws
// default load logic.
if let Some(cred) = &self.credential {
match cred {
Credential::HMAC {
access_key_id,
secret_access_key,
} => {
cfg = cfg.credentials_provider(AwsS3::Credentials::from_keys(
access_key_id,
secret_access_key,
None,
));
}
_ => {
return Err(Error::BackendConfigurationInvalid {
key: "credential".to_string(),
value: "".to_string(),
});
}
}
}

Ok(Arc::new(Backend {
// Make `/` as the default of root.
root,
Expand Down