Django compressor is not working on hosted static files on amazon s3

1.8k views Asked by At

Error: 'https://socialboard-in.s3.amazonaws.com/static/notices/index1.css' isn't accessible via COMPRESS_URL ('http://socialboard-in.s3.amazonaws.com/') and can't be compressed

settings.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'notices',
    'colleges',
    'accounts',
    'follows',
    'pagination',
    'topic',
    'wysihtml5',
    'crispy_forms',
    'ratelimit',
    'compressor',
    'storages',
)
STATIC_PATH = os.path.join(PROJECT_PATH,'static')
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    STATIC_PATH,
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    # other finders..
    'compressor.finders.CompressorFinder',
)


TEMPLATE_DIRS = (
    # Put strings here, like "home/html/django_templates" Or "C:/www/django/templates".
    # Always use forward slashes, even on windows.
    # Don't forget to use absolute paths, not relative paths.
    TEMPLATE_PATH,
    os.path.join(BASE_DIR,'templates'),
)

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']


# All files uploaded to AWS S3 will have very long cache headers
# automatically.

AWS_HEADERS = {
 'Expires': 'Thu, 31 Dec 2099 20:00:00 GMT',
 'Cache-Control': 'max-age=94608000',
}

STATICFILES_STORAGE = 'custom_storages.CachedS3BotoStorage'

COMPRESS_ENABLED = True

COMPRESS_URL = "http://socialboard-in.s3.amazonaws.com/"

STATIC_URL = "http://socialboard-in.s3.amazonaws.com/"


COMPRESS_STORAGE = 'custom_storages.CachedS3BotoStorage'
AWS_S3_CUSTOM_DOMAIN = 'socialboard-in.amazonaws.com/'

custom_storages.py

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

base.html

{% compress css %} 
<link rel="stylesheet" type="text/css" href="{% static 'notices/index1.css' %}" />
<link href="{% static 'bootstrap-3.2.0-dist/css/bootstrap.min.css' %}" rel="stylesheet">
<!--<link href="{% static 'bootstrap3-wysihtml5.min.css' %}" rel="stylesheet">  -->
{% block css %}{% endblock %} 
  {% endcompress %}

Everything is working perfectly without {% compress %} tag.

1

There are 1 answers

2
fpghost On

I think you need to define a temporarily local storage for STATIC_ROOT, then set COMPRESS_ROOT = STATIC_ROOT, also COMPRESS_STORAGE = STATICFILES_STORAGE.

Finally with your custom cached s3 storage, it also caches the files locally, issue a collectstatic (it will push locally to STATIC_ROOT before uploading to s3), then hopefully compressor will work for you.