Skip to content

Commit a3ced98

Browse files
authored
S3: get_object() now honors ResponseContentDisposition etc headers (#10057)
1 parent d0cf348 commit a3ced98

3 files changed

Lines changed: 84 additions & 0 deletions

File tree

‎moto/s3/responses.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@
174174
},
175175
}
176176

177+
ALLOWED_HEADER_OVERRIDES = {
178+
"response-content-type": "content-type",
179+
"response-content-language": "Content-Language",
180+
"response-expires": "Expires",
181+
"response-cache-control": "Cache-Control",
182+
"response-content-disposition": "Content-Disposition",
183+
"response-content-encoding": "Content-Encoding",
184+
}
185+
177186

178187
def parse_key_name(pth: str) -> str:
179188
# strip the first '/' left by urlparse
@@ -2000,6 +2009,10 @@ def get_object(self) -> TYPE_RESPONSE:
20002009
response_headers.update(key.metadata)
20012010
response_headers.update({"Accept-Ranges": "bytes"})
20022011

2012+
for param_name, header_name in ALLOWED_HEADER_OVERRIDES.items():
2013+
if param_name in self.querystring:
2014+
response_headers[header_name] = self.querystring[param_name][0]
2015+
20032016
part_number = self._get_int_param("partNumber")
20042017
if part_number:
20052018
if key.multipart:

‎tests/test_s3/test_s3.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3172,6 +3172,30 @@ def test_object_headers():
31723172
assert "BucketKeyEnabled" in res
31733173

31743174

3175+
@s3_aws_verified
3176+
def test_get_object_returns_provided_response_headers(bucket_name=None):
3177+
s3_client = boto3.client("s3", region_name=DEFAULT_REGION_NAME)
3178+
3179+
s3_client.put_object(Bucket=bucket_name, Body=b"test", Key="file.txt")
3180+
3181+
res = s3_client.get_object(
3182+
Bucket=bucket_name,
3183+
Key="file.txt",
3184+
ResponseCacheControl="max-age=67",
3185+
ResponseContentDisposition="attachment=stuff",
3186+
ResponseContentEncoding="identity",
3187+
ResponseContentLanguage="fr-FR",
3188+
ResponseContentType="image/jpegxl",
3189+
ResponseExpires=datetime.datetime(2015, 1, 1),
3190+
)
3191+
3192+
assert res.get("CacheControl") == "max-age=67"
3193+
assert res.get("ContentDisposition") == "attachment=stuff"
3194+
assert res.get("ContentEncoding") == "identity"
3195+
assert res.get("ContentLanguage") == "fr-FR"
3196+
assert res.get("ContentType") == "image/jpegxl"
3197+
3198+
31753199
if settings.TEST_SERVER_MODE:
31763200

31773201
@mock_aws
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import boto3
2+
import pytest
3+
import requests
4+
5+
from moto import settings
6+
from moto.s3.responses import DEFAULT_REGION_NAME
7+
from tests import allow_aws_request
8+
9+
from . import s3_aws_verified
10+
from .test_s3 import add_proxy_details
11+
12+
13+
@s3_aws_verified
14+
@pytest.mark.aws_verified
15+
def test_presigned_url_generates_content_response_headers(bucket_name=None):
16+
client = boto3.client("s3", DEFAULT_REGION_NAME)
17+
s3_resource = boto3.resource("s3", region_name=DEFAULT_REGION_NAME)
18+
bucket = s3_resource.Bucket(bucket_name)
19+
bucket.put_object(Body=b"ABCD", Key="file.txt")
20+
21+
params = {
22+
"Bucket": bucket.name,
23+
"Key": "file.txt",
24+
"ResponseCacheControl": "max-age=74",
25+
"ResponseContentDisposition": 'attachment; filename="foo.jpg"',
26+
"ResponseContentEncoding": "identity",
27+
"ResponseContentLanguage": "de-DE",
28+
"ResponseContentType": "image/jpeg",
29+
"ResponseExpires": "Wed, 21 Oct 2015 07:28:00 GMT",
30+
}
31+
presigned_url = client.generate_presigned_url("get_object", params, ExpiresIn=900)
32+
get_kwargs = {}
33+
if not allow_aws_request() and settings.is_test_proxy_mode():
34+
add_proxy_details(get_kwargs)
35+
response = requests.get(presigned_url, **get_kwargs)
36+
37+
assert response.content == b"ABCD"
38+
39+
assert response.headers.get("Cache-Control") == "max-age=74"
40+
assert (
41+
response.headers.get("Content-Disposition") == 'attachment; filename="foo.jpg"'
42+
)
43+
assert response.headers.get("Content-Encoding") == "identity"
44+
assert response.headers.get("Content-Language") == "de-DE"
45+
assert response.headers.get("Content-Type") == "image/jpeg"
46+
assert response.headers.get("Content-Length") == "4"
47+
assert response.headers.get("Expires") == "Wed, 21 Oct 2015 07:28:00 GMT"

0 commit comments

Comments
 (0)