Skip to content

Commit

Permalink
fix endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Mar 10, 2023
1 parent adfbe30 commit 4a569fa
Showing 1 changed file with 56 additions and 2 deletions.
58 changes: 56 additions & 2 deletions components/object_store/src/aliyun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@ use std::time::Duration;

use upstream::{
aws::{AmazonS3, AmazonS3Builder},
ClientOptions,
ClientOptions, RetryConfig,
};

fn normalize_endpoint(endpoint: &str, bucket: &str) -> String {
if endpoint.starts_with("https") {
format!(
"https://{}.{}",
bucket,
endpoint.replacen("https://", "", 1)
)
} else {
format!("http://{}.{}", bucket, endpoint.replacen("http://", "", 1))
}
}

pub fn try_new(
key_id: impl Into<String>,
key_secret: impl Into<String>,
Expand All @@ -16,13 +28,55 @@ pub fn try_new(
timeout: Duration,
) -> upstream::Result<AmazonS3> {
let cli_opt = ClientOptions::new()
.with_allow_http(true)
.with_pool_max_idle_per_host(pool_max_idle_per_host.into())
.with_timeout(timeout);
let retry_config = RetryConfig {
// TODO: add to config
max_retries: 3,
..Default::default()
};

let endpoint = endpoint.into();
let bucket = bucket.into();
let endpoint = normalize_endpoint(&endpoint, &bucket);
AmazonS3Builder::new()
.with_virtual_hosted_style_request(true)
// region is not used when virtual_hosted_style is true,
// but is required, so dummy is used here
.with_region("dummy")
.with_access_key_id(key_id)
.with_secret_access_key(key_secret)
.with_endpoint(endpoint)
.with_bucket_name(bucket)
.with_url(endpoint)
.with_client_options(cli_opt)
.with_retry(retry_config)
.build()
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_normalize_endpoint() {
let testcase = [
(
"https://oss.aliyun.com",
"test",
"https://test.oss.aliyun.com",
),
(
"http://oss.aliyun.com",
"test",
"http://test.oss.aliyun.com",
),
("no-scheme.com", "test", "http://test.no-scheme.com"),
];

for (endpoint, bucket, expected) in testcase {
let actual = normalize_endpoint(endpoint, bucket);
assert_eq!(expected, actual);
}
}
}

0 comments on commit 4a569fa

Please sign in to comment.