Skip to content

Commit

Permalink
fix(services/s3): Detect region returned too early when header is emp…
Browse files Browse the repository at this point in the history
…ty (#3187)

Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo authored Sep 26, 2023
1 parent 2da70d6 commit d5990fe
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions core/src/services/s3/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
// 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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d5990fe

Please sign in to comment.