From d5990fe28a6023c9c26e1c306d933c778a0c9638 Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Tue, 26 Sep 2023 13:46:00 +0800 Subject: [PATCH] fix(services/s3): Detect region returned too early when header is empty (#3187) Signed-off-by: Xuanwo --- core/src/services/s3/backend.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/core/src/services/s3/backend.rs b/core/src/services/s3/backend.rs index 85ef4ca2fe14..88dea3af9480 100644 --- a/core/src/services/s3/backend.rs +++ b/core/src/services/s3/backend.rs @@ -563,6 +563,10 @@ impl S3Builder { /// /// - [Amazon S3 HeadBucket API](https://docs.aws.amazon.com/zh_cn/AmazonS3/latest/API/API_HeadBucket.html) pub async fn detect_region(endpoint: &str, bucket: &str) -> Option { + // Remove the possible trailing `/` in endpoint. + let endpoint = endpoint.trim_end_matches('/'); + + // Make sure the endpoint contains the scheme. let mut endpoint = if endpoint.starts_with("http") { endpoint.to_string() } else { @@ -623,9 +627,10 @@ impl S3Builder { ); // Get region from response header no matter status code. - let region = res.headers().get("x-amz-bucket-region")?; - if let Ok(regin) = region.to_str() { - return Some(regin.to_string()); + if let Some(header) = res.headers().get("x-amz-bucket-region") { + if let Ok(regin) = header.to_str() { + return Some(regin.to_string()); + } } // Status code is 403 or 200 means we already visit the correct