Skip to content

Commit

Permalink
Fixes: jschneier#411 -- Add Cache Control setting to Google Cloud Sto…
Browse files Browse the repository at this point in the history
…rage (jschneier#505)
  • Loading branch information
jschneier authored and nitely committed Jul 30, 2018
1 parent 608e90a commit ceca661
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/backends/gcloud.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ By default files with the same name will overwrite each other. Set this to ``Fal
The maximum 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.

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

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>`_

Fields
------

Expand Down
3 changes: 2 additions & 1 deletion storages/backends/gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class GoogleCloudStorage(Storage):

file_name_charset = setting('GS_FILE_NAME_CHARSET', 'utf-8')
file_overwrite = setting('GS_FILE_OVERWRITE', True)
cache_control = setting('GS_CACHE_CONTROL', None)
# 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.
max_memory_size = setting('GS_MAX_MEMORY_SIZE', 0)
Expand Down Expand Up @@ -165,7 +166,7 @@ def _save(self, name, content):
content.name = cleaned_name
encoded_name = self._encode_name(name)
file = GoogleCloudFile(encoded_name, 'rw', self)
content.seek(0)
file.blob.cache_control = self.cache_control
file.blob.upload_from_file(content, size=content.size,
content_type=file.mime_type)
if self.default_acl:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,16 @@ def test_get_available_name(self):
def test_get_available_name_unicode(self):
filename = 'ủⓝï℅ⅆℇ.txt'
self.assertEqual(self.storage.get_available_name(filename), filename)

def test_cache_control(self):
data = 'This is some test content.'
filename = 'cache_control_file.txt'
content = ContentFile(data)
cache_control = 'public, max-age=604800'

self.storage.cache_control = cache_control
self.storage.save(filename, content)

bucket = self.storage.client.get_bucket(self.bucket_name)
blob = bucket.get_blob(filename)
self.assertEqual(blob.cache_control, cache_control)

0 comments on commit ceca661

Please # to comment.