[英]Django/AWS - An error occurred (403) when calling the HeadObject operation: Forbidden
I'm trying to set up my Django project to host static images on AWS S3 buckets, but when I try to upload an image via the Django admin panel I get the following error我正在尝试设置我的 Django 项目以在 AWS S3 存储桶上托管 static 图像,但是当我尝试通过 Django 管理面板上传图像时,我得到以下错误
These are my settings in Django这些是我在 Django 中的设置
AWS_ACCESS_KEY_ID = 'some_key'
AWS_SECRET_ACCESS_KEY = 'some_key_aswell'
AWS_STORAGE_BUCKET_NAME = 'bucket_name'
AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
AWS_S3_REGION_NAME = 'us-east-2'
Cors policy setup for the bucket存储桶的 Cors 策略设置
[
{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"GET",
"POST",
"PUT"
],
"AllowedOrigins": [
"*"
],
"ExposeHeaders": []
}
]
The IAM role used by the machine (or container) on which your Django app runs needs the following IAM policy added:运行 Django 应用程序的机器(或容器)使用的 IAM 角色需要添加以下 IAM 策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
],
"Resource": "arn:aws:s3:::<bucket>/<prefix>/*"
}
]
}
If your object is encypted, make sure that you also allow your IAM role to use the KMS key used to encrypt your object.如果您的 object 已加密,请确保您还允许 IAM 角色使用用于加密 object 的 KMS 密钥。
I went over and changed the Bucket policy to this:我过去并将 Bucket 策略更改为:
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "AllowPublicRead",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::YOUR-BUCKET-NAME/*"
}
]
}
And it worked.它奏效了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.