diff --git a/moto/s3/models.py b/moto/s3/models.py index 97c374be2..6141bbe4c 100644 --- a/moto/s3/models.py +++ b/moto/s3/models.py @@ -1631,10 +1631,12 @@ class S3Backend(BaseBackend, CloudWatchMetricProvider): # Filter for keys that start with prefix if not name.startswith(prefix): continue - # separate out all keys that contain delimiter - if delimiter and delimiter in name: - index = name.index(delimiter) + len(delimiter) - prefix_including_delimiter = name[0:index] + # separate keys that contain the same string between the prefix and the first occurrence of the delimiter + if delimiter and delimiter in name[len(prefix) :]: + end_of_delimiter = ( + len(prefix) + name[len(prefix) :].index(delimiter) + len(delimiter) + ) + prefix_including_delimiter = name[0:end_of_delimiter] common_prefixes.append(prefix_including_delimiter) continue diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index 427aad34d..7bdbe85e7 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -2463,6 +2463,20 @@ def test_list_object_versions_with_delimiter(): {"key11-without-data", "key12-without-data", "key13-without-data"} ) + # Delimiter with Prefix being the entire key + response = s3.list_object_versions( + Bucket=bucket_name, Prefix="key1-with-data", Delimiter="-" + ) + response.should.have.key("Versions").length_of(3) + response.shouldnt.have.key("CommonPrefixes") + + # Delimiter without prefix + response = s3.list_object_versions(Bucket=bucket_name, Delimiter="-with-") + response["CommonPrefixes"].should.have.length_of(8) + response["CommonPrefixes"].should.contain({"Prefix": "key1-with-"}) + # Should return all keys -without-data + response.should.have.key("Versions").length_of(24) + @mock_s3 def test_list_object_versions_with_delimiter_for_deleted_objects():