如何使用 Amazon S3 配置 django-compressor 和 django-staticfiles?

发布于 2024-12-08 23:27:00 字数 2088 浏览 1 评论 0原文

我正在尝试设置 django-compressordjango-staticfiles 以便从 Amazon S3 提供压缩的 CSS/Javascript 和图像。

我已成功使用 S3 作为后端来设置静态文件,因此 collectstatic 命令将文件发送到 S3 而不是 STATIC_ROOT

然而,当尝试将 django-compressor 添加到混合中时,这一切对我来说似乎都崩溃了。按照有关设置远程存储的文档,我创建了存储后端的子类, boto,所以我复制了示例storage.py。一旦我开始使用这个缓存后端,文件就会被复制到 static_media 而不是 S3 中。加载第一页后,CACHE 文件夹将出现在 S3 上的 static_media 文件夹中。

STATICFILES_STORAGECOMPRESS_STORAGE 设置回 boto 的普通 S3 类 (storages.backends.s3boto.S3BotoStorage) 会导致静态资源被收集到 S3 中存储桶并且没有 static_media 文件夹。但是,尝试重新加载页面会引发错误:

Caught NotImplementedError while rendering: This backend doesn't support absolute paths.

突出显示 {% compress css %} 作为标签,将 compressor/base.py 作为来源。

我的 settings.py 的 s3/staticfiles/compressor 部分:

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

那么我哪里出错了?我在使用 CachedS3BotoStorage 自定义存储时是否配置错误?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟若柳尘 2024-12-15 23:27:00

您的设置看起来正确。您应该将 STATICFILES_STORAGECOMPRESS_STORAGE 设置为 storage.CachedS3BotoStorage,而不是切换回 storages.backends.s3boto.S3BotoStorage< /代码>。

根据 this django-compressor 问题,问题在于 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",
    "compressor.finders.CompressorFinder",
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    #'django.contrib.staticfiles',
    'staticfiles',
    #...
)

再次运行 python manage.pycollectstatic 后,django-compressor 中的 CACHE 目录和收集的 staticfiles 文件都应该出现在S3上。

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.

无名指的心愿 2024-12-15 23:27:00

使用 django_compressor==1.2 对我有用。我不知道为什么你需要安装 django-staticfiles 但是除了 1.2 之外的所有版本的 django_compressor 都有这个问题。

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.

茶色山野 2024-12-15 23:27:00

经过几天的努力和研究,我终于能够做到这一点,我决定写一个 详细指南,包括如何使用 gzip 压缩它们。

基本上,您需要做一些事情:

  1. 请使用 AWS_IS_GZIPPED = True
  2. 如果您的 S3 位于美国境外, 。您需要创建一个自定义 S3Connection 类,在其中将 DefaultHost 变量覆盖为您的 S3 url。示例 s3-eu-west-1.amazonaws.com
  3. 如果您使用的是点分存储桶名称,例如 subdomain.domain.tld。您需要设置 AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'
  4. 您必须在 CachedS3BotoStorage 中设置 non_gzipped_file_content = content.file

这是您需要的 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

请注意,EUConnection 是一个自定义类我将 DefaultHost 设置为我的 S3 位置。检查更长和更详细的指南以获取完整的自定义存储和设置。py

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.

Basically you need to do a few things:

  1. Use AWS_IS_GZIPPED = True
  2. If your S3 is outside of US. You need to create a custom S3Connection class where you override the DefaultHost variable to your S3 url. Example s3-eu-west-1.amazonaws.com
  3. If you're using a dotted bucket name, example subdomain.domain.tld. You need to set AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'
  4. You have to set non_gzipped_file_content = content.file in your CachedS3BotoStorage

This is the CachedS3BotoStorage class you need:

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

浴红衣 2024-12-15 23:27:00

尝试这篇文章,用一些行完成上述解决方案,以解决在 Amazon S3 中创建许多(多个)manifest_%.json 的问题。
https://stackoverflow.com/a/31545361/1359475

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://stackoverflow.com/a/31545361/1359475

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文