【问题标题】:Aws Php SDk - Create Cloudfront distribution using hard-coded credentialsAws Php SDK - 使用硬编码凭据创建 Cloudfront 分发
【发布时间】:2019-05-09 17:37:58
【问题描述】:

我正在尝试创建一个云端分发,同时通过硬编码凭据进行身份验证。

但是当我运行我的代码时我收到了这个错误 致命错误:未捕获的 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";
    }
}

【问题讨论】:

  • 请在下面发布完整的解决方案作为答案......如果您不确定如何解决问题并且无法再重现问题,请删除该问题。

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 2012-01-04
    相关资源
    最近更新 更多