Skip to content

Commit

Permalink
[s3] do not override a returned ContentType from get_object_parameters (
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneier authored Aug 29, 2023
1 parent e7e5bb3 commit e5ac756
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
17 changes: 8 additions & 9 deletions storages/backends/s3boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,17 +481,16 @@ def size(self, name):
return self.bucket.Object(name).content_length

def _get_write_parameters(self, name, content=None):
params = {}
params = self.get_object_parameters(name)

_type, encoding = mimetypes.guess_type(name)
content_type = getattr(content, 'content_type', None)
content_type = content_type or _type or self.default_content_type
if 'ContentType' not in params:
_type, encoding = mimetypes.guess_type(name)
content_type = getattr(content, 'content_type', None)
content_type = content_type or _type or self.default_content_type

params['ContentType'] = content_type
if encoding:
params['ContentEncoding'] = encoding

params.update(self.get_object_parameters(name))
params['ContentType'] = content_type
if encoding:
params['ContentEncoding'] = encoding

if 'ACL' not in params and self.default_acl:
params['ACL'] = self.default_acl
Expand Down
19 changes: 19 additions & 0 deletions tests/test_s3boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,25 @@ def test_storage_save_gzipped(self):
Config=self.storage.transfer_config
)

def test_content_type_set_explicitly(self):
name = "test_file.gz"
content = ContentFile("data")

def get_object_parameters(name):
return {"ContentType": "application/gzip"}

self.storage.get_object_parameters = get_object_parameters
self.storage.save(name, content)

obj = self.storage.bucket.Object.return_value
obj.upload_fileobj.assert_called_with(
content,
ExtraArgs={
"ContentType": "application/gzip",
},
Config=self.storage.transfer_config
)

def test_storage_save_gzipped_non_seekable(self):
"""
Test saving a gzipped file
Expand Down

0 comments on commit e5ac756

Please sign in to comment.