首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >collectstatic在S3中错误地创建了多个CSS文件

collectstatic在S3中错误地创建了多个CSS文件
EN

Stack Overflow用户
提问于 2018-09-13 16:40:47
回答 1查看 664关注 0票数 1

我已经将文件上传到S3,在我的Wagtail/django应用程序(静态和上传)中都可以很好地工作。现在,我正在尝试使用ManifestStaticFilesStorage来启用缓存破坏。urls是由应用程序正确生成的,并且文件正在使用散列复制到S3。

但是每次我运行collectstatic时,一些文件会被复制到S3上两次--每次都有不同的哈希。到目前为止,这个问题出现在所有CSS文件中。

file.a.css由应用程序加载,是staticfiles.json中引用的文件-但在S3中它是一个20.0B的文件(应为6.3KB)。

file.b.css在S3中具有正确的内容-但是它不会出现在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"

如果有任何关于如何追踪这个问题的建议,我们将不胜感激!:)

编辑:

仔细查看所有复制到S3的文件,该问题仅发生在CSS文件中。

禁用将资产推送到S3并将其写入本地文件系统的功能与预期一样。

编辑2:

将所有dep更新为最新版本-行为与上面相同。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-14 08:26:38

我最终在django-storages问题跟踪器中偶然发现了这个问题,然后我找到了a very similar question on SO

在这两页之间,我设法解决了这个问题。为了让django-storages + ManifestStaticFilesStorage + S3协同工作,我做了以下工作:

# 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作为副警长。我最后的副手是:

"boto3==1.4.4",
"django-storages==1.7.1"
"Django==2.0.8"
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52309821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档