管理Amazon CloudFront使用分配CloudFrontAPI 和AWS SDK for PHP版本 3 - 适用于 PHP 的 AWS 开发工具包
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

管理Amazon CloudFront使用分配CloudFrontAPI 和AWS SDK for PHP版本 3

Amazon CloudFront 将内容缓存在全球边缘站点中以加快您存储在自己的服务器或 Amazon 服务(如 Amazon S3 和 Amazon EC2)上的静态和动态文件的分配速度。当用户从您的网站请求内容时,CloudFront 将从最近的边缘站点提供内容(如果文件已在该处缓存)。否则,CloudFront 将检索文件的副本,提供副本,然后将其缓存以供下一个请求使用。在边缘站点缓存内容将减少该区域中类似用户请求的延迟。

对于您创建的每个 CloudFront 分配,指定内容所在的位置以及如何在用户发出请求时分配内容。本主题重点介绍静态和动态文件(如 HTML、CSS、JSON 和图像文件)的分配。有关结合使用 CloudFront 和视频点播的信息,请参阅使用 CloudFront 的视频点播和实时流视频

以下示例演示如何:

All the example code for the AWS SDK for PHP Version 3 is available here on GitHub.

Credentials

在运行示例代码之前,您需要配置 AWS 凭证,如适用于 PHP 的 AWS 开发工具包版本 3 的凭证中所述。然后导入AWS SDK for PHP,如适用于 PHP 的 AWS 开发工具包版本 3 的基本使用模式中所述。

有关使用 Amazon CloudFront 的更多信息,请参阅 Amazon CloudFront 开发人员指南

创建 CloudFront 分配

从 Amazon S3 存储桶创建分配。在以下示例中,已注释掉可选参数,但显示默认值。要向您的分配添加自定义项,请取消注释 $distribution 内的值和参数。

要创建 CloudFront 分配,请使用 CreateDistribution 操作。

导入

require 'vendor/autoload.php'; use Aws\CloudFront\CloudFrontClient; use Aws\Exception\AwsException;

示例代码

function createS3Distribution($cloudFrontClient, $distribution) { try { $result = $cloudFrontClient->createDistribution([ 'DistributionConfig' => $distribution ]); $message = ''; if (isset($result['Distribution']['Id'])) { $message = 'Distribution created with the ID of ' . $result['Distribution']['Id']; } $message .= ' and an effective URI of ' . $result['@metadata']['effectiveUri'] . '.'; return $message; } catch (AwsException $e) { return 'Error: ' . $e['message']; } } function createsTheS3Distribution() { $originName = 'my-unique-origin-name'; $s3BucketURL = 'my-bucket-name.s3.amazonaws.com'; $callerReference = 'my-unique-caller-reference'; $comment = 'my-comment-about-this-distribution'; $defaultCacheBehavior = [ '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' => $defaultCacheBehavior, 'Enabled' => $enabled, 'Origins' => $origin ]; $cloudFrontClient = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-1' ]); echo createS3Distribution($cloudFrontClient, $distribution); } // Uncomment the following line to run this code in an AWS account. // createsTheS3Distribution();

检索 CloudFront 分配

要检索指定 CloudFront 分配的状态和详细信息,请使用 GetDistribution 操作。

导入

require 'vendor/autoload.php'; use Aws\CloudFront\CloudFrontClient; use Aws\Exception\AwsException;

示例代码

function getDistribution($cloudFrontClient, $distributionId) { try { $result = $cloudFrontClient->getDistribution([ 'Id' => $distributionId ]); $message = ''; if (isset($result['Distribution']['Status'])) { $message = 'The status of the distribution with the ID of ' . $result['Distribution']['Id'] . ' is currently ' . $result['Distribution']['Status']; } if (isset($result['@metadata']['effectiveUri'])) { $message .= ', and the effective URI is ' . $result['@metadata']['effectiveUri'] . '.'; } else { $message = 'Error: Could not get the specified distribution. ' . 'The distribution\'s status is not available.'; } return $message; } catch (AwsException $e) { return 'Error: ' . $e->getAwsErrorMessage(); } } function getsADistribution() { $distributionId = 'E1BTGP2EXAMPLE'; $cloudFrontClient = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-1' ]); echo getDistribution($cloudFrontClient, $distributionId); } // Uncomment the following line to run this code in an AWS account. // getsADistribution();

列出 CloudFront 分配

获取现有的CloudFront使用您当前账户使用指定 AWS 区域的ListDistributionsoperation.

导入

require 'vendor/autoload.php'; use Aws\CloudFront\CloudFrontClient; use Aws\Exception\AwsException;

示例代码

function listDistributions($cloudFrontClient) { try { $result = $cloudFrontClient->listDistributions([]); return $result; } catch (AwsException $e) { exit('Error: ' . $e->getAwsErrorMessage()); } } function listTheDistributions() { $cloudFrontClient = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-2' ]); $distributions = listDistributions($cloudFrontClient); if (count($distributions) == 0) { echo 'Could not find any distributions.'; } else { foreach ($distributions['DistributionList']['Items'] as $distribution) { echo 'The distribution with the ID of ' . $distribution['Id'] . ' has the status of ' . $distribution['Status'] . '.' . "\n"; } } } // Uncomment the following line to run this code in an AWS account. // listTheDistributions();

更新 CloudFront 分配

更新 CloudFront 分配的操作与创建分配的操作类似。但是,当您更新分配时,更多字段都是必填字段且必须包含所有值。要对现有分配进行更改,我们建议您首先检索现有分配,然后更新您要在 $distribution 数组中更改的值。

要更新指定的 CloudFront 分配,请使用 UpdateDistribution 操作。

导入

require 'vendor/autoload.php'; use Aws\CloudFront\CloudFrontClient; use Aws\Exception\AwsException;

示例代码

function updateDistribution($cloudFrontClient, $distributionId, $distributionConfig, $eTag) { try { $result = $cloudFrontClient->updateDistribution([ 'DistributionConfig' => $distributionConfig, 'Id' => $distributionId, 'IfMatch' => $eTag ]); return 'The distribution with the following effective URI has ' . 'been updated: ' . $result['@metadata']['effectiveUri']; } catch (AwsException $e) { return 'Error: ' . $e->getAwsErrorMessage(); } } function getDistributionConfig($cloudFrontClient, $distributionId) { try { $result = $cloudFrontClient->getDistribution([ 'Id' => $distributionId, ]); if (isset($result['Distribution']['DistributionConfig'])) { return [ 'DistributionConfig' => $result['Distribution']['DistributionConfig'], 'effectiveUri' => $result['@metadata']['effectiveUri'] ]; } else { return [ 'Error' => 'Error: Cannot find distribution configuration details.', 'effectiveUri' => $result['@metadata']['effectiveUri'] ]; } } catch (AwsException $e) { return [ 'Error' => 'Error: ' . $e->getAwsErrorMessage() ]; } } function getDistributionETag($cloudFrontClient, $distributionId) { try { $result = $cloudFrontClient->getDistribution([ 'Id' => $distributionId, ]); if (isset($result['ETag'])) { return [ 'ETag' => $result['ETag'], 'effectiveUri' => $result['@metadata']['effectiveUri'] ]; } else { return [ 'Error' => 'Error: Cannot find distribution ETag header value.', 'effectiveUri' => $result['@metadata']['effectiveUri'] ]; } } catch (AwsException $e) { return [ 'Error' => 'Error: ' . $e->getAwsErrorMessage() ]; } } function updateADistribution() { // $distributionId = 'E1BTGP2EXAMPLE'; $distributionId = 'E1X3BKQ569KEMH'; $cloudFrontClient = new CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-1' ]); // To change a distribution, you must first get the distribution's // ETag header value. $eTag = getDistributionETag($cloudFrontClient, $distributionId); if (array_key_exists('Error', $eTag)) { exit($eTag['Error']); } // To change a distribution, you must also first get information about // the distribution's current configuration. Then you must use that // information to build a new configuration. $currentConfig = getDistributionConfig($cloudFrontClient, $distributionId); if (array_key_exists('Error', $currentConfig)) { exit($currentConfig['Error']); } // To change a distribution's configuration, you can set the // distribution's related configuration value as part of a change request, // for example: // 'Enabled' => true // Some configuration values are required to be specified as part of a change // request, even if you don't plan to change their values. For ones you // don't want to change but are required to be specified, you can just reuse // their current values, as follows. $distributionConfig = [ 'CallerReference' => $currentConfig['DistributionConfig']["CallerReference"], 'Comment' => $currentConfig['DistributionConfig']["Comment"], 'DefaultCacheBehavior' => $currentConfig['DistributionConfig']["DefaultCacheBehavior"], 'DefaultRootObject' => $currentConfig['DistributionConfig']["DefaultRootObject"], 'Enabled' => $currentConfig['DistributionConfig']["Enabled"], 'Origins' => $currentConfig['DistributionConfig']["Origins"], 'Aliases' => $currentConfig['DistributionConfig']["Aliases"], 'CustomErrorResponses' => $currentConfig['DistributionConfig']["CustomErrorResponses"], 'HttpVersion' => $currentConfig['DistributionConfig']["HttpVersion"], 'CacheBehaviors' => $currentConfig['DistributionConfig']["CacheBehaviors"], 'Logging' => $currentConfig['DistributionConfig']["Logging"], 'PriceClass' => $currentConfig['DistributionConfig']["PriceClass"], 'Restrictions' => $currentConfig['DistributionConfig']["Restrictions"], 'ViewerCertificate' => $currentConfig['DistributionConfig']["ViewerCertificate"], 'WebACLId' => $currentConfig['DistributionConfig']["WebACLId"] ]; echo updateDistribution($cloudFrontClient, $distributionId, $distributionConfig, $eTag['ETag']); } // Uncomment the following line to run this code in an AWS account. // updateADistribution();

禁用 CloudFront 分配

要停用或删除分配,请将其状态从“已部署”更改为“已禁用”。

要禁用指定的 CloudFront 分配,请使用 DisableDistribution 操作。

导入

require 'vendor/autoload.php'; use Aws\CloudFront\CloudFrontClient; use Aws\Exception\AwsException;

示例代码

function disableDistribution($cloudFrontClient, $distributionId, $distributionConfig, $eTag) { try { $result = $cloudFrontClient->updateDistribution([ 'DistributionConfig' => $distributionConfig, 'Id' => $distributionId, 'IfMatch' => $eTag ]); return 'The distribution with the following effective URI has ' . 'been disabled: ' . $result['@metadata']['effectiveUri']; } catch (AwsException $e) { return 'Error: ' . $e->getAwsErrorMessage(); } } function getDistributionConfig($cloudFrontClient, $distributionId) { try { $result = $cloudFrontClient->getDistribution([ 'Id' => $distributionId, ]); if (isset($result['Distribution']['DistributionConfig'])) { return [ 'DistributionConfig' => $result['Distribution']['DistributionConfig'], 'effectiveUri' => $result['@metadata']['effectiveUri'] ]; } else { return [ 'Error' => 'Error: Cannot find distribution configuration details.', 'effectiveUri' => $result['@metadata']['effectiveUri'] ]; } } catch (AwsException $e) { return [ 'Error' => 'Error: ' . $e->getAwsErrorMessage() ]; } } function getDistributionETag($cloudFrontClient, $distributionId) { try { $result = $cloudFrontClient->getDistribution([ 'Id' => $distributionId, ]); if (isset($result['ETag'])) { return [ 'ETag' => $result['ETag'], 'effectiveUri' => $result['@metadata']['effectiveUri'] ]; } else { return [ 'Error' => 'Error: Cannot find distribution ETag header value.', 'effectiveUri' => $result['@metadata']['effectiveUri'] ]; } } catch (AwsException $e) { return [ 'Error' => 'Error: ' . $e->getAwsErrorMessage() ]; } } function disableADistribution() { $distributionId = 'E1BTGP2EXAMPLE'; $cloudFrontClient = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-1' ]); // To disable a distribution, you must first get the distribution's // ETag header value. $eTag = getDistributionETag($cloudFrontClient, $distributionId); if (array_key_exists('Error', $eTag)) { exit($eTag['Error']); } // To delete a distribution, you must also first get information about // the distribution's current configuration. Then you must use that // information to build a new configuration, including setting the new // configuration to disabled. $currentConfig = getDistributionConfig($cloudFrontClient, $distributionId); if (array_key_exists('Error', $currentConfig)) { exit($currentConfig['Error']); } $distributionConfig = [ 'CacheBehaviors' => $currentConfig['DistributionConfig']["CacheBehaviors"], 'CallerReference' => $currentConfig['DistributionConfig']["CallerReference"], 'Comment' => $currentConfig['DistributionConfig']["Comment"], 'DefaultCacheBehavior' => $currentConfig['DistributionConfig']["DefaultCacheBehavior"], 'DefaultRootObject' => $currentConfig['DistributionConfig']["DefaultRootObject"], 'Enabled' => false, 'Origins' => $currentConfig['DistributionConfig']["Origins"], 'Aliases' => $currentConfig['DistributionConfig']["Aliases"], 'CustomErrorResponses' => $currentConfig['DistributionConfig']["CustomErrorResponses"], 'HttpVersion' => $currentConfig['DistributionConfig']["HttpVersion"], 'Logging' => $currentConfig['DistributionConfig']["Logging"], 'PriceClass' => $currentConfig['DistributionConfig']["PriceClass"], 'Restrictions' => $currentConfig['DistributionConfig']["Restrictions"], 'ViewerCertificate' => $currentConfig['DistributionConfig']["ViewerCertificate"], 'WebACLId' => $currentConfig['DistributionConfig']["WebACLId"] ]; echo disableDistribution($cloudFrontClient, $distributionId, $distributionConfig, $eTag['ETag']); } // Uncomment the following line to run this code in an AWS account. // disableADistribution();

删除 CloudFront 分配

一旦分配处于已禁用状态,您即可删除分配。

要删除指定的 CloudFront 分配,请使用 DeleteDistribution 操作。

导入

require 'vendor/autoload.php'; use Aws\CloudFront\CloudFrontClient; use Aws\Exception\AwsException;

示例代码

function deleteDistribution($cloudFrontClient, $distributionId, $eTag) { try { $result = $cloudFrontClient->deleteDistribution([ 'Id' => $distributionId, 'IfMatch' => $eTag ]); return 'The distribution at the following effective URI has ' . 'been deleted: ' . $result['@metadata']['effectiveUri']; } catch (AwsException $e) { return 'Error: ' . $e->getAwsErrorMessage(); } } function getDistributionETag($cloudFrontClient, $distributionId) { try { $result = $cloudFrontClient->getDistribution([ 'Id' => $distributionId, ]); if (isset($result['ETag'])) { return [ 'ETag' => $result['ETag'], 'effectiveUri' => $result['@metadata']['effectiveUri'] ]; } else { return [ 'Error' => 'Error: Cannot find distribution ETag header value.', 'effectiveUri' => $result['@metadata']['effectiveUri'] ]; } } catch (AwsException $e) { return [ 'Error' => 'Error: ' . $e->getAwsErrorMessage() ]; } } function deleteADistribution() { $distributionId = 'E17G7YNEXAMPLE'; $cloudFrontClient = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-1' ]); // To delete a distribution, you must first get the distribution's // ETag header value. $eTag = getDistributionETag($cloudFrontClient, $distributionId); if (array_key_exists('Error', $eTag)) { exit($eTag['Error']); } else { echo deleteDistribution($cloudFrontClient, $distributionId, $eTag['ETag']); } } // Uncomment the following line to run this code in an AWS account. // deleteADistribution();