我已经将文件上传到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更新为最新版本-行为与上面相同。
发布于 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"
https://stackoverflow.com/questions/52309821
复制相似问题