add get_all_buckets
This commit is contained in:
parent
28a46a5a3a
commit
ba360daa0f
@ -23,7 +23,7 @@ class FakeBucket(object):
|
||||
|
||||
|
||||
class S3Backend(BaseBackend):
|
||||
base_url = "https://(.+).s3.amazonaws.com"
|
||||
base_url = "https://(.*)s3.amazonaws.com"
|
||||
|
||||
def __init__(self):
|
||||
self.buckets = {}
|
||||
@ -33,6 +33,9 @@ class S3Backend(BaseBackend):
|
||||
self.buckets[bucket_name] = new_bucket
|
||||
return new_bucket
|
||||
|
||||
def get_all_buckets(self):
|
||||
return self.buckets.values()
|
||||
|
||||
def get_bucket(self, bucket_name):
|
||||
return self.buckets.get(bucket_name)
|
||||
|
||||
|
@ -4,9 +4,17 @@ from .models import s3_backend
|
||||
|
||||
def bucket_response(uri, body, headers):
|
||||
hostname = uri.hostname
|
||||
bucket_name = hostname.replace(".s3.amazonaws.com", "")
|
||||
method = uri.method
|
||||
|
||||
s3_base_url = "s3.amazonaws.com"
|
||||
if hostname == s3_base_url:
|
||||
# No bucket specified. Listing all buckets
|
||||
all_buckets = s3_backend.get_all_buckets()
|
||||
template = Template(S3_ALL_BUCKETS)
|
||||
return template.render(buckets=all_buckets)
|
||||
|
||||
bucket_name = hostname.replace(".s3.amazonaws.com", "")
|
||||
|
||||
if method == 'GET':
|
||||
bucket = s3_backend.get_bucket(bucket_name)
|
||||
if bucket:
|
||||
@ -66,6 +74,21 @@ def key_response(uri_info, body, headers):
|
||||
import pdb;pdb.set_trace()
|
||||
|
||||
|
||||
S3_ALL_BUCKETS = """<ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01">
|
||||
<Owner>
|
||||
<ID>bcaf1ffd86f41161ca5fb16fd081034f</ID>
|
||||
<DisplayName>webfile</DisplayName>
|
||||
</Owner>
|
||||
<Buckets>
|
||||
{% for bucket in buckets %}
|
||||
<Bucket>
|
||||
<Name>{{ bucket.name }}</Name>
|
||||
<CreationDate>2006-02-03T16:45:09.000Z</CreationDate>
|
||||
</Bucket>
|
||||
{% endfor %}
|
||||
</Buckets>
|
||||
</ListAllMyBucketsResult>"""
|
||||
|
||||
S3_BUCKET_GET_RESPONSE = """<ListBucket xmlns="http://doc.s3.amazonaws.com/2006-03-01">\
|
||||
<Bucket>{{ bucket.name }}</Bucket>\
|
||||
<Prefix>notes/</Prefix>\
|
||||
|
@ -53,3 +53,13 @@ def test_bucket_deletion():
|
||||
conn.delete_bucket("foobar")
|
||||
|
||||
conn.get_bucket.when.called_with("foobar").should.throw(S3ResponseError)
|
||||
|
||||
|
||||
@mock_s3
|
||||
def test_get_all_buckets():
|
||||
conn = boto.connect_s3('the_key', 'the_secret')
|
||||
bucket = conn.create_bucket("foobar")
|
||||
bucket = conn.create_bucket("foobar2")
|
||||
buckets = conn.get_all_buckets()
|
||||
|
||||
buckets.should.have.length_of(2)
|
||||
|
Loading…
Reference in New Issue
Block a user