我正在尝试在Django应用程序的AWS S3存储桶中设置媒体和静态文件存储,并且在尝试运行python manage.py collectstatic将静态文件放入存储桶时遇到以下错误:
botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
我正在运行boto3和Django存储。我已经浏览了这里的其他答案,并首先尝试了那里的想法。我的访问 key 等正确,因为我可以连接到SES OK。我在存储桶中配置了CORS。
我的存储桶策略是
{
"Id": "Policyxxx",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmtxxx",
"Action": "s3:*",
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::bucketname/*",
"arn:aws:s3:::bucketname"
],
"Principal": {
"AWS": [
"arn:aws:iam::xxxx:user/xxxx"
]
}
}
]
}
我的IAM用户具有如下的AmazonS3FullAccess:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": "*"
}
]
}
我还尝试过创建自己的策略,并将其附加到IAM用户,如下所示:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::bucketname",
"arn:aws:s3:::bucketname/*"
]
}
]
}
这些都不起作用,因此我显然缺少一些东西。
请您参考如下方法:
我有同样的错误。而且,与您不同的是,我使用具有正确IAM策略的合适用户。
在输出:
python manage.py collectstatic
在AccessDenied堆栈错误之前,我可以从
django-storage lib中读取以下消息:
UserWarning: The default behavior of S3Boto3Storage is insecure and will change in django-storages 2.0. By default files and new buckets are saved with an ACL of 'public-read' (globally publicly readable). Version 2.0 will default to using the bucket's ACL. To opt into the new behavior set AWS_DEFAULT_ACL = None, otherwise to silence this warning explicitly set AWS_DEFAULT_ACL. "The default behavior of S3Boto3Storage is insecure and will change "
这导致我尝试。
通过设置 :
AWS_DEFAULT_ACL = None
然后,将静态文件收集到存储桶中。




