在多服务器设置上使用django-compressor与django-compressor的正确方法

时间:2016-02-02 10:09:57

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

我试图弄清楚如何在多服务器设置中将django-storages-redux与django-compressor一起使用。所有静态文件都应使用boto存储在Amazon S3上。

到目前为止我得到了什么

使用的图书馆:

boto==2.38.0
boto3==1.2.3
botocore==1.3.18
Django==1.7.7
django-compressor==1.6
django-storages-redux==1.3

重要设置:

S3_ACCESS_KEY_ID = "-- REMOVED --"
S3_SECRET_ACCESS_KEY = "-- REMOVED --"

S3_DOMAINURL_FRT = "image.mydomain.com"
S3_BUCKETNAME_FRT = "image.mydomain.com"

AWS_ACCESS_KEY_ID = S3_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY = S3_SECRET_ACCESS_KEY
AWS_STORAGE_BUCKET_NAME = S3_BUCKETNAME_FRT
AWS_S3_CUSTOM_DOMAIN = S3_DOMAINURL_FRT
AWS_QUERYSTRING_AUTH = False
AWS_S3_FILE_OVERWRITE = False
AWS_S3_SECURE_URLS = False
AWS_S3_USE_SSL = False

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

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

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

COMPRESS_STORAGE = STATICFILES_STORAGE = 'project.storage.CachedS3BotoStorage'
STATIC_URL = COMPRESS_URL = 'http://%s/' % S3_DOMAINURL_FRT

COMPRESS_OFFLINE = True
COMPRESS_OUTPUT_DIR = 'cache'

" project.storage"代码:

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

os.environ['S3_USE_SIGV4'] = 'True'


class S3Storage(S3BotoStorage):
    @property
    def connection(self):
        if self._connection is None:
            self._connection = self.connection_class(
                self.access_key, self.secret_key,
                calling_format=self.calling_format, host='s3.eu-central-1.amazonaws.com')
        return self._connection


class CachedS3BotoStorage(S3Storage):
    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

    def get_available_name(self, name):
        """ Always overwrite existing file with the same name. """
        name = self._clean_name(name)
        return name

问题

因为我们有多个服务器,所以我们使用COMPRESS_OFFLINE并运行django-compress' 压缩命令。此命令在我们运行命令的服务器上本地生成文件。还在本地生成manifest.json。另外,因为我们使用django-storage,本地文件被复制到S3。显而易见。但是现在当尝试从另一个网络服务器运行django时,这些文件不存在,我们会遇到如下错误:

You have offline compression enabled but key "677803469038e2efb349aad5ddc60c39" is missing from offline manifest. You may need to run "python manage.py compress".

我们如何在一台服务器上压缩文件,让所有其他服务器通过链接到S3存储桶来使用这些文件?我想我们必须以某种方式将manifest.json文件传递给所有其他服务器的本地文件系统?

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我认为您希望在此应用程序中将COMPRESS_CSS_HASHING_METHOD设置为'content'The compressor settings point this out