mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
AWS | storage: fixes ignored location with new buckets
When creating a new directory the location wasn't forwarded properly. Something like the following is now working as expected (see limitation below): Fog::Storage[:aws].directories.create(key: 'bucket', location: 'us-west-1') It also changes the following behaviour: when no location is specified new buckets are explicitly created in the same region as the service connection. Known limitations: Fog::AWS::Directory instance don't use their location to connect to S3. If you're using an AWS::Storage with a region different than us-east-1 you can only create buckets in the same region.
This commit is contained in:
parent
62f07402b3
commit
f36a3065a9
3 changed files with 27 additions and 12 deletions
|
@ -9,14 +9,12 @@ module Fog
|
|||
class Directory < Fog::Model
|
||||
VALID_ACLS = ['private', 'public-read', 'public-read-write', 'authenticated-read']
|
||||
|
||||
# See http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketPUT.html
|
||||
INVALID_LOCATIONS = ['us-east-1']
|
||||
|
||||
attr_reader :acl
|
||||
|
||||
identity :key, :aliases => ['Name', 'name']
|
||||
|
||||
attribute :creation_date, :aliases => 'CreationDate'
|
||||
attribute :creation_date, :aliases => 'CreationDate', :type => 'time'
|
||||
attribute :location, :aliases => 'LocationConstraint', :type => 'string'
|
||||
|
||||
def acl=(new_acl)
|
||||
unless VALID_ACLS.include?(new_acl)
|
||||
|
@ -35,16 +33,12 @@ module Fog
|
|||
end
|
||||
|
||||
def location
|
||||
requires :key
|
||||
attributes[:location] || bucket_location || self.service.region
|
||||
@location ||= (bucket_location || self.service.region)
|
||||
end
|
||||
|
||||
# NOTE: you can't change the region once the bucket is created
|
||||
def location=(new_location)
|
||||
if INVALID_LOCATIONS.include?(new_location)
|
||||
raise ArgumentError, "location must not include any of #{INVALID_LOCATIONS.join(', ')}. See http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketPUT.html"
|
||||
else
|
||||
@location = new_location
|
||||
end
|
||||
@location = new_location
|
||||
end
|
||||
|
||||
def files
|
||||
|
@ -103,7 +97,9 @@ module Fog
|
|||
|
||||
options['x-amz-acl'] = acl if acl
|
||||
|
||||
if location = attributes[:location] || (self.service.region != 'us-east-1' && self.service.region)
|
||||
# http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUT.html
|
||||
# Ignore the default region us-east-1
|
||||
if !persisted? && location != DEFAULT_REGION
|
||||
options['LocationConstraint'] = location
|
||||
end
|
||||
|
||||
|
@ -122,6 +118,8 @@ module Fog
|
|||
private
|
||||
|
||||
def bucket_location
|
||||
requires :key
|
||||
return nil unless persisted?
|
||||
data = service.get_bucket_location(key)
|
||||
data.body['LocationConstraint']
|
||||
end
|
||||
|
|
|
@ -6,6 +6,8 @@ module Fog
|
|||
class AWS < Fog::Service
|
||||
extend Fog::AWS::CredentialFetcher::ServiceMethods
|
||||
|
||||
DEFAULT_REGION = 'us-east-1'
|
||||
|
||||
requires :aws_access_key_id, :aws_secret_access_key
|
||||
recognizes :endpoint, :region, :host, :path, :port, :scheme, :persistent, :use_iam_profile, :aws_session_token, :aws_credentials_expire_at, :path_style
|
||||
|
||||
|
|
|
@ -21,6 +21,21 @@ Shindo.tests("Storage[:aws] | directory", ["aws"]) do
|
|||
end
|
||||
end
|
||||
|
||||
directory_attributes = {
|
||||
:key => uniq_id('different-region'),
|
||||
:location => 'eu-west-1',
|
||||
}
|
||||
|
||||
model_tests(Fog::Storage[:aws].directories, directory_attributes, Fog.mocking?) do
|
||||
tests("#location").returns('eu-west-1') do
|
||||
@instance.location
|
||||
end
|
||||
|
||||
tests("#location").returns('eu-west-1') do
|
||||
Fog::Storage[:aws].directories.get(@instance.identity).location
|
||||
end
|
||||
end
|
||||
|
||||
directory_attributes = {
|
||||
:key => uniq_id('fogdirectorytests')
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue