Last Updated: February 25, 2016
·
2.216K
· groodt

Using aws/s3 gem in non-US regions

If you try to use the 'aws/s3' gem for a bucket that is not created in the US region, then you get a AWS::S3::PermanentRedirect error:

AWS::S3::Base.establish_connection!(
    :access_key_id     => 'KEY',
    :secret_access_key => 'SECRET'
)
#<AWS::S3::Connection:0x007f9c82a0ef58 @options={:server=>"s3.amazonaws.com", :port=>80, :access_key_id=>"KEY", :secret_access_key=>"SECRET"}, @access_key_id="ID", @secret_access_key="SECRET", @http=#<Net::HTTP s3.amazonaws.com:80 open=false>>

bucket = AWS::S3::Bucket.find('mybucket')
AWS::S3::PermanentRedirect: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.

A quick fix for this is if all your buckets are in the same region is to update the DEFAULT_HOST of the module like this:

AWS::S3::DEFAULT_HOST = "s3-eu-west-1.amazonaws.com"

AWS::S3::Base.establish_connection!(
    :access_key_id     => 'KEY',
    :secret_access_key => 'SECRET'
)
#<AWS::S3::Connection:0x007fc26b913090 @options={:server=>"s3-eu-west-1.amazonaws.com", :port=>80, :access_key_id=>"KEY", :secret_access_key=>"SECRET"}, @access_key_id="ID", @secret_access_key="SECRET", @http=#<Net::HTTP s3-eu-west-1.amazonaws.com:80 open=false>>

AWS::S3::Bucket.find('mybucket')
#<AWS::S3::Bucket:0x007fc26bab4048 @attributes={"xmlns"=>"http://s3.amazonaws.com/doc/2006-03-01/", "name"=>"mybucket", "prefix"=>nil, "marker"=>nil, "max_keys"=>1000, "is_truncated"=>false}, @object_cache=[]>

You are then able to connect successfully.