From d0146fefc3ed5053f26410e22b3cb7ee5c8ca56b Mon Sep 17 00:00:00 2001 From: jiacai2050 Date: Fri, 10 Mar 2023 12:14:05 +0800 Subject: [PATCH] fix endpoint --- components/object_store/src/aliyun.rs | 58 ++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/components/object_store/src/aliyun.rs b/components/object_store/src/aliyun.rs index bc417b5a05..1e06921bb9 100644 --- a/components/object_store/src/aliyun.rs +++ b/components/object_store/src/aliyun.rs @@ -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, key_secret: impl Into, @@ -16,13 +28,55 @@ pub fn try_new( timeout: Duration, ) -> upstream::Result { 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 tests { + 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); + } + } +}