Skip to content

Commit

Permalink
Merge pull request #58148 from yariks5s/fix_s3_regions
Browse files Browse the repository at this point in the history
S3-links region independency
  • Loading branch information
yariks5s committed Dec 26, 2023
2 parents 85b1493 + 50724f1 commit 41c2752
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/IO/S3/Client.cpp
Expand Up @@ -574,6 +574,9 @@ Client::doRequest(RequestType & request, RequestFn request_fn) const
if (!new_uri)
return result;

if (initial_endpoint.substr(11) == "amazonaws.com") // Check if user didn't mention any region
new_uri->addRegionToURI(request.getRegionOverride());

const auto & current_uri_override = request.getURIOverride();
/// we already tried with this URI
if (current_uri_override && current_uri_override->uri == new_uri->uri)
Expand Down
5 changes: 5 additions & 0 deletions src/IO/S3/Requests.h
Expand Up @@ -58,6 +58,11 @@ class ExtendedRequest : public BaseRequest
return BaseRequest::GetChecksumAlgorithmName();
}

std::string getRegionOverride() const
{
return region_override;
}

void overrideRegion(std::string region) const
{
region_override = std::move(region);
Expand Down
6 changes: 6 additions & 0 deletions src/IO/S3/URI.cpp
Expand Up @@ -146,6 +146,12 @@ URI::URI(const std::string & uri_)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Bucket or key name are invalid in S3 URI.");
}

void URI::addRegionToURI(const std::string &region)
{
if (auto pos = endpoint.find("amazonaws.com"); pos != std::string::npos)
endpoint = endpoint.substr(0, pos) + region + "." + endpoint.substr(pos);
}

void URI::validateBucket(const String & bucket, const Poco::URI & uri)
{
/// S3 specification requires at least 3 and at most 63 characters in bucket name.
Expand Down
1 change: 1 addition & 0 deletions src/IO/S3/URI.h
Expand Up @@ -32,6 +32,7 @@ struct URI

URI() = default;
explicit URI(const std::string & uri_);
void addRegionToURI(const std::string & region);

static void validateBucket(const std::string & bucket, const Poco::URI & uri);
};
Expand Down

0 comments on commit 41c2752

Please sign in to comment.