Skip to content

Commit

Permalink
correctly evaluate region in CAmazonS3Client::CreateRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
Zer0xFF committed May 18, 2022
1 parent 4579db6 commit eb91000
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion include/amazon/AmazonS3Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ class CAmazonS3Client : public CAmazonClient
void PutObject(const PutObjectRequest&);

private:
Request CreateRequest(Framework::Http::HTTP_VERB, std::string, std::string = "");
Request CreateRequest(Framework::Http::HTTP_VERB, std::string, std::string, std::string = "");
};
17 changes: 8 additions & 9 deletions src/amazon/AmazonS3Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CAmazonS3Client::CAmazonS3Client(CAmazonConfigs configs, std::string region)
{
}

CAmazonClient::Request CAmazonS3Client::CreateRequest(Framework::Http::HTTP_VERB method, std::string bucket, std::string path)
CAmazonClient::Request CAmazonS3Client::CreateRequest(Framework::Http::HTTP_VERB method, std::string bucket, std::string region, std::string path)
{
Request rq;
rq.method = method;
Expand Down Expand Up @@ -40,14 +40,14 @@ CAmazonClient::Request CAmazonS3Client::CreateRequest(Framework::Http::HTTP_VERB
}

std::string endpoint = "amazonaws.com";
if(m_region.empty())
if(region.empty())
{
rq.host = string_format("%s.s3.%s", bucket.c_str(), endpoint.c_str());
rq.urlHost = string_format("s3.%s", endpoint.c_str());
}
else
{
rq.host = string_format("%s.s3-%s.%s", bucket.c_str(), m_region.c_str(), endpoint.c_str());
rq.host = string_format("%s.s3-%s.%s", bucket.c_str(), region.c_str(), endpoint.c_str());
rq.urlHost = rq.host;
}
return rq;
Expand All @@ -68,8 +68,7 @@ GetBucketLocationResult CAmazonS3Client::GetBucketLocation(const GetBucketLocati
{
throw new std::runtime_error("Bucket name must be provided.");
}

Request rq = CreateRequest(Framework::Http::HTTP_VERB::GET, request.bucket);
Request rq = CreateRequest(Framework::Http::HTTP_VERB::GET, request.bucket, "");
//We add a bucket parameter even if the S3 API doesn't use it to prevent caching
rq.query = string_format("bucket=%s&location=", request.bucket.c_str());

Expand All @@ -92,7 +91,7 @@ GetBucketLocationResult CAmazonS3Client::GetBucketLocation(const GetBucketLocati

GetObjectResult CAmazonS3Client::GetObject(const GetObjectRequest& request)
{
Request rq = CreateRequest(Framework::Http::HTTP_VERB::GET, request.bucket, Framework::UrlEncode(request.key));
Request rq = CreateRequest(Framework::Http::HTTP_VERB::GET, request.bucket, m_region, Framework::UrlEncode(request.key));

if(request.range.first != request.range.second)
{
Expand All @@ -117,7 +116,7 @@ GetObjectResult CAmazonS3Client::GetObject(const GetObjectRequest& request)

HeadObjectResult CAmazonS3Client::HeadObject(const HeadObjectRequest& request)
{
Request rq = CreateRequest(Framework::Http::HTTP_VERB::HEAD, request.bucket, Framework::UrlEncode(request.key));
Request rq = CreateRequest(Framework::Http::HTTP_VERB::HEAD, request.bucket, m_region, Framework::UrlEncode(request.key));

auto response = ExecuteRequest(rq);
if(response.statusCode != Framework::Http::HTTP_STATUS_CODE::OK)
Expand Down Expand Up @@ -145,7 +144,7 @@ HeadObjectResult CAmazonS3Client::HeadObject(const HeadObjectRequest& request)

ListObjectsResult CAmazonS3Client::ListObjects(std::string bucket)
{
Request rq = CreateRequest(Framework::Http::HTTP_VERB::GET, bucket);
Request rq = CreateRequest(Framework::Http::HTTP_VERB::GET, bucket, m_region);

auto response = ExecuteRequest(rq);
if(response.statusCode != Framework::Http::HTTP_STATUS_CODE::OK)
Expand All @@ -172,7 +171,7 @@ ListObjectsResult CAmazonS3Client::ListObjects(std::string bucket)

void CAmazonS3Client::PutObject(const PutObjectRequest& request)
{
Request rq = CreateRequest(Framework::Http::HTTP_VERB::PUT, Framework::UrlEncode(request.key));
Request rq = CreateRequest(Framework::Http::HTTP_VERB::PUT, request.bucket.c_str(), m_region, Framework::UrlEncode(request.key));
rq.content = request.data;

auto response = ExecuteRequest(rq);
Expand Down

0 comments on commit eb91000

Please sign in to comment.