Django-storages: Google Cloud Storage: Cache Control

Created on 23 Oct 2017  路  9Comments  路  Source: jschneier/django-storages

It'd be nice to be able set the cache control header at the time of object upload. The docs for this can be found here: https://googlecloudplatform.github.io/google-cloud-python/latest/storage/blobs.html#google.cloud.storage.blob.Blob.cache_control

Most helpful comment

In case anybody else ends up here, to solve this issue I made a custom storage backend with updated _save method like this:

class GoogleCloudStorage(OriginalStorage):

    def _save(self, name, content):
        cleaned_name = clean_name(name)
        name = self._normalize_name(cleaned_name)

        content.name = cleaned_name
        encoded_name = self._encode_name(name)
        file = GoogleCloudFile(encoded_name, 'rw', self)
        # Modification start
        file.blob.cache_control = 'public, max-age=31622400'
        # Modification end
        file.blob.upload_from_file(content, size=content.size,
                                   content_type=file.mime_type)
        return cleaned_name

@ezarowny, thanks for sharing api for this.

All 9 comments

Hello, actually this is very important to use google cloud as storage backend. Because google-cdn requires cache-control headers to cache google-cloud-storage files.

In case anybody else ends up here, to solve this issue I made a custom storage backend with updated _save method like this:

class GoogleCloudStorage(OriginalStorage):

    def _save(self, name, content):
        cleaned_name = clean_name(name)
        name = self._normalize_name(cleaned_name)

        content.name = cleaned_name
        encoded_name = self._encode_name(name)
        file = GoogleCloudFile(encoded_name, 'rw', self)
        # Modification start
        file.blob.cache_control = 'public, max-age=31622400'
        # Modification end
        file.blob.upload_from_file(content, size=content.size,
                                   content_type=file.mime_type)
        return cleaned_name

@ezarowny, thanks for sharing api for this.

Thanks for sharing, @gfhuseyinyilmaz wouldn't it be nice to have it as a setting? The same way it is for S3?
http://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html?highlight=CacheControl

@zamai It would be nice to have it as a setting, but most of the time you want different headers for different type of files, this would be a really nice feature imho.

Is this project still active? This setting seems like a necessity as not everyone wants no-cache. There seems to be a PR for this with tests failing for some reason.

@nbau21 not as much as it deserves. There is a fair amount of duplicity and technical debt from when I forked it combined with me shifting to freelance has sort of crushed a lot of my time & will. Ideally people who added large backends would be happy to maintain them but I don't think they owe anything to anyone.

Obviously just added in this fix to master. Thanks for the ping.

Thanks for the response. It's the beauty of open source, anyone can chime in, fork, etc.

Also thanks for merging this to master. I didn't mean to sound entitled in my previous post- I guess I was a bit frustrated at the lack of support for Google Cloud Storage in Django, compared to AWS.

Original storage not defined

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sheepeatingtaz picture sheepeatingtaz  路  7Comments

vdboor picture vdboor  路  4Comments

AGASS007 picture AGASS007  路  7Comments

stebunovd picture stebunovd  路  3Comments

ryanovas picture ryanovas  路  6Comments