Skip to content

Commit

Permalink
S3: Return ETag in 304 responses from get_object (#8535)
Browse files Browse the repository at this point in the history
  • Loading branch information
apinkney97 authored Feb 2, 2025
1 parent 8bc37d8 commit a67975b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 9 additions & 3 deletions moto/s3/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1501,14 +1501,20 @@ def _key_response_get(self, query: Dict[str, Any], key_name: str) -> TYPE_RESPON
def get_object(self) -> TYPE_RESPONSE:
key, not_modified = self._get_key()
response_headers = self._get_cors_headers_other()
if not_modified:
return 304, response_headers, "Not Modified"

if key.version_id != "null":
response_headers["x-amz-version-id"] = key.version_id

response_headers.update(key.metadata)
response_headers.update(key.response_dict)

if not_modified:
# Real S3 omits any content-* headers for a 304
for header in list(response_headers.keys()):
if header.startswith("content-"):
response_headers.pop(header)
return 304, response_headers, "Not Modified"

response_headers.update(key.metadata)
response_headers.update({"Accept-Ranges": "bytes"})

part_number = self._get_int_param("partNumber")
Expand Down
2 changes: 2 additions & 0 deletions tests/test_s3/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2127,6 +2127,7 @@ def test_get_object_if_none_match():
s3_client.get_object(Bucket=bucket_name, Key=key, IfNoneMatch=etag)
err_value = err.value
assert err_value.response["Error"] == {"Code": "304", "Message": "Not Modified"}
assert err_value.response["ResponseMetadata"]["HTTPHeaders"]["etag"] == etag


@mock_aws
Expand Down Expand Up @@ -2227,6 +2228,7 @@ def test_head_object_if_none_match():
s3_client.head_object(Bucket=bucket_name, Key=key, IfNoneMatch=etag)
err_value = err.value
assert err_value.response["Error"] == {"Code": "304", "Message": "Not Modified"}
assert err_value.response["ResponseMetadata"]["HTTPHeaders"]["etag"] == etag


@mock_aws
Expand Down

0 comments on commit a67975b

Please # to comment.