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

查看:117
本文介绍了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.3KB).

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

Deps:

"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.

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

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

推荐答案

我最终在关于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作为部门.我最后的部门是:

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天全站免登陆