Django压缩器不支持亚马逊s3上的托管静态文件

时间:2015-06-12 10:43:39

标签: python django amazon-s3 django-compressor

错误:' https://socialboard-in.s3.amazonaws.com/static/notices/index1.css'无法通过COMPRESS_URL(' http://socialboard-in.s3.amazonaws.com/')访问,无法压缩

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

没有{%compress%}标记,一切正常。

1 个答案:

答案 0 :(得分:0)

我认为您需要为STATIC_ROOT定义临时本地存储,然后设置COMPRESS_ROOT = STATIC_ROOT,同时设置COMPRESS_STORAGE = STATICFILES_STORAGE

最后使用自定义缓存的s3存储,它还会在本地缓存文件,发出一个collectstatic(在上传到s3之前它会在本地推送到STATIC_ROOT),然后希望压缩器能为你工作。