php Amazon Cloudfront签名的URL访问被拒绝问题

plicqrtu  于 2023-02-28  发布在  PHP
关注(0)|答案(1)|浏览(149)

我正试图建立Amazone cloudfront的签名URL的内容,位于我的S3 bucket。我已经按照程序建立一个签名的URL从amazone aws文档是http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-creating-signed-url-custom-policy.html#private-content-custom-policy-creating-signature-download-procedure,但由于某种原因,我正在建立的URL是得到这个消息“AccessDeniedAccess被拒绝”。
我还从分发设置行为中添加了“受信任的签名人”为“self”,并为该分发添加了orignin。我不确定我遗漏了什么。以下是我的php代码

<?php

    function rsa_sha1_sign($policy, $private_key_filename) 
    {
        $signature = "";

        // load the private key
        $fp = fopen($private_key_filename, "r");
        $priv_key = fread($fp, 8192);
        fclose($fp);
        //echo $priv_key;
        $pkeyid = openssl_get_privatekey($priv_key);

        // compute signature
        openssl_sign($policy, $signature, $pkeyid);

        // free the key from memory
        openssl_free_key($pkeyid);
        //echo $signature;
        return $signature;
     }

    function url_safe_base64_encode($value) 
    {
        $encoded = base64_encode($value);
        // replace unsafe characters +, = and / with 
        // the safe characters -, _ and ~
        return str_replace(
            array('+', '=', '/'),
            array('-', '_', '~'),
            $encoded);
     }

    $key_pair_id = "MY_KEY_PAIR_ID";
    $donwload_cname = "MY_DOWNLOAD_CNAME";
    $download_url = "MY_DOMAIN_NAME/download/2012/01/FILE_NAME.mp3";

    $DateLessThan = time() + (24*7*60*60);
    $policy = '{"Statement":[{"Resource":"'.$download_url.'","Condition":{"DateLessThan":{"AWS:EpochTime":'.$DateLessThan.'}}}]}';

    $private_key_file = "MY_PRIVATE_KEY_FILE.pem";

    $signature = rsa_sha1_sign($policy, $private_key_file);
    $signature = url_safe_base64_encode($signature);

    $final_url = $download_url.'?Policy='.url_safe_base64_encode($policy).'&Signature='.$signature.'&Key-Pair-Id='.$key_pair_id;
    echo $final_url;

?>

我已经尝试与这两个域名和cname为这download_url但是no effects.那是我已经尝试这两个url格式

$download_url = "MY_DOMAIN_NAME/download/2012/01/FILE_NAME.mp3" 
$download_url = "MY_DOWNLOAD_CNAME/download/2012/01/FILE_NAME.mp3"

但是没有工作。有没有人能帮我这个忙。我敢肯定,一些非常小的东西是失踪在这里或需要做的事情与桶设置,但不知道该怎么做,现在。需要紧急帮助

qni6mghb

qni6mghb1#

你还必须在下载URL中添加协议(http://或https://)。因为具有和不具有该协议的签名将不同。Cloud Front后端可能使用完整URL(具有http://或https://)生成签名,但该签名与你的签名不匹配,并且你将收到访问被拒绝错误。
使您的下载网址如下,

$download_url = "http://MY_DOMAIN_NAME/download/2012/01/FILE_NAME.mp3"

或用于https://

$download_url = "https://MY_DOMAIN_NAME/download/2012/01/FILE_NAME.mp3"

相关问题