django collectstatic与django-storages重新复制所有文件

时间:2017-07-09 20:51:46

标签: django amazon-s3 django-staticfiles django-storage

我正在使用django.contrib.staticfiles和django-storage来将我的静态文件部署到Amazon S3。我使用的django版本是1.10.4,django-storages版本是1.5.2。

现在,当我运行collectstatic时,即使本地文件没有变化,它也会将所有文件从本地系统重新复制到S3。查看collectstatic管理命令代码,我可以看到:

方法delete_file:

            # The full path of the target file
            if self.local:
                full_path = self.storage.path(prefixed_path)
            else:
                full_path = None
            # Skip the file if the source file is younger
            # Avoid sub-second precision (see #14665, #19540)
            if (target_last_modified.replace(microsecond=0) >= source_last_modified.replace(microsecond=0) and
                    full_path and not (self.symlink ^ os.path.islink(full_path))):
                if prefixed_path not in self.unmodified_files:
                    self.unmodified_files.append(prefixed_path)
                self.log("Skipping '%s' (not modified)" % path)
                return False

在调试时,我看到即使target_last_modified> = source_last_modified但full_path为None,这就是检查失败的原因,它最终会在远程上删除文件。我不确定我做错了什么,或者我错过了一些设置,因为它正在重新上传文件。有趣的是,如果我删除上面代码中的额外检查,只需检查:

if (target_last_modified.replace(microsecond=0) >= source_last_modified.replace(microsecond=0)):

它工作正常。

我在SO上看到过类似的问题,但主要是由于S3与本地系统的时区不同。在我的例子中,我的本地时区和S3桶区都是相同的。在任何情况下,上述黑客都表明问题不是由于时区差异造成的。

1 个答案:

答案 0 :(得分:1)

我们的解决方案是使用Collectfast

https://github.com/jazzband/collectfast

它在上传之前缓存并比较文件的md5校验和。我们很想知道这个问题的根本原因,但这解决了这个问题的缓慢。

相关问题