生成 CloudFront 签名 URL 的问题;总是拒绝访问

Issues generating CloudFront signed URLs; always Access Denied

我在使用 CloudFront 生成签名 URL 时遇到问题。无论我尝试什么,我都只会收到“拒绝访问”响应。

我在 CloudFront 中创建了一个分配和一个 CloudFront 密钥对 ID。我已经下载了该密钥对 ID 的私钥和 public 密钥。

在一个简单的 PHP 脚本中,我正在尝试以下操作:

use Aws\CloudFront\CloudFrontClient;

$cloudfront = new CloudFrontClient([
    'credentials' => [
        'key' => '[redacted]', // Access key ID of IAM user with Administrator policy
        'secret' => '[redacted]', // Secret access key of same IAM user
    ],
    'debug' => true,
    'region' => 'eu-west-1',
    'version' => 'latest',
]);

$expires = strtotime('+6 hours');

$resource = 'https://[redacted].cloudfront.net/mp4/bunny-trailer.mp4';

$url = $cloudfront->getSignedUrl([
    'url' => $resource,
    'policy' => json_encode([
        'Statement' => [
            [
                'Resource' => $resource,
                'Condition' => [
                    'DateLessThan' => [
                        'AWS:EpochTime' => $expires,
                    ],
                ],
            ],
        ],
    ]),
    'expires' => $expires,
    'key_pair_id' => '[redacted]', // Access key ID of CloudFront key pair
    'private_key' => '[redacted]', // Relative path to pk-[redacted].pem file
]);

但是当访问生成的 URL 时,它总是在浏览器中给我一个代码为“AccessDenied”的错误。

我做错了什么?

发现问题所在。我的 S3 存储桶中的对象不可公开访问,而且我没有添加源访问身份,因此 CloudFront 无法从我的源(我的 S3 存储桶)中提取对象来缓存它们。

一旦我添加了源访问身份并将其添加到我的 S3 存储桶的策略中,我的对象立即可以通过我的 CloudFront 分配通过签名 URL 进行访问。

相关文档:https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html#private-content-creating-oai