diff --git a/storages/backends/gcloud.py b/storages/backends/gcloud.py index e9855752..e6169d77 100644 --- a/storages/backends/gcloud.py +++ b/storages/backends/gcloud.py @@ -62,10 +62,9 @@ def _get_file(self): ) if "r" in self._mode: self._is_dirty = False + # This automatically decompresses the file self.blob.download_to_file(self._file, checksum="crc32c") self._file.seek(0) - if self._storage.gzip and self.blob.content_encoding == "gzip": - self._file = self._decompress_file(mode=self._mode, file=self._file) return self._file def _set_file(self, value): diff --git a/tests/test_gcloud.py b/tests/test_gcloud.py index 1b8a040d..67dbfab2 100644 --- a/tests/test_gcloud.py +++ b/tests/test_gcloud.py @@ -14,6 +14,7 @@ from google.cloud.storage.retry import DEFAULT_RETRY from storages.backends import gcloud +from storages.backends.gcloud import GoogleCloudFile class GCloudTestCase(TestCase): @@ -571,3 +572,17 @@ def test_storage_save_gzip(self, *args): self.assertEqual(zfile.read(), b"I should be gzip'd") finally: patcher.stop() + + def test_storage_read_gzip(self, *args): + """ + Test reading a gzipped file decompresses content only once. + """ + name = "test_storage_save.css" + file = GoogleCloudFile(name, "rb", self.storage) + blob = mock.MagicMock() + file.blob = blob + blob.download_to_file = lambda f, checksum=None: f.write(b"No gzip") + blob.content_encoding = "gzip" + f = file._get_file() + + f.read() # This should not fail