如何更新Amazon CloudFront分配

时间:2019-06-19 05:58:03

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

我想使用AWS提供的updatedistribution方法来更新Amazon CloudFront分发的状态。

我不知道所有必需参数的默认值。

我的代码是:

list($before, $after) = explode('.', $domain, 2);
        $domain_Items = "*." . $after;

        $result = $client->updateDistribution([
            'DistributionConfig' =>
            [
                'Aliases' =>
                [
                    'Items' => [$domain_Items, $after],
                    'Quantity' => 2
                ],
                'CallerReference' => $domain,
                'Comment' => 'custom domain for ' . $domain,
                'Enabled' => false,
                'PriceClass' => 'PriceClass_All',
                'CacheBehaviors' => [
                    'Items' => [
                        [
                            'AllowedMethods' =>
                            [
                                'CachedMethods' =>
                                [
                                    'Items' => ['HEAD', 'GET'],
                                    'Quantity' => 2,
                                ],
                                'Items' => ['HEAD', 'GET', 'OPTIONS', 'PUT', 'POST', 'PATCH', 'DELETE'],
                                'Quantity' => 7,
                            ],
                            'Compress' => true,
                            'DefaultTTL' => 0,
                            //'FieldLevelEncryptionId' => '<string>',
                            'ForwardedValues' => [
                                'Cookies' => [
                                    'Forward' => 'all',
                                    'WhitelistedNames' => [
                                        'Quantity' => 5,
                                        'Items' => ['Host', 'Referer', 'Orign', 'User-Agent', 'HTTP_REFERER'],
                                    ],
                                ],
                                'Headers' =>
                                [
                                    'Items' => ['Host', 'Referer', 'Orign', 'User-Agent', 'HTTP_REFERER'],
                                    'Quantity' => 5
                                ],
                                'QueryString' => true,
                                'QueryStringCacheKeys' => [
                                    'Items' => ['Host', 'Referer', 'Orign', 'User-Agent', 'HTTP_REFERER'],
                                    'Quantity' => 5
                                ],
                            ],
                            'LambdaFunctionAssociations' => [
                                'Items' => [
                                    [
                                        'EventType' => 'viewer-request',
                                        'IncludeBody' => true,
                                        'LambdaFunctionARN' => '', //<string>
                                    ]
                                ],
                                'Quantity' => 1,
                            ],
                            'MaxTTL' => 0,
                            'MinTTL' => 0,
                            'PathPattern' => '', //<string>
                            'SmoothStreaming' => true,
                            'TargetOriginId' => 'ELB-saglus-test-uat-web-783948842',
                            'TrustedSigners' => [
                                'Enabled' => false,
                                //'Items' => ['<string>'],
                                'Quantity' => 0,
                            ],
                            'ViewerProtocolPolicy' => 'redirect-to-https',
                        ],
                    ],
                    'Quantity' => 1,
                ],
                'DefaultCacheBehavior' =>
                [
                    'AllowedMethods' =>
                    [
                        'CachedMethods' =>
                        [
                            'Items' => ['HEAD', 'GET'],
                            'Quantity' => 2,
                        ],
                        'Items' => ['HEAD', 'GET', 'OPTIONS', 'PUT', 'POST', 'PATCH', 'DELETE'],
                        'Quantity' => 7,
                    ],
                    'Compress' => true,
                    'DefaultTTL' => 0,
                    'FieldLevelEncryptionId' => '',
                    'ForwardedValues' =>
                    [
                        'Cookies' =>
                        [
                            'Forward' => 'all'
                        ],
                        'WhitelistedNames' => [
                            'Quantity' => 5,
                            'Items' => ['Host', 'Referer', 'Orign', 'User-Agent', 'HTTP_REFERER'],
                        ],
                        'Headers' =>
                        [
                            'Items' => ['Host', 'Referer', 'Orign', 'User-Agent', 'HTTP_REFERER'],
                            'Quantity' => 5,
                        ],
                        'QueryString' => true,
                        'QueryStringCacheKeys' => [
                            'Items' => ['Host', 'Referer', 'Orign', 'User-Agent', 'HTTP_REFERER'],
                            'Quantity' => 5, // REQUIRED
                        ],
                    ],
                    'LambdaFunctionAssociations' => [
                        'Items' => [
                            [
                                'EventType' => 'viewer-request',
                                'IncludeBody' => false,
                                'LambdaFunctionARN' => '', // REQUIRED <string>
                            ]
                        ],
                        'Quantity' => 1, // REQUIRED
                    ],
                    'MaxTTL' => 600,
                    'MinTTL' => 0,
                    'SmoothStreaming' => false,
                    'TargetOriginId' => 'ELB-saglus-test-uat-web-783948842',
                    'TrustedSigners' =>
                    [
                        'Enabled' => false,
                        'Quantity' => 0
                    ],
                    'ViewerProtocolPolicy' => 'redirect-to-https'
                ],
                'DefaultRootObject' => '',
                'HttpVersion' => 'http2',
                'IsIPV6Enabled' => false,
                'Logging' => [
                    'Bucket' => 'saglus-aws-logs.s3.amazonaws.com', // REQUIRED
                    'Enabled' => true, // REQUIRED
                    'IncludeCookies' => true, // REQUIRED
                    'Prefix' => 'logs-for-' . $domain, // REQUIRED
                ],
                'Origins' =>
                [
                    'Items' =>
                    [
                        [
                            'CustomHeaders' =>
                            [
                                'Items' =>
                                [
                                    [
                                        'HeaderName' => 'X-Origin-Verify',
                                        'HeaderValue' => 'cnxpwcausbtobmebhebadbergdifn'
                                    ],
                                ],
                                'Quantity' => 1
                            ],
                            'CustomOriginConfig' =>
                            [
                                'HTTPPort' => 80,
                                'HTTPSPort' => 443,
                                'OriginKeepaliveTimeout' => 5,
                                'OriginProtocolPolicy' => 'http-only',
                                'OriginReadTimeout' => 120,
                                'OriginSslProtocols' =>
                                [
                                    'Items' => ['TLSv1'],
                                    'Quantity' => 1
                                ],
                            ],
                            'DomainName' => 'saglus-test-uat-web-783948842.us-east-1.elb.amazonaws.com',
                            'Id' => 'ELB-saglus-test-uat-web-783948842',
                            'OriginPath' => ''
                        ],
                    ],
                    'Quantity' => 1,
                ],
                'WebACLId' => '108e7697-00db-4330-8d55-bbe57ca94e44'
            ],
            'Id' => $distribution_id,
            'IfMatch' => $ETag,
        ]);

但是,我得到了错误:

error

参考链接:updatedistribution in AWS SDK for PHP 3.x

1 个答案:

答案 0 :(得分:0)

$client = new Aws\CloudFront\CloudFrontClient([
            'region' => 'us-east-1',
            'version' => 'latest',
            'credentials' => [
                'key' => $this->AcmKey,
                'secret' => $this->AcmSecret
            ]
        ]);

        //        $id = 'E2SYUN95DWJFXC';
        $id = $business_custom_data->distribution_id;
        try {
            $result = $client->getDistribution([
                'Id' => $id,
            ]);
        } catch (AwsException $e) {
            // output error message if fails
            echo $e->getMessage();
            echo "\n";
        }

        $currentConfig = $result["Distribution"]["DistributionConfig"];
        $ETag = $result["ETag"];


        $distribution = [
            'CallerReference' => $currentConfig["CallerReference"], // REQUIRED
            'Comment' => $currentConfig["Comment"], // REQUIRED
            'DefaultCacheBehavior' => $currentConfig["DefaultCacheBehavior"], // REQUIRED
            'DefaultRootObject' => $currentConfig["DefaultRootObject"],
            //'Enabled' => $currentConfig["Enabled"], // REQUIRED
            'Enabled' => False, // REQUIRED
            'Origins' => $currentConfig["Origins"], // REQUIRED
            'Aliases' => $currentConfig["Aliases"],
            'CustomErrorResponses' => $currentConfig["CustomErrorResponses"],
            'HttpVersion' => $currentConfig["HttpVersion"],
            'CacheBehaviors' => $currentConfig["CacheBehaviors"],
            'Logging' => $currentConfig["Logging"],
            'PriceClass' => $currentConfig["PriceClass"],
            'Restrictions' => $currentConfig["Restrictions"],
            'ViewerCertificate' => $currentConfig["ViewerCertificate"],
            'WebACLId' => $currentConfig["WebACLId"],
        ];


        try {
            $result = $client->updateDistribution([
                'DistributionConfig' => $distribution,
                'Id' => $id,
                'IfMatch' => $ETag
            ]);
            $status = true;
            //var_dump($result);
            //die;
        } catch (AwsException $e) {
            // output error message if fails
            echo $e->getMessage();
            echo "\n";
        }