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

时间:2019-05-07 07:02:59

标签: php amazon-web-services aws-sdk amazon-cloudfront aws-php-sdk

我正在尝试通过硬编码凭据进行身份验证时创建一个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',
    ];

    $enabled = false;
    $origin = [
        'Items' => [
            [
                'DomainName' => $s3BucketURL,
                'Id' => $originName,
                'OriginPath' => '',
                'CustomHeaders' => ['Quantity' => 0],
                'S3OriginConfig' => ['OriginAccessIdentity' => ''],

            ],
        ],
        'Quantity' => 1,
    ];



    $distribution = [
        'CallerReference' => $callerReference,
        'Comment' => $comment,
        'DefaultCacheBehavior' => $cacheBehavior,
        'Enabled' => $enabled,
        'Origins' => $origin,

    ];

    try {
        $result = $client->createDistribution([
            'DistributionConfig' => $distribution, //REQUIRED
        ]);
        var_dump($result);
    } catch (AwsException $e) {
        // output error message if fails
        echo $e->getMessage();
        echo "\n";
    }
}

1 个答案:

答案 0 :(得分:0)

解决方案是像这样创建cloudfront客户端

 $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,
                ],
          ]);