S3:list_object_versions(): Fix delimiter to take prefix into account (#5562)
This commit is contained in:
parent
121e3eadb8
commit
5f7f3e6e46
@ -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
|
||||
|
||||
|
@ -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():
|
||||
|
Loading…
Reference in New Issue
Block a user