Skip to content

Commit

Permalink
[gcloud] fix saving content as gzipped
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneier committed Dec 23, 2022
1 parent ca228c8 commit 32a248d
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions storages/backends/gcloud.py
@@ -1,3 +1,5 @@
import gzip
import io
import mimetypes
import warnings
from datetime import timedelta
Expand All @@ -11,11 +13,9 @@

from storages.base import BaseStorage
from storages.compress import CompressedFileMixin
from storages.compress import CompressStorageMixin
from storages.utils import check_location
from storages.utils import clean_name
from storages.utils import get_available_overwrite_name
from storages.utils import is_seekable
from storages.utils import safe_join
from storages.utils import setting
from storages.utils import to_bytes
Expand Down Expand Up @@ -102,7 +102,7 @@ def close(self):


@deconstructible
class GoogleCloudStorage(CompressStorageMixin, BaseStorage):
class GoogleCloudStorage(BaseStorage):
def __init__(self, **settings):
super().__init__(**settings)

Expand Down Expand Up @@ -174,6 +174,13 @@ def _open(self, name, mode='rb'):
raise FileNotFoundError('File does not exist: %s' % name)
return file_object

def _compress_content(self, content):
zbuf = io.BytesIO()
with gzip.GzipFile(mode='wb', fileobj=zbuf, mtime=0.0) as zfile:
zfile.write(to_bytes(content.read()))
zbuf.seek(0)
return zbuf

def _save(self, name, content):
cleaned_name = clean_name(name)
name = self._normalize_name(cleaned_name)
Expand All @@ -195,10 +202,9 @@ def _save(self, name, content):
for prop, val in blob_params.items():
setattr(file_object.blob, prop, val)

rewind = is_seekable(content)
file_object.blob.upload_from_file(
content,
rewind=rewind,
rewind=True,
retry=DEFAULT_RETRY,
size=getattr(content, 'size', None),
**upload_params
Expand Down Expand Up @@ -300,9 +306,9 @@ def get_created_time(self, name):

def url(self, name, parameters=None):
"""
Return public url or a signed url for the Blob.
This DOES NOT check for existance of Blob - that makes codes too slow
for many use cases.
Return public URL or a signed URL for the Blob.
The existnce of blobs are not verified for public URLs, it makes the code too slow.
"""
name = self._normalize_name(clean_name(name))
blob = self.bucket.blob(name)
Expand Down

0 comments on commit 32a248d

Please sign in to comment.