MeWrite Docs

AccessDenied (S3)

AWS S3へのアクセス権限がない場合に発生するエラー

概要

AccessDenied は、AWS S3バケットやオブジェクトへのアクセス権限がない場合に発生するエラーです。IAMポリシー、バケットポリシー、ACLの設定不備が主な原因です。

エラーメッセージ

An error occurred (AccessDenied) when calling the GetObject operation: Access Denied
An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
1
2
3
4
5
6
<Error>
    <Code>AccessDenied</Code>
    <Message>Access Denied</Message>
    <RequestId>XXXXXXXX</RequestId>
    <HostId>XXXXXXXX</HostId>
</Error>

原因

  1. IAMポリシー不足: ユーザー/ロールにS3アクセス権限がない
  2. バケットポリシー: バケットポリシーでアクセスが拒否されている
  3. ブロックパブリックアクセス: パブリックアクセスがブロックされている
  4. オブジェクト所有者: 別アカウントがアップロードしたオブジェクト
  5. 暗号化キー: KMS暗号化されたオブジェクトへのキーアクセス権限がない
  6. VPCエンドポイント: VPCエンドポイントポリシーで制限されている

解決策

1. IAMポリシーを確認・追加

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:PutObject",
                "s3:DeleteObject",
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::my-bucket",
                "arn:aws:s3:::my-bucket/*"
            ]
        }
    ]
}

AWS CLIで確認:

1
2
3
4
5
6
7
8
9
# 現在のユーザー/ロールを確認
aws sts get-caller-identity

# アタッチされたポリシーを確認
aws iam list-attached-user-policies --user-name myuser
aws iam list-attached-role-policies --role-name myrole

# インラインポリシーを確認
aws iam list-user-policies --user-name myuser

2. バケットポリシーを確認

1
2
# バケットポリシーを取得
aws s3api get-bucket-policy --bucket my-bucket

バケットポリシーの例(特定IAMユーザーに許可):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::123456789012:user/myuser"
            },
            "Action": [
                "s3:GetObject",
                "s3:PutObject"
            ],
            "Resource": "arn:aws:s3:::my-bucket/*"
        }
    ]
}

3. パブリックアクセスブロック設定を確認

1
2
# パブリックアクセスブロック設定を確認
aws s3api get-public-access-block --bucket my-bucket

パブリックアクセスが必要な場合:

1
2
# パブリックアクセスブロックを無効化(注意して実行)
aws s3api delete-public-access-block --bucket my-bucket

4. オブジェクトACLを確認

1
2
3
4
5
# オブジェクトのACLを確認
aws s3api get-object-acl --bucket my-bucket --key my-object

# オブジェクトの所有者を確認
aws s3api head-object --bucket my-bucket --key my-object

クロスアカウントでアップロードする場合:

1
2
# バケット所有者にフルコントロールを付与
aws s3 cp file.txt s3://my-bucket/ --acl bucket-owner-full-control

5. KMS暗号化キーの権限を追加

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "kms:Decrypt",
                "kms:GenerateDataKey"
            ],
            "Resource": "arn:aws:kms:ap-northeast-1:123456789012:key/xxxxx"
        }
    ]
}

6. バケット所有権設定を確認

1
2
# バケット所有権を確認
aws s3api get-bucket-ownership-controls --bucket my-bucket

ACLを無効化してバケット所有者に統一:

1
2
aws s3api put-bucket-ownership-controls --bucket my-bucket \
    --ownership-controls Rules=[{ObjectOwnership=BucketOwnerEnforced}]

7. CloudFront OAC/OAI経由でのアクセス

CloudFrontからS3にアクセスする場合のバケットポリシー:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "cloudfront.amazonaws.com"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::my-bucket/*",
            "Condition": {
                "StringEquals": {
                    "AWS:SourceArn": "arn:aws:cloudfront::123456789012:distribution/EDFDVBD6EXAMPLE"
                }
            }
        }
    ]
}

デバッグ方法

AWS CLI でテスト

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# オブジェクト一覧を取得
aws s3 ls s3://my-bucket/

# オブジェクトをダウンロード
aws s3 cp s3://my-bucket/test.txt ./

# オブジェクトをアップロード
aws s3 cp ./test.txt s3://my-bucket/

# デバッグモードで詳細を確認
aws s3 ls s3://my-bucket/ --debug

IAM Policy Simulator

AWSコンソールでIAM Policy Simulatorを使用して、ポリシーの評価結果を確認できます。

AWS の他のエラー

最終更新: 2025-12-08