【问题标题】:collectstatic incorrectly creates multiple CSS files in S3collectstatic 在 S3 中错误地创建了多个 CSS 文件
【发布时间】:2019-02-17 23:01:35
【问题描述】:

我已经将文件上传到 S3,我的 Wagtail/django 应用程序(静态和上传)工作正常。现在我正在尝试使用ManifestStaticFilesStorage 来启用缓存清除。应用程序正在正确生成 url,并且正在将带有哈希的文件复制到 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

部门:

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

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

编辑:

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

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

编辑 2:

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

【问题讨论】:

  • 您是否尝试在 prod 上重新启动 Django?
  • 我做过...很多次:p

标签: django amazon-s3 boto3 wagtail django-storage


【解决方案1】:

我最终在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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 2018-08-02
    • 2015-07-29
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    相关资源
    最近更新 更多