生成CloudFront签名URL的问题;始终拒绝访问

时间:2018-07-03 10:45:26

标签: amazon-web-services amazon-cloudfront

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

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

在一个简单的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”。

我在做什么错了?

1 个答案:

答案 0 :(得分:3)

发现了问题所在。 S3存储桶中的对象不是公开可访问的,而且我没有添加原始访问身份,因此CloudFront无法从我的原始位置(我的S3存储桶)中提取对象以对其进行缓存。

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

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

相关问题