<p>经过几天的努力和研究,我终于能够做到这一点。在</p>
<p>基本上你需要做一些事情:</p>
<ol>
<li>使用<code>AWS_IS_GZIPPED = True</code></li>
<li>如果你的S3在我们外面。您需要创建一个自定义的<code>S3Connection</code>类,在这个类中,您可以将<code>DefaultHost</code>变量覆盖到s3url中。示例<code>s3-eu-west-1.amazonaws.com</code></li>
<li>如果您使用的是虚线存储区名称,例如<code>subdomain.domain.tld</code>。您需要设置<code>AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'</code></li>
<li>您必须在<code>CachedS3BotoStorage</code>中设置<code>non_gzipped_file_content = content.file</code></li>
</ol>
<p>这是您需要的<code>CachedS3BotoStorage</code>类:</p>
<pre><code>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
</code></pre>