如何在heroku上设置django-compression,离线压缩到S3 [英] How to setup django-compressor on heroku, offline compression to S3
问题描述
这是我的设置:
我遵循在SO和不同博客中发现的每个QA建议,一切都可以在我的开发机器上正常工作, / p>
DEFAULT_FILE_STORAGE ='arena.utils.MediaRootS3BotoStorage'#媒体文件
#storage
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = os.environ.get('AWS_STORAGE_BUCKET_NAME')
AWS_PRELOAD_METADATA = True需要修复manage.py collectstatic命令以仅上传已更改的文件而不是所有文件
S3_URL ='https://%s.s3.amazonaws.com'%AWS_STORAGE_BUCKET_NAME
MEDIA_URL = S3_URL +'/ media /'
STATIC_URL = S3_URL +'/ static /'
ADMIN_MEDIA_PREFIX = STATIC_URL +'admin /'
COMPRESS_URL = STATIC_URL
COMPRESS_OFFLINE = True
COMPRESS_STORAGE ='utils.CachedS3BotoStor年龄'
STATICFILES_STORAGE = COMPRESS_STORAGE
当我运行collectstatic / compress一切都可以,我看到文件被收集到S3并放置在适当的位置。我看到清单文件。
加载带有压缩标签的任何页面,显示错误 OfflineGenerationError:启用了脱机压缩,但是键入d2a53169c44dec41ce3ee7da19b2b9d4从脱机清单中缺少。
运行 python manage.py compress
再次解决不了。当我检查清单文件时,确实找不到它的键。
这里有什么问题?
问题我已经检查了:
如何配置django -compressor和django-staticfiles与亚马逊的S3?
使用远程存储配置django-compression(django-storage - amazon s3)
我用一些行完成了上述解决方案,解决了在Amazon S3中创建许多(倍数)manifest _%。json的问题
in setting.py:
STATICFILES_STORAGE ='your_package.s3utils.CachedS3BotoStorag s3utils.py中的e'
从storages.backends.s3boto导入S3BotoStorage
从django.core.files.storage导入get_storage_class
class CachedS3BotoStorage(S3BotoStorage):
S3存储后端也可以在本地保存文件。
location ='static'
def __init __(self,* args,** kwargs):
super(CachedS3BotoStorage,self).__ init __ * args,** kwargs)
self.local_storage = get_storage_class(
compress.storage.CompressorFileStorage)()
def url(self,name):
修复问题图像admin Django S3图像
url = super(CachedS3BotoStorage,self).url(name)
如果name.endswith('/ ')而不是url.endswith('/'):
url + ='/'
return url
def save(self,name,content):
name = super(CachedS3BotoStorage,self).save(name,content)
self.local_storage._save(name,content)
返回名称
#这里是秘密的生成多个manifest.json并在Amazon S3中删除manifest.json
def get_available_name(self,name):
如果self.exists(name):
self.delete(name)
返回名称
I followed every QA suggestions found on SO and in different blogs, Everything works ok on my dev machine and nothing works on heroku.
here are my settings:
DEFAULT_FILE_STORAGE = 'arena.utils.MediaRootS3BotoStorage' # media files
# storage
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = os.environ.get('AWS_STORAGE_BUCKET_NAME')
AWS_PRELOAD_METADATA = True # necessary to fix manage.py collectstatic command to only upload changed files instead of all files
S3_URL = 'https://%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
MEDIA_URL = S3_URL + '/media/'
STATIC_URL = S3_URL + '/static/'
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
COMPRESS_URL = STATIC_URL
COMPRESS_OFFLINE = True
COMPRESS_STORAGE = 'utils.CachedS3BotoStorage'
STATICFILES_STORAGE = COMPRESS_STORAGE
When i run collectstatic/compress everything is ok, i see the files being collected to S3 and put in proper places. I see the manifest file.
Loading any page with compressor tag, show an error OfflineGenerationError: You have offline compression enabled but key "d2a53169c44dec41ce3ee7da19b2b9d4" is missing from offline manifest.
Running python manage.py compress
again solves nothing. when i check the manifest file, indeed the key it looks for doesn't exist.
What is going wrong here?
Question i already checked:
How to configure django-compressor and django-staticfiles with Amazon's S3?
Django Compressor with S3 URL Heroku
Configuring django-compressor with remote storage (django-storage - amazon s3)
I completed the above solution with some lines, to fix the problem that create many (multiples) manifest_%.json in Amazon S3
in setting.py:
STATICFILES_STORAGE = 'your_package.s3utils.CachedS3BotoStorage'
in s3utils.py:
from storages.backends.s3boto import S3BotoStorage
from django.core.files.storage import get_storage_class
class CachedS3BotoStorage(S3BotoStorage):
"""
S3 storage backend that saves the files locally, too.
"""
location = 'static'
def __init__(self, *args, **kwargs):
super(CachedS3BotoStorage, self).__init__(*args, **kwargs)
self.local_storage = get_storage_class(
"compressor.storage.CompressorFileStorage")()
def url(self, name):
"""
Fix problem images admin Django S3 images
"""
url = super(CachedS3BotoStorage, self).url(name)
if name.endswith('/') and not url.endswith('/'):
url += '/'
return url
def save(self, name, content):
name = super(CachedS3BotoStorage, self).save(name, content)
self.local_storage._save(name, content)
return name
# HERE is secret to dont generating multiple manifest.json and to delete manifest.json in Amazon S3
def get_available_name(self, name):
if self.exists(name):
self.delete(name)
return name
这篇关于如何在heroku上设置django-compression,离线压缩到S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!