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

[gcloud] Fix saving already gzipped files & tests #1366

Merged
merged 1 commit into from
Mar 15, 2024
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
7 changes: 5 additions & 2 deletions storages/backends/gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
class GoogleCloudFile(CompressedFileMixin, File):
def __init__(self, name, mode, storage):
self.name = name
self.mime_type = mimetypes.guess_type(name)[0]
self.mime_type, self.mime_encoding = mimetypes.guess_type(name)
self._mode = mode
self._storage = storage
self.blob = storage.bucket.get_blob(name, chunk_size=storage.blob_chunk_size)
Expand Down Expand Up @@ -190,8 +190,11 @@ def _save(self, name, content):
content.name = cleaned_name
file_object = GoogleCloudFile(name, "rw", self)

upload_params = {}
blob_params = self.get_object_parameters(name)
if file_object.mime_encoding and CONTENT_ENCODING not in blob_params:
blob_params[CONTENT_ENCODING] = file_object.mime_encoding

upload_params = {}
upload_params["predefined_acl"] = blob_params.pop("acl", self.default_acl)
upload_params[CONTENT_TYPE] = blob_params.pop(
CONTENT_TYPE, file_object.mime_type
Expand Down
16 changes: 6 additions & 10 deletions tests/test_gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@ def setUp(self):
self.bucket_name = "test_bucket"
self.filename = "test_file.txt"
self.storage = gcloud.GoogleCloudStorage(bucket_name=self.bucket_name)


class GCloudStorageTests(GCloudTestCase):
def setUp(self):
super().setUp()
self.client_patcher = mock.patch("storages.backends.gcloud.Client")
self.client_patcher.start()

def tearDown(self):
super().tearDown()
self.client_patcher.stop()


class GCloudStorageTests(GCloudTestCase):
def test_open_read(self):
"""
Test opening a file and reading from it
Expand Down Expand Up @@ -507,7 +505,6 @@ def setUp(self):
self.storage.gzip = True

@mock.patch("google.cloud.storage.blob.Blob._do_upload")
@mock.patch("google.auth.default", return_value=["foo", None])
def test_storage_save_gzipped(self, *args):
"""
Test saving a gzipped file
Expand All @@ -521,24 +518,23 @@ def test_storage_save_gzipped(self, *args):
try:
patcher.start()
self.storage.save(name, content)
blob.upload_from_file.assert_called_with(
obj = self.storage._bucket.get_blob()
obj.upload_from_file.assert_called_with(
mock.ANY,
rewind=True,
retry=DEFAULT_RETRY,
size=None,
size=11,
predefined_acl=None,
content_type="application/javascript",
)
finally:
patcher.stop()

@mock.patch("google.cloud.storage.blob.Blob._do_upload")
@mock.patch("google.auth.default", return_value=["foo", None])
def test_storage_save_gzip(self, *args):
"""
Test saving a file with gzip enabled.
"""
self.storage.gzip = True
name = "test_storage_save.css"
content = ContentFile("I should be gzip'd")

Expand Down
Loading