Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regional Europe/Asia buckets support (tests included) #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/aws/s3/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ module AWS #:nodoc:
module S3
constant :DEFAULT_HOST, 's3.amazonaws.com'

# See: http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RequestEndpoints.html
ENDPOINTS = {
:us => 's3-us-west-1.amazonaws.com',
:asia => 's3-ap-southeast-1.amazonaws.com',
:europe => 's3-eu-west-1.amazonaws.com'
}

# AWS::S3::Base is the abstract super class of all classes who make requests against S3, such as the built in
# Service, Bucket and S3Object classes. It provides methods for making requests, inferring or setting response classes,
# processing request options, and accessing attributes from S3's response data.
Expand Down
9 changes: 7 additions & 2 deletions lib/aws/s3/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ module ClassMethods
#
# * <tt>:server</tt> - The server to make requests to. You can use this to specify your bucket in the subdomain,
# or your own domain's cname if you are using virtual hosted buckets. Defaults to <tt>s3.amazonaws.com</tt>.
# * <tt>:region</tt> - The region from which to take the default server. You can use this to ovverride the default
# server if you used a regional bucket from <tt>:europe</tt>, <tt>:asia</tt> or <tt>:us</tt>. This can't be used
# together with the <tt>:server</tt> setting. Defaults to <tt>nil</tt>.
# * <tt>:port</tt> - The port to the requests should be made on. Defaults to 80 or 443 if the <tt>:use_ssl</tt>
# argument is set.
# * <tt>:use_ssl</tt> - Whether requests should be made over SSL. If set to true, the <tt>:port</tt> argument
Expand Down Expand Up @@ -251,12 +254,12 @@ def default_connection
end

class Options < Hash #:nodoc:
VALID_OPTIONS = [:access_key_id, :secret_access_key, :server, :port, :use_ssl, :persistent, :proxy].freeze
VALID_OPTIONS = [:access_key_id, :secret_access_key, :server, :region, :port, :use_ssl, :persistent, :proxy].freeze

def initialize(options = {})
super()
validate(options)
replace(:server => DEFAULT_HOST, :port => (options[:use_ssl] ? 443 : 80))
replace(:server => options[:region] ? ENDPOINTS[options[:region]] : DEFAULT_HOST, :port => (options[:use_ssl] ? 443 : 80))
merge!(options)
end

Expand All @@ -273,6 +276,8 @@ def validate(options)
invalid_options = options.keys - VALID_OPTIONS
raise InvalidConnectionOption.new(invalid_options) unless invalid_options.empty?
raise ArgumentError, "Missing proxy settings. Must specify at least :host." if options[:proxy] && !options[:proxy][:host]
raise ArgumentError, "Ambiguous server/region option: please don't use :server and :region options together." if options[:server] && options[:region]
raise ArgumentError, "Bad region: please use one of #{ENDPOINTS.keys.map(&:inspect).join(', ')}." if options[:region] && !ENDPOINTS.key?(options[:region])
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions test/connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,23 @@ def test_recognizing_that_the_settings_want_to_connect_through_a_proxy
assert options.connecting_through_proxy?
end

def test_server_and_region_settings_cant_live_together
assert_raises(ArgumentError) do
generate_options(:region => :europe, :server => "example.org")
end
end

def test_server_setting_is_extracted_from_region
options = generate_options(:region => :europe)
assert_equal ENDPOINTS[:europe], options[:server]
end

def test_region_is_a_valid_s3_endpoint
assert_raises(ArgumentError) do
generate_options(:region => :antartica)
end
end

private
def assert_key_transfered(key, value, options)
assert_equal value, options[key]
Expand Down