我在使用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 回答
发现了问题所在 . 我的S3存储桶中的对象无法公开访问,并且我没有添加Origin Access Identity,因此CloudFront无法从我的源(我的S3存储桶)中提取对象来缓存它们 .
一旦我添加了Origin Access Identity并将其添加到我的S3存储桶策略中,我的对象就可以通过我的CloudFront分发通过签名URL立即访问 .
相关文件:https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-restricting-access-to-s3.html#private-content-creating-oai