UncompressableFileError:'scripts / app.js'无法通过COMPRESS_URL('http://my-bucket.s3-us-west-2.amazonaws.com/')访问,无法压缩

时间:2016-01-08 19:11:52

标签: django amazon-s3 django-staticfiles django-storage django-compressor

我正在尝试将django-compressor和django-storages-redux与django staticfiles和Amazon S3一起使用。这些是我的设置:

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'

对于静态文件,我使用自定义存储后端,如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

首先,我运行python manage.py collectstatic,效果很好,并将所有文件复制到S3。

现在我有一个简单的模板:

{% 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>

在浏览器中打开已连接的django视图会给出以下异常:

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

但该文件存在且可访问(通过http和https)。此处引发了例外情况:https://github.com/django-compressor/django-compressor/blob/2.0/compressor/base.py#L82

似乎get_basename(self, url)https://github.com/django-compressor/django-compressor/blob/2.0/compressor/base.py#L72)已在此处收到相对网址。

有人知道如何解决这个问题吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

  

好像是get_basename(self,url)   (https://github.com/django-compressor/django-compressor/blob/2.0/compressor/base.py#L72)   已经在这里收到一个相对网址。

这就是问题所在。使用static模板标记获取绝对URL应解决此问题。

{% 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>

您可能还想查看此答案https://stackoverflow.com/a/18400426