diff --git a/moto/s3/models.py b/moto/s3/models.py index 0a84ba731..69ef827ff 100644 --- a/moto/s3/models.py +++ b/moto/s3/models.py @@ -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) diff --git a/moto/s3/responses.py b/moto/s3/responses.py index 1d69384da..766c06e9f 100644 --- a/moto/s3/responses.py +++ b/moto/s3/responses.py @@ -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( diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index 264b8bb08..54da1b9ac 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -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/'])