I'm trying to setup django-compressor and django-staticfiles so that the compressed CSS/Javascript and images are served from Amazon's S3.
我正在尝试设置django压缩器和django-staticfiles,以便从Amazon的S3提供压缩的CSS/Javascript和图像。
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
.
我已经使用S3作为后台设置了staticfile,所以它的collectstatic命令将文件发送到S3而不是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.
然而,当我试图将django-压缩机添加到混合机上时,它似乎对我来说都是崩溃了。在建立远程存储的文档之后,我创建了一个存储后端(boto)的子类,所以我将这个示例复制到storage.py。一旦我开始使用这个缓存后端,文件就会被复制到static_media而不是S3中。加载第一页后,缓存文件夹出现在S3上,并出现在static_media文件夹中。
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:
将STATICFILES_STORAGE和COMPRESS_STORAGE设置回boto的正常S3类(storages.backends3boto.s3botostorage),结果是将静态资产收集到S3 bucket中,而不是static_media文件夹。但是尝试重新加载页面会抛出错误:
Caught NotImplementedError while rendering: This backend doesn't support absolute paths.
highlighting {% compress css %}
as the tag and compressor/base.py
as the origin.
突出显示{% compress css %}作为标签和压缩器/基。py是原点。
The s3/staticfiles/compressor section of my settings.py
:
我的settingss3 /staticfiles/压缩器部分。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?
那么我哪里做错了呢?在使用CachedS3BotoStorage自定义存储时,我是否配置错误?
4 个解决方案
#1
10
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
.
您的设置正确。应该将STATICFILES_STORAGE和COMPRESS_STORAGE设置为storage。不过要缓存3botostorage,不要切换回store .backend .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.
根据这个django压缩器问题,问题是django静态文件在collectstatic进程(使用shutil.copy2)中保存的方式。这个问题在更新版本的Django -staticfiles中得到了纠正,该文件可以使用,而不是与Django 1.3一起发布的文件。
pip install django-staticfiles==dev
And in your settings.py
, switch to the updated version:
和在你的设置。py,切换到更新版本:
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.
在运行python管理。py collectstatic,来自django压缩器的缓存目录和收集的staticfiles文件都应该显示在S3上。
#2
0
Using django_compressor==1.2
worked for me. I am not sure why you need to install django-staticfiles however all the versions of django_compressor
except 1.2 has that issue.
使用django_压缩机=1.2对我来说是可行的。我不确定为什么需要安装django-staticfiles,但是除了1.2之外的所有版本都有这个问题。
#3
0
After plenty of days of hard work and research I was finally able to do this and I decided to write a detailed guide about it, including how to also serve them zipped with gzip.
经过了很多天的艰苦工作和研究,我终于做到了这一点,我决定写一份详细的指南,包括如何用gzip压缩它们。
Basically you need to do a few things:
基本上你需要做一些事情:
- Use
AWS_IS_GZIPPED = True
- 使用AWS_IS_GZIPPED = True
- If your S3 is outside of US. You need to create a custom
S3Connection
class where you override theDefaultHost
variable to your S3 url. Examples3-eu-west-1.amazonaws.com
- 如果你的S3不在我们的范围内。您需要创建一个自定义S3Connection类,在其中将DefaultHost变量覆盖到S3 url。例子s3 -欧盟-西方- 1. - amazonaws.com
- If you're using a dotted bucket name, example
subdomain.domain.tld
. You need to setAWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'
- 如果您使用的是虚线的bucket名称,例如subdomain.domain.tld。您需要设置AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'
- You have to set
non_gzipped_file_content = content.file
in yourCachedS3BotoStorage
- 必须设置non_gzipped_file_content = content。文件在你CachedS3BotoStorage
This is the CachedS3BotoStorage
class you need:
这是您需要的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
Note that EUConnection
is a custom class where I set DefaultHost
to my S3 location. Check the much longer and detailed guide for complete custom storages and settings.py
注意,EUConnection是一个自定义类,我将DefaultHost设置为S3位置。请查看更长的详细指南,以获得完整的自定义存储和设置
#4
0
Try this post that complete the above solution with some lines, to fix the problem that create many (multiples) manifest_%.json in Amazon S3. https://*.com/a/31545361/1359475
尝试使用一些行来完成上面的解决方案,以修复创建多个(多个)manifest . %的问题。json在Amazon S3。https://*.com/a/31545361/1359475
#1
10
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
.
您的设置正确。应该将STATICFILES_STORAGE和COMPRESS_STORAGE设置为storage。不过要缓存3botostorage,不要切换回store .backend .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.
根据这个django压缩器问题,问题是django静态文件在collectstatic进程(使用shutil.copy2)中保存的方式。这个问题在更新版本的Django -staticfiles中得到了纠正,该文件可以使用,而不是与Django 1.3一起发布的文件。
pip install django-staticfiles==dev
And in your settings.py
, switch to the updated version:
和在你的设置。py,切换到更新版本:
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.
在运行python管理。py collectstatic,来自django压缩器的缓存目录和收集的staticfiles文件都应该显示在S3上。
#2
0
Using django_compressor==1.2
worked for me. I am not sure why you need to install django-staticfiles however all the versions of django_compressor
except 1.2 has that issue.
使用django_压缩机=1.2对我来说是可行的。我不确定为什么需要安装django-staticfiles,但是除了1.2之外的所有版本都有这个问题。
#3
0
After plenty of days of hard work and research I was finally able to do this and I decided to write a detailed guide about it, including how to also serve them zipped with gzip.
经过了很多天的艰苦工作和研究,我终于做到了这一点,我决定写一份详细的指南,包括如何用gzip压缩它们。
Basically you need to do a few things:
基本上你需要做一些事情:
- Use
AWS_IS_GZIPPED = True
- 使用AWS_IS_GZIPPED = True
- If your S3 is outside of US. You need to create a custom
S3Connection
class where you override theDefaultHost
variable to your S3 url. Examples3-eu-west-1.amazonaws.com
- 如果你的S3不在我们的范围内。您需要创建一个自定义S3Connection类,在其中将DefaultHost变量覆盖到S3 url。例子s3 -欧盟-西方- 1. - amazonaws.com
- If you're using a dotted bucket name, example
subdomain.domain.tld
. You need to setAWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'
- 如果您使用的是虚线的bucket名称,例如subdomain.domain.tld。您需要设置AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'
- You have to set
non_gzipped_file_content = content.file
in yourCachedS3BotoStorage
- 必须设置non_gzipped_file_content = content。文件在你CachedS3BotoStorage
This is the CachedS3BotoStorage
class you need:
这是您需要的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
Note that EUConnection
is a custom class where I set DefaultHost
to my S3 location. Check the much longer and detailed guide for complete custom storages and settings.py
注意,EUConnection是一个自定义类,我将DefaultHost设置为S3位置。请查看更长的详细指南,以获得完整的自定义存储和设置
#4
0
Try this post that complete the above solution with some lines, to fix the problem that create many (multiples) manifest_%.json in Amazon S3. https://*.com/a/31545361/1359475
尝试使用一些行来完成上面的解决方案,以修复创建多个(多个)manifest . %的问题。json在Amazon S3。https://*.com/a/31545361/1359475