Sort s3 object versions by last_modified_on in descending order (#3588)

* Sort s3 object versions by last_modified_on in descending order

- addresses https://github.com/localstack/localstack/issues/3433

* fix for python 2

* fix lint
This commit is contained in:
Rahul Ranjan 2021-01-15 23:58:28 +05:30 committed by GitHub
parent 7ff60683e0
commit 628c026a07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 5 deletions

View File

@ -6,7 +6,11 @@ import sys
import six
from botocore.awsrequest import AWSPreparedRequest
from moto.core.utils import str_to_rfc_1123_datetime, py2_strip_unicode_keys
from moto.core.utils import (
str_to_rfc_1123_datetime,
py2_strip_unicode_keys,
unix_time_millis,
)
from six.moves.urllib.parse import parse_qs, urlparse, unquote, parse_qsl
import xmltodict
@ -458,6 +462,8 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):
else:
delete_marker_list.append(version)
template = self.response_template(S3_BUCKET_GET_VERSIONS)
key_list.sort(key=lambda r: (r.name, -unix_time_millis(r.last_modified)))
return (
200,
{},

View File

@ -873,12 +873,12 @@ def test_list_versions():
versions.should.have.length_of(2)
versions[0].name.should.equal("the-key")
versions[0].version_id.should.equal(key_versions[0])
versions[0].get_contents_as_string().should.equal(b"Version 1")
versions[0].version_id.should.equal(key_versions[1])
versions[0].get_contents_as_string().should.equal(b"Version 2")
versions[1].name.should.equal("the-key")
versions[1].version_id.should.equal(key_versions[1])
versions[1].get_contents_as_string().should.equal(b"Version 2")
versions[1].version_id.should.equal(key_versions[0])
versions[1].get_contents_as_string().should.equal(b"Version 1")
key = Key(bucket, "the2-key")
key.set_contents_from_string("Version 1")
@ -3700,6 +3700,10 @@ def test_boto3_list_object_versions():
len(response["Versions"]).should.equal(2)
keys = set([item["Key"] for item in response["Versions"]])
keys.should.equal({key})
# the first item in the list should be the latest
response["Versions"][0]["IsLatest"].should.equal(True)
# Test latest object version is returned
response = s3.get_object(Bucket=bucket_name, Key=key)
response["Body"].read().should.equal(items[-1])