简体   繁体   English

Django/AWS - 调用 HeadObject 操作时发生错误(403):禁止

[英]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.

相关问题 Django 收集 static S3 调用 HeadObject 操作时发生错误(403):禁止访问 - Django collect static S3 An error occurred (403) when calling the HeadObject operation: Forbidden 为什么我会收到 botocore.exceptions.ClientError:调用 HeadObject 操作时发生错误 (403):禁止? - Why am I getting botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden? (DJANGO + S3) + ZAPPA 调用 HeadObject 操作时发生错误 (400): Bad Request - (DJANGO + S3) + ZAPPA An error occurred (400) when calling the HeadObject operation: Bad Request Collectstatic 失败 - botocore.exceptions.ClientError:调用 HeadObject 操作时发生错误 (404):未找到 - Collectstatic failing - botocore.exceptions.ClientError: An error occurred (404) when calling the HeadObject operation: Not Found AWS S3 和 Django 返回“调用 PutObject 操作时发生错误 (AccessDenied)” - AWS S3 and Django returns "An error occurred (AccessDenied) when calling the PutObject operation" 在 django 中调用回调 URL 时出现 Forbidden (403) 错误 - Forbidden (403) error when calling the callback URL in django [Django][AWS S3] botocore.exceptions.clienterror 调用 PutObject 操作时发生错误(访问被拒绝) - [Django][AWS S3] botocore.exceptions.clienterror an error occurred (accessdenied) when calling the PutObject operation Django 403禁止错误 - Django 403 Forbidden Error AWS Lambda,调用 PutObject 操作时发生错误 (InvalidToken):提供的令牌格式错误或无效 - AWS Lambda, An error occurred (InvalidToken) when calling the PutObject operation: The provided token is malformed or otherwise invalid django中的csrf(forbidden 403)错误 - csrf(forbidden 403) error in django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM