Mar*_*ean 10 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
]);
Run Code Online (Sandbox Code Playgroud)
但是当访问生成的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