Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3: Return ETag in 304 responses from get_object #8535

Merged
merged 1 commit into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2103,6 +2103,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 @@ -2203,6 +2204,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
Loading