Skip to content

Commit

Permalink
Switching S3 load_from_filename to infer the mimetype from the local …
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
spiralman and tjensen committed Jun 16, 2020
1 parent 4bf5213 commit 1ac35ca
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion storage/s3_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
8 changes: 4 additions & 4 deletions tests/test_s3_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 1ac35ca

Please sign in to comment.