11

django_compressorを使用してgzipファイルをAmazonS3に送信するにはどうすればよいですか?

いくつかの方法で試しましたが、うまくいきませんでした。これが私の最後のsettings.py構成です:

COMPRESS_ENABLED = True
COMPRESS_OFFLINE = True

COMPRESS_ROOT = STATIC_ROOT
COMPRESS_URL = "http://xxx.cloudfront.net/"
STATIC_URL = COMPRESS_URL
COMPRESS_OUTPUT_DIR = 'CACHE'

#COMPRESS_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
COMPRESS_STORAGE = 'core.storage.CachedS3BotoStorage'

STATICFILES_STORAGE = 'compressor.storage.GzipCompressorFileStorage'
COMPRESS_YUI_BINARY = 'java -jar contrib/yuicompressor-2.4.7/build/yuicompressor-2.4.7.jar'
COMPRESS_YUI_JS_ARGUMENTS = ''
COMPRESS_CSS_FILTERS = ['compressor.filters.yui.YUICSSFilter']
COMPRESS_JS_FILTERS = ['compressor.filters.yui.YUIJSFilter']
COMPRESS_CSS_HASHING_METHOD = 'hash'

と私のstorage.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
4

3 に答える 3

8

django-storages S3 ストレージ バックエンドは gzip をサポートします。settings.py に追加します。

AWS_IS_GZIPPED = True
于 2013-10-04T11:46:20.803 に答える
5

何日もの懸命な作業と研究の後、私はついにこれを行うことができました.

基本的に、いくつかのことを行う必要があります。

  1. 使用するAWS_IS_GZIPPED = True
  2. S3 が米国外にある場合。変数を S3 URL にS3Connectionオーバーライドするカスタム クラスを作成する必要があります。DefaultHosts3-eu-west-1.amazonaws.com
  3. ドット付きバケット名を使用している場合、例subdomain.domain.tld. 設定する必要がありますAWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'
  4. あなたはあなたに設定する必要がnon_gzipped_file_content = content.fileありますCachedS3BotoStorage

これはCachedS3BotoStorageあなたが必要とするクラスです:

class CachedS3BotoStorage(S3BotoStorage):
    """
    S3 storage backend that saves the files locally, too.

    """
    connection_class = EUConnection
    location = settings.STATICFILES_LOCATION

    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):
        non_gzipped_file_content = content.file
        name = super(CachedS3BotoStorage, self).save(name, content)
        content.file = non_gzipped_file_content
        self.local_storage._save(name, content)
        return name
于 2015-05-06T17:32:52.613 に答える
2

Update 2019:公式ドキュメントに記載されています

#mysite.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):
        self.local_storage._save(name, content)
        super(CachedS3BotoStorage, self).save(name, self.local_storage._open(name))
        return name

そしてあなたの設定:

#settings.py
INSTALLED_APPS += ['compressor']

AWS_IS_GZIPPED = True

STATIC_ROOT = '/path/to/staticfiles' #if not set, set this to an empty folder
COMPRESS_ROOT = STATIC_ROOT
STATICFILES_STORAGE = 'mysite.storage.CachedS3BotoStorage'
COMPRESS_STORAGE = STATICFILES_STORAGE
STATIC_URL = 'https://compressor-test.s3.amazonaws.com/'
COMPRESS_URL = STATIC_URL

単純に実行できるよりも:

python manage.py collectstatic
于 2019-02-09T11:33:22.703 に答える