我正在更新我的应用程序的依赖项。我对requirements.txt进行了以下更改:
- boto:升级到2.34.0
- django-pipeline:升级到1.4.2
- django-require:升级到1.0.6
- django-storages:最新版本 (1.1.8)
现在,当我尝试在 S3 存储桶中运行collectstatic 时,出现以下错误:
boto.exception.S3ResponseError: S3ResponseError: 400 Bad Request
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>InvalidArgument</Code><Message></Message><ArgumentName>x-amz-acl</ArgumentName>
<ArgumentValue>/tmp/tmpDyVin1</ArgumentValue><RequestId>xxx</RequestId>
<HostId>yyy</HostId></Error>
并不感到惊讶,因为显然/tmp/tmpDyVin1
是 not a valid value for x-amz-acl 。
问题源于指定我自己的类来处理结合了 Boto、Require 和 Pipeline 的静态文件:
settings.py
STATICFILES_STORAGE = 'myapp.storage.OptimizedS3BotoStorage'
存储.py
from pipeline.storage import PipelineMixin
from require.storage import OptimizedFilesMixin
from storages.backends.s3boto import S3BotoStorage
class ReleaseVersionCachedFilesMixin(CachedFilesMixin):
def hashed_name(self, name, content=None):
...
class OptimizedS3BotoStorage(PipelineMixin, OptimizedFilesMixin, ReleaseVersionCachedFilesMixin, S3BotoStorage):
pass
这与所有这些模块的旧版本完美且一致。深入研究新代码,我发现问题出在这三者的交互上:
- 何时
S3BotoStorage.__init__()
被调用,第一个参数,acl
,作为此 tmp 目录的值传入。这将覆盖之前的值public-read
并导致上述问题。 - 这个
__init__()
例程由CachedFilesMixin.__init__()
调用,它接收args = ('/tmp/tmpnNUVD9',)
. - 那个
__init__()
被PipelineMixin.__init__()
调用,它执行以下操作:
def __init__(self, location=None, *args, **kwargs):
if not settings.PIPELINE_ENABLED and location is None:
location = tempfile.mkdtemp()
super(PipelineMixin, self).__init__(location, *args, **kwargs)
所以,问题是Pipeline传入location
作为第一个参数,它向下传播并成为 acl
.
最佳答案
解决这个令人讨厌的问题的方法似乎只是将 PipelineMixin 移动到我的自定义存储类中参数列表的末尾,但这会破坏 r.js 优化器。解决方案是将此 PIPELINE_ENABLED
标志设置为 True
。
(文档显示 this flag defaults to not settings.DEBUG
,但这对于您的环境可能不是有意义的依赖项。直到 1.4 之前,PipelineMixin
中才以这种方式使用该标志,但 it's not documented in the directives for upgrading to 1.4 。)
关于django - 将collectstatic 与最新版本的Boto、管道和存储一起使用时,x-amz-acl 设置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27771184/