Search code examples
djangodjango-storagedjango-compressor

Django Compressor with S3 URL Heroku


I am currently using django compressor and django storages to run my static media off of s3. My files are as follows:

My storage as per the docs is:

from django.core.files.storage import get_storage_class
from storages.backends.s3boto import S3BotoStorage

class CachedS3BotoStorage(S3BotoStorage):
    """
    S3 storage backend that saves the files locally, too.
    """
    def __init__(self, *args, **kwargs):
        super(CachedS3BotoStorage, self).__init__(*args, **kwargs)
        self.local_storage = get_storage_class(
            "compressor.storage.CompressorFileStorage")()

    def save(self, name, content):
        name = super(CachedS3BotoStorage, self).save(name, content)
        self.local_storage._save(name, content)
        return name

my settings are:

# S3 Storage Section
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = DEFAULT_FILE_STORAGE
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']
AWS_STORAGE_BUCKET_NAME = os.environ['AWS_STORAGE_BUCKET_NAME']
# AWS_S3_SECURE_URLS = False #turns off https for static files (necessary)

# Used to make sure that only changed files are uploaded with collectstatic
AWS_PRELOAD_METADATA = True

# Django compressor settings
STATICFILES_FINDERS += (
                        'compressor.finders.CompressorFinder',
                        )
COMPRESS_ENABLED = True
COMPRESS_OFFLINE = True
COMPRESS_URL = STATIC_URL
COMPRESS_ROOT = STATIC_ROOT
COMPRESS_STORAGE = 'erp.storage.CachedS3BotoStorage'
STATICFILES_STORAGE = 'erp.storage.CachedS3BotoStorage'
AWS_LOCATION = 'static'
AWS_QUERYSTRING_EXPIRE = 7200

COMPRESS_JS_FILTERS = [
    'compressor.filters.template.TemplateFilter',
]

There is a lot of media to compress which is why I have opted to use offline compression and run the manage.py compress command rather than run collectstatic on dyno restarts as it is just to slow.

Django compressor provides me with a querystring which is great however it contains html which does not load. i.e.

<link rel="stylesheet" href="site-url/static/CACHE/css/da0c0fa8dd51.css?Signature=Signature&amp;Expires=Expires&amp;AWSAccessKeyId=key 

the two amp; items should not be there. I would rather have it secure but I've also tried AWS_S3_SECURE_URLS = False in the settings which does not seem to change things which makes me think there is something wrong.

I'm using django 1.4 so maybe there is something incompatible.


Solution

  • I can confirm that if using compress management command you'll need to redo the manifest the file. I did it manually but I'm sure there is a better way. Pretty small problem but I spent a bit of time on this and perhaps it will save someone else some time.