【问题标题】:UncompressableFileError: 'scripts/app.js' isn't accessible via COMPRESS_URL ('http://my-bucket.s3-us-west-2.amazonaws.com/') and can't be compressedUncompressableFileError: 'scripts/app.js' 不能通过 COMPRESS_URL ('http://my-bucket.s3-us-west-2.amazonaws.com/') 访问并且不能被压缩
【发布时间】:2016-04-13 13:36:23
【问题描述】:

我正在尝试将 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) 已经在这里收到了一个相对网址。

有人知道怎么解决吗?

提前致谢!

【问题讨论】:

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


    【解决方案1】:

    看起来像 get_basename(self, url) (https://github.com/django-compressor/django-compressor/blob/2.0/compressor/base.py#L72) 已经在这里收到了一个相对的 url。

    这就是问题所在。使用 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

    【讨论】:

    • 这并不能解决问题。我已经有static 模板标签,但不起作用。
    猜你喜欢
    • 2020-11-24
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多