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

Div*_*ick 5 django amazon-s3 django-storage django-staticfiles

我使用 django.contrib.staticfiles 和 django-storages 将静态文件部署到 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
Run Code Online (Sandbox Code Playgroud)

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

if (target_last_modified.replace(microsecond=0) >= source_last_modified.replace(microsecond=0)):
Run Code Online (Sandbox Code Playgroud)

效果很好。

我在 SO 上看到过类似的问题,但它们主要是由于 S3 与本地系统的时区不同所致。就我而言,我的本地时区和 S3 存储桶区域是相同的。无论如何,上述黑客表明问题不是由于时区差异造成的。

Fli*_*rPA 3

我们的解决方案是使用Collectfast

https://github.com/jazzband/collectfast

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

  • 根本原因正如我所描述的那样。默认的collectstatic不会查找远程存储上是否存在该文件。显然它无法/不使用 S3Boto3Storage 来查找远程文件。无论如何,现在 Collectfast 对我来说很有效。 (2认同)