Skip to content

Commit

Permalink
Fixes: #411 -- Add Cache Control setting to Google Cloud Storage (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneier authored Jun 1, 2018
1 parent 9f07eab commit 1de11e6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/backends/gcloud.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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
2 changes: 2 additions & 0 deletions storages/backends/gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class GoogleCloudStorage(Storage):
auto_create_acl = setting('GS_AUTO_CREATE_ACL', 'projectPrivate')
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 @@ -163,6 +164,7 @@ def _save(self, name, content):
content.name = cleaned_name
encoded_name = self._encode_name(name)
file = GoogleCloudFile(encoded_name, 'rw', self)
file.blob.cache_control = self.cache_control
file.blob.upload_from_file(content, size=content.size,
content_type=file.mime_type)
return cleaned_name
Expand Down
13 changes: 13 additions & 0 deletions tests/test_gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,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 1de11e6

Please sign in to comment.