如何使用Amazon S3来配置django-compression和django-staticfiles? [英] How to configure django-compressor and django-staticfiles with Amazon's S3?
问题描述
我正在尝试设置 django-compression 和 django-staticfiles ,以便压缩的CSS / Javascript和图像从Amazon S3获得。
我设法使用S3作为后端设置staticfiles,所以它是 collectstatic
命令将文件发送到S3而不是 STATIC_ROOT
。
但是,当尝试添加 django-compression
。关于设置远程存储的文档,我创建了一个存储后端的子类, boto ,所以我复制了示例到 storage.py
。一旦我开始使用这个缓存的后端,这些文件被复制到static_media而不是S3。第一页加载后,CACHE文件夹出现在S3和static_media文件夹中。
设置 STATICFILES_STORAGE
和 COMPRESS_STORAGE
返回到boto的正常S3类( storages.backends.s3boto.S3BotoStorage
)导致静态资产收集到S3 bucket和no static_media文件夹。但是尝试重新加载页面会抛出错误:
渲染时捕获NotImplementedError:此后端不支持绝对路径。
突出显示 {%compress css%}
as标签和 compression / base.py
作为原点。
我的 settings.py
:
DEFAULT_FILE_STORAGE ='storages.backends.s3boto.S3BotoStorage '
AWS_ACCESS_KEY_ID ='key'
AWS_SECRET_ACCESS_KEY ='secret'
AWS_STORAGE_BUCKET_NAME ='我的桶'
S3_URL ='http://my-bucket.s3.amazonaws.com /'
MEDIA_ROOT ='client_media'
MEDIA_URL ='/ media /'
STATIC_ROOT ='static_media'
STATIC_URL = S3_URL
ADMIN_MEDIA_PREFIX = S3_URL + 'admin /'
STATICFILES_DIRS =(
join(DIRNAME,'static'),
)
STATICFILES_FINDERS =(
'django.contrib.staticfiles.finders.FileSystemFinder ',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compress.finders.CompressorFinder',
)
COM PRESS_ENABLED = True
COMPRESS_URL = S3_URL
COMPRESS_ROOT = STATIC_ROOT
COMPRESS_STORAGE ='storage.CachedS3BotoStorage'
STATICFILES_STORAGE = COMPRESS_STORAGE
那我哪里错了?在使用 CachedS3BotoStorage
自定义存储空间时,是否配置错误?
p>您的设置看起来正确。您应该保持 STATICFILES_STORAGE
和 COMPRESS_STORAGE
设置为 storage.CachedS3BotoStorage
而不是切换回 storages.backends.s3boto.S3BotoStorage
。
根据这个 django压缩器问题,问题是django-staticfiles在收集过程中保存的方式(使用 shutil.copy2
)。 django-staticfiles 的较新版本已更正此问题,其中可以使用,而不是Django 1.3附带的。
pip install django-staticfiles == dev
在您的 settings.py
中切换到更新版本:
STATICFILES_FINDERS =(
#django.contrib.staticfiles.finders.FileSystemFinder,
# django.contrib.staticfiles.finders.AppDirectoriesFinder,
staticfiles.finders.FileSystemFinder,
staticfiles.finders.AppDirectoriesFinder,
compress.finders.CompressorFinder,
)
INSTALLED_APPS =(
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
#'django.contrib.staticfiles',
'staticfiles',
#...
)
再次运行 python manage.py collectstatic
后,来自django-compression的CACHE目录和收集的staticfiles文件都应该出现在S3上。 p>
I'm trying to setup django-compressor and django-staticfiles so that the compressed CSS/Javascript and images are served from Amazon's S3.
I've managed to setup staticfiles using S3 as the backend so it's collectstatic
command sends the files to S3 instead of STATIC_ROOT
.
However when trying to add django-compressor
to the mix is where it all seems to fall apart for me. Following the documentation on setting up remote storages I've created a subclass of the storage backend, boto, so I copied the example to storage.py
. Once I start using this cached backend the files are copied into static_media and not S3. After the first page load the CACHE folder appears on S3 and in the static_media folder.
Setting STATICFILES_STORAGE
and COMPRESS_STORAGE
back to boto's normal S3 class (storages.backends.s3boto.S3BotoStorage
) results in the static assets being collected into the S3 bucket and no static_media folder. However trying to reload the page throws the error:
Caught NotImplementedError while rendering: This backend doesn't support absolute paths.
highlighting {% compress css %}
as the tag and compressor/base.py
as the origin.
The s3/staticfiles/compressor section of my settings.py
:
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_ACCESS_KEY_ID = 'key'
AWS_SECRET_ACCESS_KEY ='secret'
AWS_STORAGE_BUCKET_NAME = 'my-bucket'
S3_URL = 'http://my-bucket.s3.amazonaws.com/'
MEDIA_ROOT = 'client_media'
MEDIA_URL = '/media/'
STATIC_ROOT = 'static_media'
STATIC_URL = S3_URL
ADMIN_MEDIA_PREFIX = S3_URL + 'admin/'
STATICFILES_DIRS = (
join(DIRNAME, 'static'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
)
COMPRESS_ENABLED = True
COMPRESS_URL = S3_URL
COMPRESS_ROOT = STATIC_ROOT
COMPRESS_STORAGE = 'storage.CachedS3BotoStorage'
STATICFILES_STORAGE = COMPRESS_STORAGE
So where am I going wrong? Have I mis-configured something when using the CachedS3BotoStorage
custom storage maybe?
Your settings look correct. You should keep both STATICFILES_STORAGE
and COMPRESS_STORAGE
set to storage.CachedS3BotoStorage
though and not switch back to storages.backends.s3boto.S3BotoStorage
.
According to this django-compressor issue, the problem is with the way django-staticfiles saves during the collectstatic process (using shutil.copy2
). This issue has been corrected in the newer version of django-staticfiles, which can be used instead of the one that ships with Django 1.3.
pip install django-staticfiles==dev
And in your settings.py
, switch to the updated version:
STATICFILES_FINDERS = (
#"django.contrib.staticfiles.finders.FileSystemFinder",
#"django.contrib.staticfiles.finders.AppDirectoriesFinder",
"staticfiles.finders.FileSystemFinder",
"staticfiles.finders.AppDirectoriesFinder",
"compressor.finders.CompressorFinder",
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
#'django.contrib.staticfiles',
'staticfiles',
#...
)
After running python manage.py collectstatic
again, both the CACHE directory from django-compressor and the collected staticfiles files should show up on S3.
这篇关于如何使用Amazon S3来配置django-compression和django-staticfiles?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!