7

I'm trying to use django-compressor and django-storages-redux together with django staticfiles and Amazon S3. These are my settings:

STATIC_URL = COMPRESS_URL = 'http://my-bucket.s3-us-west-2.amazonaws.com/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

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

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'site-static'),
)

COMPRESS_PRECOMPILERS = (
    ('text/scss', 'sass --scss --compass {infile} {outfile}'),
)

COMPRESS_CSS_FILTERS = [
    'compressor.filters.css_default.CssAbsoluteFilter',
    'compressor.filters.cssmin.CSSMinFilter',
]

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = COMPRESS_STORAGE = 'myapp.apps.mymodel.storage.CachedS3BotoStorage'
COMPRESS_OUTPUT_DIR = 'cache'
COMPRESS_ENABLED = False

AWS_S3_HOST = "s3-us-west-2.amazonaws.com"
AWS_ACCESS_KEY_ID = '---'
AWS_SECRET_ACCESS_KEY = '---'
AWS_STORAGE_BUCKET_NAME = 'my-bucket'
AWS_QUERYSTRING_AUTH = False
AWS_S3_CUSTOM_DOMAIN = 'my-bucket.s3-us-west-2.amazonaws.com'

For staticfiles I use a custom storage backend, as advised here http://django-compressor.readthedocs.org/en/latest/remote-storages/

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

First I ran python manage.py collectstatic which worked well and copied all the files to S3.

Now I have a simple template like that:

{% load compress static %}
<html><head>
{% compress js %}
  <script src="scripts/app.js"></script>
  <script src="scripts/controllers/main.js"></script>
{% endcompress %}
</head><body></body></html>

Opening that connected django view in a browser gives me the following exception:

'scripts/app.js' isn't accessible via COMPRESS_URL ('http://my-bucket.s3-us-west-2.amazonaws.com/') and can't be compressed

But the file is there and accessible (via http and https). The exception is raised here: https://github.com/django-compressor/django-compressor/blob/2.0/compressor/base.py#L82

Seems like get_basename(self, url) (https://github.com/django-compressor/django-compressor/blob/2.0/compressor/base.py#L72) already receives a relative url here.

Anybody knows how to fix that?

Thanks in advance!

Matthias Scholz
  • 1,015
  • 1
  • 13
  • 25

1 Answers1

0

Seems like get_basename(self, url) (https://github.com/django-compressor/django-compressor/blob/2.0/compressor/base.py#L72) already receives a relative url here.

That is the problem. Using the static template tag to get absolute URL should fix this.

{% load compress static %}
<html><head>
{% compress js %}
  <script src="{% static "scripts/app.js" %}"></script>
  <script src="{% static "scripts/controllers/main.js" %}"></script>
{% endcompress %}
</head><body></body></html>

You might also want to look at this answer https://stackoverflow.com/a/18400426

Community
  • 1
  • 1