Skip to content

Commit

Permalink
Improve URL generation by using region and improving virtual domain s…
Browse files Browse the repository at this point in the history
…tyle addressing

See http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
for a list of all endpoints.
  • Loading branch information
workeitel committed Jan 13, 2016
1 parent be528e0 commit 8dca2e5
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions s3_file/libraries/s3_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,7 @@ def self.with_region_detect(region = nil)
end

def self.do_request(method, url, bucket, path, aws_access_key_id, aws_secret_access_key, token, region)
if url.nil?
url = 'https://' + (
if bucket =~ /\./
"s3.amazonaws.com/#{bucket}"
else
"#{bucket}.s3.amazonaws.com"
end
)
end
url = build_endpoint_url(bucket, region) if url.nil?

with_region_detect(region) do |real_region|
client.reset_before_execution_procs
Expand All @@ -110,6 +102,20 @@ def self.do_request(method, url, bucket, path, aws_access_key_id, aws_secret_acc
end
end

def self.build_endpoint_url(bucket, region)
endpoint = if region && region != "us-east-1"
"s3-#{region}.amazonaws.com"
else
"s3.amazonaws.com"
end

if bucket =~ /^[a-z0-9][a-z0-9-]+[a-z0-9]$/
"https://#{bucket}.#{endpoint}"
else
"https://#{endpoint}/#{bucket}"
end
end

def self.get_md5_from_s3(bucket, url, path, aws_access_key_id, aws_secret_access_key, token, region = nil)
get_digests_from_s3(bucket, url, path, aws_access_key_id, aws_secret_access_key, token, region)["md5"]
end
Expand Down

0 comments on commit 8dca2e5

Please sign in to comment.