Skip to content

Commit

Permalink
[gcloud] Add configurable blob parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneier committed Sep 20, 2021
1 parent 9e5a3dc commit 1a8812f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
26 changes: 24 additions & 2 deletions docs/backends/gcloud.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,31 @@ must fit in memory. Recommended if you are going to be uploading large files.

This must be a multiple of 256K (1024 * 256)

``GS_CACHE_CONTROL`` (optional: default is ``None``)
``GS_OBJECT_PARAMETERS`` (optional: default is ``{}``)

Sets Cache-Control HTTP header for the file, more about HTTP caching can be found `here <https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching#cache-control>`_
Dictionary of key-value pairs mapping from blob property name to value.

Use this to set parameters on all objects. To set these on a per-object
basis, subclass the backend and override ``GoogleCloudStorage.get_object_parameters``.

The valid property names are ::

acl
cache_control
content_disposition
content_encoding
content_language
content_type
metadata
storage_class

If not set, the ``content_type`` property will be guessed.

If set, ``acl`` overrides :ref:`GS_DEFAULT_ACL <gs-default-acl>`.

.. warning::

Do not set ``name``. This is set automatically based on the filename.

``GS_CUSTOM_ENDPOINT`` (optional: default is ``None``)

Expand Down
35 changes: 31 additions & 4 deletions storages/backends/gcloud.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mimetypes
import warnings
from datetime import timedelta
from tempfile import SpooledTemporaryFile

Expand All @@ -23,6 +24,9 @@
"See https://github.com/GoogleCloudPlatform/gcloud-python")


CONTENT_TYPE = 'content_type'


class GoogleCloudFile(File):
def __init__(self, name, mode, storage):
self.name = name
Expand Down Expand Up @@ -106,6 +110,7 @@ def get_default_settings(self):
"expiration": setting('GS_EXPIRATION', timedelta(seconds=86400)),
"file_overwrite": setting('GS_FILE_OVERWRITE', True),
"cache_control": setting('GS_CACHE_CONTROL'),
"object_parameters": setting('GS_OBJECT_PARAMETERS', {}),
# The max amount of memory a returned file can take up before being
# rolled over into a temporary file on disk. Default is 0: Do not
# roll over.
Expand Down Expand Up @@ -154,12 +159,34 @@ def _save(self, name, content):

content.name = cleaned_name
file = GoogleCloudFile(name, 'rw', self)
file.blob.cache_control = self.cache_control
file.blob.upload_from_file(
content, rewind=True, size=content.size,
content_type=file.mime_type, predefined_acl=self.default_acl)

upload_params = {}
blob_params = self.get_object_parameters(name, content)
if 'cache_control' not in blob_params and self.cache_control:
warnings.warn(
'The GS_CACHE_CONTROL setting is deprecated. Use GS_OBJECT_PARAMETERS to set any '
'writable blob property or override GoogleCloudStorage.get_object_parameters to '
'vary the parameters per object.', DeprecationWarning
)
blob_params['cache_control'] = self.cache_control

upload_params['predefined_acl'] = blob_params.pop('acl', self.default_acl)
if CONTENT_TYPE not in blob_params:
upload_params[CONTENT_TYPE] = file.mime_type

for prop, val in blob_params.items():
setattr(file.blob, prop, val)

file.blob.upload_from_file(content, rewind=True, size=content.size, **upload_params)
return cleaned_name

def get_object_parameters(self, name, content):
"""Override this to return a dictionary of overwritable blob-property to value.
Returns GS_OBJECT_PARAMETRS by default. See the docs for all possible options.
"""
return self.object_parameters.copy()

def delete(self, name):
name = self._normalize_name(clean_name(name))
try:
Expand Down

0 comments on commit 1a8812f

Please sign in to comment.