collectstatic 在 S3 中错误地创建了多个 CSS 文件 [英] collectstatic incorrectly creates multiple CSS files in S3

查看:11
本文介绍了collectstatic 在 S3 中错误地创建了多个 CSS 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将文件上传到 S3,使用我的 Wagtail/django 应用程序(静态和上传)工作正常.现在我正在尝试使用 ManifestStaticFilesStorage 来启用缓存破坏.应用程序正确生成了 url,并且文件正在使用哈希复制到 S3.

I have uploading files to S3 working fine with my Wagtail/django application (both static and uploads). Now I'm trying to use ManifestStaticFilesStorage to enable cache busting. The urls are correctly being generated by the application and files are being copied with hashes to S3.

但是每次我运行 collectstatic 时,一些文件都会被复制两次到 S3 - 每个都有不同的哈希值.到目前为止,所有 CSS 文件都存在此问题.

But each time I run collectstatic some files get copied twice to S3 - each with a different hash. So far the issue is ocurring for all CSS files.

file.a.css 由应用程序加载并且是 staticfiles.json 中引用的文件 - 但它在 S3 中是 20.0B 文件(应该是 6.3知识库).

file.a.css is loaded by the application and is the file referenced in staticfiles.json - however it is a 20.0B file in S3 (should be 6.3KB).

file.b.css 在 S3 中有正确的内容 - 但是它没有出现在 collectstatic 生成的输出中.

file.b.css has the correct contents in S3 - however it does NOT appear in the output generated by collectstatic.

# custom_storages.py
from django.conf import settings
from django.contrib.staticfiles.storage import ManifestFilesMixin
from storages.backends.s3boto import S3BotoStorage


class CachedS3Storage(ManifestFilesMixin, S3BotoStorage):
    pass


class StaticStorage(CachedS3Storage):
    location = settings.STATICFILES_LOCATION


class MediaStorage(S3BotoStorage):
    location = settings.MEDIAFILES_LOCATION
    file_overwrite = False

部门:

"boto==2.47.0",
"boto3==1.4.4",
"django-storages==1.5.2"
"Django==2.0.8"

任何有关在何处查找此问题的指示将不胜感激!:)

Any pointers on where to look to track down this issue would be appreciated! :)

更仔细地查看复制到 S3 的所有文件,问题仅出现在 CSS 文件中.

Looking more carefully at all the files copied to S3 the issue is ONLY occurring for CSS files.

禁止将资产推送到 S3 并将它们写入本地文件系统按预期工作.

Disabling pushing assets to S3 and writing them to the local filesystem works as expected.

编辑 2:

将所有 deps 更新到最新版本 - 与上述行为相同.

Updated all the deps to the latest version - same behavior as above.

推荐答案

我最终在 django-storages 问题跟踪器,然后引导我到 一个非常关于SO的类似问题.

I eventually stumbled across this issue in django-storages issue tracker which then lead me to a very similar question on SO.

在这两个页面之间,我设法解决了这个问题.我做了以下事情让 django-storages + ManifestStaticFilesStorage + S3 一起工作:

Between these two pages I managed to resolve the issue. I did the following to get django-storages + ManifestStaticFilesStorage + S3 to work together:

# custom_storages.py
from django.conf import settings
from django.contrib.staticfiles.storage import ManifestFilesMixin
from storages.backends.s3boto3 import S3Boto3Storage  # note boto3!!


class PatchedS3StaticStorage(S3Boto3Storage):
    def _save(self, name, content):
        if hasattr(content, 'seek') and hasattr(content, 'seekable') and content.seekable():
            content.seek(0)
        return super()._save(name, content)


class CachedS3Storage(ManifestFilesMixin, PatchedS3StaticStorage):
    pass


class StaticStorage(CachedS3Storage):
    location = settings.STATICFILES_LOCATION


class MediaStorage(S3Boto3Storage):
    location = settings.MEDIAFILES_LOCATION
    file_overwrite = False

请注意,我必须使用 boto3 才能使其工作 django-storages 必须 >= 1.5 才能使用 boto3.我删除了 boto 作为 dep.我的最终决定是:

Note that I had to use boto3 to get this to work django-storages must be >= 1.5 to use boto3. I removed boto as a dep. My final deps were:

"boto3==1.4.4",
"django-storages==1.7.1"
"Django==2.0.8"

这篇关于collectstatic 在 S3 中错误地创建了多个 CSS 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆