猿问

Aws Php SDk - 使用硬编码凭据创建 Cloudfront 分发

我正在尝试在通过硬编码凭据进行身份验证的同时创建一个 Cloudfront 发行版。


但是,当我运行我的代码时收到此错误 致命错误:未捕获的 Aws\Exception\CredentialsException:无法从 /.aws/credentials 读取凭据


似乎 aws sdk 正在尝试使用此处列出的第二种方法(https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html)进行身份验证- 当您将凭据放在 ./aws 文件夹中


这是我的代码(取自 aws 文档)-知道为什么这不起作用吗?


public function create_cloudfront_client(){


    $region='us-east-1';   

    $client = new Aws\CloudFront\CloudFrontClient([

        'profile' => 'default',

        'version'       =>  'latest',

        'region'  => 'us-east-1', 

         'debug' => true,

        'credentials' =>[

                    'key'    => $this->aws_key,

                    'secret' => $this->aws_secret,

                    ],

              ]);


    $originName = 'cloudfrontme';

    $s3BucketURL = 'https://s3.amazonaws.com/cloudfrontme';

    $callerReference = 'uniquestring99';

    $comment = 'Created by AWS SDK for PHP';

    $cacheBehavior = [


        'AllowedMethods' => [

            'CachedMethods' => [

                'Items' => ['HEAD', 'GET'],

                'Quantity' => 2,

            ],

            'Items' => ['HEAD', 'GET'],

            'Quantity' => 2,

        ],

        'Compress' => false,

        'DefaultTTL' => 0,

        'FieldLevelEncryptionId' => '',

        'ForwardedValues' => [

            'Cookies' => [

                'Forward' => 'none',

            ],

            'Headers' => [

                'Quantity' => 0,

            ],

            'QueryString' => false,

            'QueryStringCacheKeys' => [

                'Quantity' => 0,

            ],

        ],

        'LambdaFunctionAssociations' => ['Quantity' => 0],

        'MaxTTL' => 0,

        'MinTTL' => 0,

        'SmoothStreaming' => false,

        'TargetOriginId' => $originName,

        'TrustedSigners' => [

            'Enabled' => false,

            'Quantity' => 0,

        ],

        'ViewerProtocolPolicy' => 'allow-all',

    ];

扬帆大鱼
浏览 188回答 1
1回答

UYOU

解决方案是像这样创建云前端客户端 $client =  Aws\CloudFront\CloudFrontClient::factory(array(        'region' => $bucket_region,        'version' => 'latest',        'credentials' => [            'key'    => $this->aws_key,            'secret' => $this->aws_secret,            ]    ));但是我不明白为什么这个版本有效,而下面的(来自 aws docs)却没有。谁能解释一下?谢谢$client = new Aws\CloudFront\CloudFrontClient([    'version'       =>  'latest',    'region' => $bucket_region,     'debug' => true,    'credentials' =>[                'key'    => $this->aws_key,                'secret' => $this->aws_secret,                ],          ]);
随时随地看视频慕课网APP
我要回答