From 1ac35ca88e6be16a4196431b94ae2a9dd99178bd Mon Sep 17 00:00:00 2001 From: Thomas Stephens Date: Tue, 16 Jun 2020 13:43:45 -0500 Subject: [PATCH] Switching S3 load_from_filename to infer the mimetype from the local filename, rather than the remote storage URI, so that it works the same as Google storage and load_from_directory Co-authored-by: Tim Jensen --- storage/s3_storage.py | 2 +- tests/test_s3_storage.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/storage/s3_storage.py b/storage/s3_storage.py index 666f392..c510408 100644 --- a/storage/s3_storage.py +++ b/storage/s3_storage.py @@ -75,7 +75,7 @@ def load_from_filename(self, file_path: str) -> None: client = self._connect() extra_args = None - content_type = mimetypes.guess_type(self._storage_uri)[0] + content_type = mimetypes.guess_type(file_path)[0] if content_type is not None: extra_args = {"ContentType": content_type} diff --git a/tests/test_s3_storage.py b/tests/test_s3_storage.py index f71e4d4..252d45a 100644 --- a/tests/test_s3_storage.py +++ b/tests/test_s3_storage.py @@ -113,16 +113,16 @@ def test_load_from_filename( @mock.patch("boto3.s3.transfer.S3Transfer", autospec=True) @mock.patch("boto3.session.Session", autospec=True) - def test_load_fromname_file_guesses_content_type_based_on_filename( + def test_load_from_filename_guesses_content_type_based_on_filename( self, mock_session_class: mock.Mock, mock_transfer_class: mock.Mock) -> None: mock_transfer = mock_transfer_class.return_value - storage = get_storage("s3://access_key:access_secret@bucket/some/whatever.jpeg") + storage = get_storage("s3://access_key:access_secret@bucket/some/file") - storage.load_from_filename("source/file") + storage.load_from_filename("source/whatever.jpeg") mock_transfer.upload_file.assert_called_with( - "source/file", "bucket", "some/whatever.jpeg", extra_args={"ContentType": "image/jpeg"}) + "source/whatever.jpeg", "bucket", "some/file", extra_args={"ContentType": "image/jpeg"}) @mock.patch("boto3.session.Session", autospec=True) def test_save_to_file(self, mock_session_class: mock.Mock) -> None: