diff --git a/storages/backends/gcloud.py b/storages/backends/gcloud.py index 02aa519ba..8cefd12dc 100644 --- a/storages/backends/gcloud.py +++ b/storages/backends/gcloud.py @@ -182,12 +182,12 @@ def _save(self, name, content): if self.gzip and upload_params.get(CONTENT_TYPE) in self.gzip_content_types: content = self._compress_content(content) file_object.blob.content_encoding = 'gzip' - CONTENT_TYPE[CONTENT_ENCODING] = 'gzip' + upload_params[CONTENT_ENCODING] = 'gzip' for prop, val in blob_params.items(): setattr(file_object.blob, prop, val) - file_object.blob.upload_from_file(content, rewind=True, size=content.size, **upload_params) + file_object.blob.upload_from_file(content, rewind=True, size=getattr(content, 'size', None), **upload_params) return cleaned_name def get_object_parameters(self, name): diff --git a/tests/test_gcloud.py b/tests/test_gcloud.py index 1ea18d258..dae551e73 100644 --- a/tests/test_gcloud.py +++ b/tests/test_gcloud.py @@ -11,6 +11,7 @@ from google.cloud.storage.blob import Blob from storages.backends import gcloud +from tests.test_s3boto3 import NonSeekableContentFile class GCloudTestCase(TestCase): @@ -102,7 +103,7 @@ def test_save(self): self.storage._client.bucket.assert_called_with(self.bucket_name) self.storage._bucket.get_blob().upload_from_file.assert_called_with( content, rewind=True, size=len(data), content_type=mimetypes.guess_type(self.filename)[0], - content_encoding=None, predefined_acl=None) + predefined_acl=None) def test_save2(self): data = 'This is some test ủⓝï℅ⅆℇ content.' @@ -114,7 +115,7 @@ def test_save2(self): self.storage._client.bucket.assert_called_with(self.bucket_name) self.storage._bucket.get_blob().upload_from_file.assert_called_with( content, rewind=True, size=len(data), content_type=mimetypes.guess_type(filename)[0], - content_encoding=None, predefined_acl=None) + predefined_acl=None) def test_save_with_default_acl(self): data = 'This is some test ủⓝï℅ⅆℇ content.' @@ -131,7 +132,7 @@ def test_save_with_default_acl(self): self.storage._client.bucket.assert_called_with(self.bucket_name) self.storage._bucket.get_blob().upload_from_file.assert_called_with( content, rewind=True, size=len(data), content_type=mimetypes.guess_type(filename)[0], - content_encoding=None, predefined_acl='publicRead') + predefined_acl='publicRead') def test_delete(self): self.storage.delete(self.filename) @@ -403,26 +404,62 @@ def test_cache_control(self): blob = bucket.get_blob(filename) self.assertEqual(blob.cache_control, cache_control) - def test_save_gzip(self): + def test_storage_save_gzipped(self): """ - Test saving a file with gzip enabled. + Test saving a gzipped file """ - self.storage.gzip = True + name = 'test_storage_save.gz' + content = ContentFile("I am gzip'd") + self.storage.save(name, content) + obj = self.storage._bucket.get_blob() + obj.upload_from_file.assert_called_with( + mock.ANY, + rewind=True, + size=11, + predefined_acl=None, + content_type=None + ) - data = "I should be gzip'd" - filename = 'test_storage_save.css' - content = ContentFile(data) - self.storage.save(filename, content) + def test_storage_save_gzipped_non_seekable(self): + """ + Test saving a gzipped file + """ + name = 'test_storage_save.gz' + content = NonSeekableContentFile("I am gzip'd") + self.storage.save(name, content) + obj = self.storage._bucket.get_blob() + obj.upload_from_file.assert_called_with( + mock.ANY, + rewind=True, + size=11, + predefined_acl=None, + content_type=None + ) + def test_storage_save_gzip(self): + """ + Test saving a file with gzip enabled. + """ + self.storage.gzip = True + name = 'test_storage_save.css' + content = ContentFile("I should be gzip'd") + self.storage.save(name, content) self.storage._client.bucket.assert_called_with(self.bucket_name) obj = self.storage._bucket.get_blob() - obj.upload_from_file.assert_called() + obj.upload_from_file.assert_called_with( + mock.ANY, + rewind=True, + size=None, + predefined_acl=None, + content_type='text/css', + content_encoding='gzip' + ) args, kwargs = obj.upload_from_file.call_args content = args[0] zfile = gzip.GzipFile(mode='rb', fileobj=content) self.assertEqual(zfile.read(), b"I should be gzip'd") - def test__save_gzip_twice(self): + def test_storage_save_gzip_twice(self): """ Test saving the same file content twice with gzip enabled. """ @@ -438,7 +475,14 @@ def test__save_gzip_twice(self): # Then self.storage._client.bucket.assert_called_with(self.bucket_name) obj = self.storage._bucket.get_blob() - obj.upload_from_file.assert_called() + obj.upload_from_file.assert_called_with( + mock.ANY, + rewind=True, + size=None, + predefined_acl=None, + content_type='text/css', + content_encoding='gzip' + ) args, kwargs = obj.upload_from_file.call_args content = args[0] zfile = gzip.GzipFile(mode='rb', fileobj=content)