【问题标题】:Receive 403 Forbidden error everytime I try to upload a file to s3 bucket. (PHP, SDK)每次我尝试将文件上传到 s3 存储桶时都会收到 403 Forbidden 错误。 (PHP, SDK)
【发布时间】:2020-02-01 03:03:35
【问题描述】:

我之前已经遇到过两个错误,这是我每次尝试将文件上传到我的 s3 存储桶时的最新错误:

PUT https://cactustestphp.s3.us-west-1.amazonaws.com/videouploads/my-image.png` resulted in a `403 

Forbidden` response: AccessDeniedAccess Denied33D5F8 (truncated...) AccessDenied (client): 

Access Denied - AccessDeniedAccess

如果有人知道我为什么会遇到这个问题,请随时回复!我已经在下面发布了我的用户和存储桶策略,以防出现任何明显的错误。

这是我的 php 代码:

<?php
require 'vendor/autoload.php';

use Aws\S3\S3Client;

// Instantiate an Amazon S3 client.
$s3 = new S3Client([
    'version' => 'latest',
    'region'  => 'US-West', 
    'credentials' => [
        'key'    => 'garbage',
        'secret' => 'garbage'
    ]
]);


$bucketName = 'garbage';
$file_Path = __DIR__ . '/my-image.png';
$key = basename($file_Path);

// Upload a publicly accessible file. The file size and type are determined by the SDK.
try {
    $result = $s3->putObject([
        'Bucket' => $bucketName,
        'Key'    => 'videouploads/' . $key,
        'Body'   => fopen($file_Path, 'r'),
        'ACL'    => 'public-read',
    ]);
    echo $result->get('ObjectURL');
} catch (Aws\S3\Exception\S3Exception $e) {
    echo "There was an error uploading the file.\n";
    echo $e->getMessage();
}


?>

我的存储桶政策:

{
    "Version": "2012-10-17",
    "Id": "Policy1488494182833",
    "Statement": [
        {
            "Sid": "Stmt1488493308547",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::056984788586:user/cactustest123"
            },
            "Action": [
                "s3:ListBucket",
                "s3:ListBucketVersions",
                "s3:GetBucketLocation",
                "s3:Get*",
                "s3:Put*"
            ],
            "Resource": "arn:aws:s3:::cactustestphp"
        }
    ]
}

我的用户政策:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListAllMyBuckets",
                "s3:PutObject",
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::*"
            ]
        }
    ]
}

【问题讨论】:

    标签: php amazon-web-services amazon-s3 sdk


    【解决方案1】:

    请检查您的 IAM 用户或角色是否拥有该存储桶的足够权限,同时查看存储桶策略以确保没有 KMS SSE 强制等策略。

    【讨论】:

    • 我更新了我的帖子以显示我的存储桶和用户的策略。你可以看看他们。
    • 请将“s3:PutObjectAcl”放入Actions
    • 将此行添加到我的用户策略中仍然不起作用。
    • Acyually,我只是在我的 php 代码中注释了引用 acl 的行,它起作用了!
    猜你喜欢
    • 2017-11-06
    • 2017-11-07
    • 1970-01-01
    • 2022-06-10
    • 2018-01-07
    • 2022-01-26
    • 2012-05-28
    • 2017-08-08
    • 2021-04-19
    相关资源
    最近更新 更多