Skip to content

Commit 4ee85b3

Browse files
authored
services/s3: Don't load_from_env if users already inputs (#23)
Signed-off-by: Xuanwo <[email protected]>
1 parent e2dc3f4 commit 4ee85b3

File tree

3 files changed

+73
-28
lines changed

3 files changed

+73
-28
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ repository = "https://github.com/datafuselabs/opendal"
99
keywords = ["storage", "data", "s3"]
1010
categories = ["filesystem"]
1111

12+
[[bench]]
13+
name = "s3"
14+
harness = false
15+
1216
[dependencies]
1317
async-compat = "0.2.1"
1418
async-trait = "0.1.52"
@@ -30,3 +34,4 @@ uuid = { version = "0.8.2", features = ["serde", "v4"] }
3034
anyhow = "1.0"
3135
rand = "0.8.5"
3236
sha2 = "0.10.1"
37+
criterion = { version = "0.3", features = ["async", "async_tokio"] }

benches/s3.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2022 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
use criterion::{criterion_group, criterion_main, Criterion};
15+
use opendal::credential::Credential;
16+
17+
async fn init() {
18+
opendal::services::s3::Backend::build()
19+
.bucket("test")
20+
.region("test")
21+
.credential(Credential::hmac("test", "test"))
22+
.finish()
23+
.await
24+
.unwrap();
25+
}
26+
27+
fn criterion_benchmark(c: &mut Criterion) {
28+
let runtime = tokio::runtime::Runtime::new().unwrap();
29+
30+
c.bench_function("s3_init", |b| {
31+
b.to_async(&runtime).iter(|| init());
32+
});
33+
}
34+
35+
criterion_group!(benches, criterion_benchmark);
36+
criterion_main!(benches);

src/services/s3.rs

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,47 @@ impl Builder {
119119
String::new()
120120
};
121121

122-
// Load config from environment, including:
122+
// Config Loader will load config from environment.
123+
//
124+
// We will take user's input first if any. If there is no user input, we
125+
// will fallback to the aws default load chain like the following:
126+
//
123127
// - Environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_REGION
124128
// - The default credentials files located in ~/.aws/config and ~/.aws/credentials (location can vary per platform)
125129
// - Web Identity Token credentials from the environment or container (including EKS)
126130
// - ECS Container Credentials (IAM roles for tasks)
127131
// - EC2 Instance Metadata Service (IAM Roles attached to instance)
128-
let cfg = aws_config::load_from_env().await;
129-
130-
let mut cfg = AwsS3::config::Builder::from(&cfg);
132+
//
133+
// Please keep in mind that the config loader only detect region and credentials.
134+
let mut cfg_loader = aws_config::ConfigLoader::default();
131135

132136
if let Some(region) = &self.region {
133-
cfg = cfg.region(AwsS3::Region::new(Cow::from(region.clone())));
137+
cfg_loader = cfg_loader.region(AwsS3::Region::new(Cow::from(region.clone())));
138+
}
139+
140+
if let Some(cred) = &self.credential {
141+
match cred {
142+
Credential::HMAC {
143+
access_key_id,
144+
secret_access_key,
145+
} => {
146+
cfg_loader = cfg_loader.credentials_provider(AwsS3::Credentials::from_keys(
147+
access_key_id,
148+
secret_access_key,
149+
None,
150+
));
151+
}
152+
_ => {
153+
return Err(Error::BackendConfigurationInvalid {
154+
key: "credential".to_string(),
155+
value: "".to_string(),
156+
});
157+
}
158+
}
134159
}
135160

161+
let mut cfg = AwsS3::config::Builder::from(&cfg_loader.load().await);
162+
136163
// Load users input first, if user not input, we will fallback to aws
137164
// default load logic.
138165
if let Some(endpoint) = &self.endpoint {
@@ -170,29 +197,6 @@ impl Builder {
170197
cfg = cfg.endpoint_resolver(AwsS3::Endpoint::immutable(uri));
171198
}
172199

173-
// Load users input first, if user not input, we will fallback to aws
174-
// default load logic.
175-
if let Some(cred) = &self.credential {
176-
match cred {
177-
Credential::HMAC {
178-
access_key_id,
179-
secret_access_key,
180-
} => {
181-
cfg = cfg.credentials_provider(AwsS3::Credentials::from_keys(
182-
access_key_id,
183-
secret_access_key,
184-
None,
185-
));
186-
}
187-
_ => {
188-
return Err(Error::BackendConfigurationInvalid {
189-
key: "credential".to_string(),
190-
value: "".to_string(),
191-
});
192-
}
193-
}
194-
}
195-
196200
Ok(Arc::new(Backend {
197201
// Make `/` as the default of root.
198202
root,

0 commit comments

Comments
 (0)