2013-03-02 45 views
9

如何使用django_compressor將gziped文件發送到Amazon S3?是否可以使用django_compressor/S3/gzip?

我嘗試了幾種方法,但它沒有奏效。這是我最後的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 
+0

我工作了同樣的問題,只是提出一個問題上django_compressor的GitHub:https://github.com/jezdez/django_compressor/issues/368 – kevinharvey 2013-03-05 15:42:10

+0

祝你好運@kcharvey!我期待着下一個版本:-D – Thomas 2013-03-06 04:01:28

+0

任何人排序?我試圖讓這個工作。 – Murph 2013-05-22 22:10:16

回答

7

django-storages S3存儲後端supports gzip。加入settings.py:

AWS_IS_GZIPPED = True 
+0

cloudfront用戶注意事項:這不足以讓Cloudfront提供gzip資產服務 – 2014-11-16 10:38:12

5

大量的艱苦工作和研究幾天後我終於能夠做到這一點,我決定寫一個detailed guide了。

基本上你需要做的幾件事情:

  1. 使用AWS_IS_GZIPPED = True
  2. 如果你的S3是美國以外。您需要創建在您覆蓋DefaultHost變量設置爲S3網址的自定義S3Connection類。例如s3-eu-west-1.amazonaws.com
  3. 如果您使用的是點鬥名,例如subdomain.domain.tld。您需要設置AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'
  4. 你必須設置non_gzipped_file_content = content.fileCachedS3BotoStorage

這是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 
+1

這是正確的答案。另見https://github.com/django-compressor/django-compressor/issues/404#issuecomment-18787650 – chhantyal 2015-07-01 19:11:16