Add S3 location response. Closes #279.

This commit is contained in:
Steve Pulec 2014-12-10 20:44:00 -05:00
parent a2e56afef8
commit 4ab3b318eb
3 changed files with 23 additions and 4 deletions

View File

@ -160,12 +160,17 @@ class FakeMultipart(object):
class FakeBucket(object):
def __init__(self, name):
def __init__(self, name, region_name):
self.name = name
self.region_name = region_name
self.keys = _VersionedKeyStore()
self.multiparts = {}
self.versioning_status = None
@property
def location(self):
return self.region_name
@property
def is_versioned(self):
return self.versioning_status == 'Enabled'
@ -184,10 +189,10 @@ class S3Backend(BaseBackend):
def __init__(self):
self.buckets = {}
def create_bucket(self, bucket_name):
def create_bucket(self, bucket_name, region_name):
if bucket_name in self.buckets:
raise BucketAlreadyExists()
new_bucket = FakeBucket(name=bucket_name)
new_bucket = FakeBucket(name=bucket_name, region_name=region_name)
self.buckets[bucket_name] = new_bucket
return new_bucket

View File

@ -89,6 +89,10 @@ class ResponseObject(object):
return 200, headers, template.render(
bucket_name=bucket_name,
uploads=multiparts)
elif 'location' in querystring:
bucket = self.backend.get_bucket(bucket_name)
template = Template(S3_BUCKET_LOCATION)
return 200, headers, template.render(location=bucket.location)
elif 'versioning' in querystring:
versioning = self.backend.get_bucket_versioning(bucket_name)
template = Template(S3_BUCKET_GET_VERSIONING)
@ -148,7 +152,7 @@ class ResponseObject(object):
return 404, headers, ""
else:
try:
new_bucket = self.backend.create_bucket(bucket_name)
new_bucket = self.backend.create_bucket(bucket_name, region_name)
except BucketAlreadyExists:
if region_name == DEFAULT_REGION_NAME:
# us-east-1 has different behavior
@ -470,6 +474,9 @@ S3_DELETE_BUCKET_WITH_ITEMS_ERROR = """<?xml version="1.0" encoding="UTF-8"?>
<HostId>sdfgdsfgdsfgdfsdsfgdfs</HostId>
</Error>"""
S3_BUCKET_LOCATION = """<?xml version="1.0" encoding="UTF-8"?>
<LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/">{{ location }}</LocationConstraint>"""
S3_BUCKET_VERSIONING = """
<?xml version="1.0" encoding="UTF-8"?>
<VersioningConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">

View File

@ -685,3 +685,10 @@ def test_setting_content_encoding():
key = bucket.get_key("keyname")
key.content_encoding.should.equal("gzip")
@mock_s3
def test_bucket_location():
conn = boto.s3.connect_to_region("us-west-2")
bucket = conn.create_bucket('mybucket')
bucket.get_location().should.equal("us-west-2")