Clean up code for listing s3 keys. Fix #14.

This commit is contained in:
Steve Pulec 2013-05-07 00:03:05 -04:00
parent 6ceb36161c
commit 4c8244b755
3 changed files with 15 additions and 6 deletions

View File

@ -104,17 +104,18 @@ class S3Backend(BaseBackend):
if prefix:
for key_name, key in bucket.keys.iteritems():
if key_name.startswith(prefix):
if delimiter and '/' in key_name.lstrip(prefix):
key_without_prefix = key_name.replace(prefix, "", 1)
if delimiter and delimiter in key_without_prefix:
# If delimiter, we need to split out folder_results
key_without_prefix = "{}/".format(key_name.lstrip(prefix).split("/")[0])
folder_results.add("{}{}".format(prefix, key_without_prefix))
key_without_delimiter = key_without_prefix.split(delimiter)[0]
folder_results.add("{}{}{}".format(prefix, key_without_delimiter, delimiter))
else:
key_results.add(key)
else:
for key_name, key in bucket.keys.iteritems():
if delimiter and '/' in key_name:
if delimiter and delimiter in key_name:
# If delimiter, we need to split out folder_results
folder_results.add(key_name.split("/")[0])
folder_results.add(key_name.split(delimiter)[0])
else:
key_results.add(key)

View File

@ -37,7 +37,7 @@ def _bucket_response(request, full_url, headers):
bucket = s3_backend.get_bucket(bucket_name)
if bucket:
prefix = querystring.get('prefix', [None])[0]
delimiter = querystring.get('delimiter')
delimiter = querystring.get('delimiter', [None])[0]
result_keys, result_folders = s3_backend.prefix_query(bucket, prefix, delimiter)
template = Template(S3_BUCKET_GET_RESPONSE)
return template.render(

View File

@ -227,3 +227,11 @@ def test_bucket_key_listing_order():
delimiter = '/'
keys = [x.name for x in bucket.list(prefix=None, delimiter=delimiter)]
keys.should.equal(['toplevel'])
delimiter = None
keys = [x.name for x in bucket.list(prefix + 'x', delimiter)]
keys.should.equal([u'toplevel/x/key', u'toplevel/x/y/key', u'toplevel/x/y/z/key'])
delimiter = '/'
keys = [x.name for x in bucket.list(prefix + 'x', delimiter)]
keys.should.equal([u'toplevel/x/'])