Heroku& amp; Django-compressor Error Amazon S3“UncompressableFileError”

时间:2017-03-08 19:51:38

标签: python django heroku amazon-s3 django-compressor

所以我知道已经有一些关于这个主题的文章了,但我尝试了各种设置组合以使django-compressor工作,但没有成功。有什么想法吗?

settings.py

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    # other finders..
    'compressor.finders.CompressorFinder',
)
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
STATIC_URL = '/static/'

DEFAULT_FILE_STORAGE = "mysite.s3utils.MediaS3BotoStorage"

COMPRESS_ROOT = STATIC_ROOT
STATICFILES_STORAGE = 'mysite.storage.CachedS3BotoStorage'
COMPRESS_STORAGE = STATICFILES_STORAGE
COMPRESS_URL = STATIC_URL

s3utils.py

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


class StaticS3BotoStorage(S3BotoStorage):
    """
    Storage for static files.
    """

    def __init__(self, *args, **kwargs):
        kwargs['location'] = 'static'
        super().__init__(*args, **kwargs)


class MediaS3BotoStorage(S3BotoStorage):
    """
    Storage for uploaded media files.
    """

    def __init__(self, *args, **kwargs):
        kwargs['location'] = 'media'
        super().__init__(*args, **kwargs)


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

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

的index.html

{% load compress %}
{% compress css %}
<link href="{% static 'libs/twitter-bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
{% endcompress %}

错误:

  

compressor.exceptions.UncompressableFileError:   'https://mysite.s3.amazonaws.com:443/libs/twitter-bootstrap/css/bootstrap.min.css'   无法通过COMPRESS_URL访问   ('https://mysite.s3.amazonaws.com/static/')并且无法压缩

所以我从traceback错误中知道这个错误是由于django-compressor使用代码而引发的:

if not url.startswith(base_url):
    raise UncompressableFileError(
        "'%s' isn't accesible via COMPRESS_URL ('%s') and can't be"
        " compressed" % (url, base_url))

所以似乎由于某些原因COMPRESS_URL正在使用:443端口,并且缺少/static/后缀,否则它将起作用。

设定:

  • Django v1.10.6
  • Python v3.5
  • Django-compressor 2.1.1

1 个答案:

答案 0 :(得分:0)

docs开始,您应该设置:

override func viewDidLoad()
{
    super.viewDidLoad()

    // ...

    textField.delegate = self
}

func textFieldDidBeginEditing(_ textField: UITextField)
{
    print("Text clicked")
}

而不是像您的问题所示的部分网址。但考虑到您正在使用的设置,我甚至不确定这是您的问题。 (我的理解是你希望压缩器在S3上获取文件并在本地存储压缩结果)

查看django source code for Storage class,我会检查您的设置中COMPRESS_URL = "http://mysite.s3.amazonaws.com/" 的内容,这可能是端口MEDIA_URL的来源。

如果这没有帮助,我建议您发布错误的完整追溯,这可能有助于了解正在发生的事情。