【问题标题】:Proper way of using django-storages with django-compressor on a multi-server setup在多服务器设置上使用 django-storages 和 django-compressor 的正确方法
【发布时间】:2016-05-11 01:53:57
【问题描述】:

我试图弄清楚如何在多服务器设置上使用 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' compress 命令。该命令在我们运行该命令的服务器上本地生成文件。还会在本地生成 manifest.json。也因为我们使用 django-storages 本地文件被复制到 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 文件获取到所有其他服务器的本地文件系统?

非常感谢您提前提供的帮助!

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-11
      • 2012-09-12
      • 2013-07-18
      • 2011-09-27
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      相关资源
      最近更新 更多