适用于 PHP 的 AWS 开发工具包
开发人员指南
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

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

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

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

以下示例演示如何:

GitHub 上的此处提供了AWS SDK for PHP版本 3 的所有示例代码。

凭证

在运行示例代码之前,您需要配置 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;

示例代码

$client = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-2' ]); $originName = 'Name to identify the S3 bucket'; $s3BucketURL = '<bucket-name>.s3.amazonaws.com'; $callerReference = 'unique string'; $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, ]; /* * $cache = [ * 'Quantity' => 0, * ]; * $rootObject = '<string>'; * $alias = [ * 'Quantity' => 0, * ]; * $customError = [ * 'Quantity' => 0, * ]; * $httpVersion = 'http1.1'; * $IPV6 = false; * $logging = [ * 'Bucket' => '', * 'Enabled' => false, * 'IncludeCookies' => false, * 'Prefix' => '', * ]; * $priceClass = 'PriceClass_100'; * $restrictions = [ * 'GeoRestriction' => [ * 'Quantity' => 0, * 'RestrictionType' => 'none', * ], * ]; * $viewerCert = [ * 'CertificateSource' => 'cloudfront', * 'CloudFrontDefaultCertificate' => true, * 'MinimumProtocolVersion' => 'TLSv1', * ]; * $webACLid = ''; */ $distribution = [ 'CallerReference' => $callerReference, 'Comment' => $comment, 'DefaultCacheBehavior' => $cacheBehavior, 'Enabled' => $enabled, 'Origins' => $origin, //'CacheBehaviors' => $cache, //'DefaultRootObject' => $rootObject, //'Aliases' => $alias, //'CustomErrorResponses' => $customError, //'HttpVersion' => $httpVersion, //'IsIPV6Enabled' => $IPV6, //'Logging' => $logging, //'PriceClass' => $priceClass, //'Restrictions' => $restrictions, //'ViewerCertificate' => $viewerCert, //'WebACLId' => $webACLid, ]; try { $result = $client->createDistribution([ 'DistributionConfig' => $distribution, //REQUIRED ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

检索 CloudFront 分配

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

导入

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

示例代码

$client = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-2' ]); $distributionId = 'E1A2B3C4D5E'; try { $result = $client->getDistribution([ 'Id' => $distributionId, //REQUIRED ]); print("<p>The Distribution " . $result['Distribution']['Id'] . " is currently " . $result['Distribution']['Status'] . "</p>"); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

列出 CloudFront 分配

使用 ListDistributions 操作从您的当前账户获取指定 AWS 区域中现有 CloudFront 分配的列表。

导入

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

示例代码

$client = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-2' ]); try { $result = $client->listDistributions([ ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

更新 CloudFront 分配

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

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

导入

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

示例代码

$client = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-2' ]); $id = 'E1A2B3C4D5E'; 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 '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 ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

禁用 CloudFront 分配

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

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

导入

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

示例代码

$client = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-2' ]); $id = 'E1A2B3C4D5E'; try { $result = $client->getDistribution([ 'Id' => $id, ]); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; } $enabled = false; $currentConfig = $result["Distribution"]["DistributionConfig"]; $ETag = $result["ETag"]; $distribution = [ 'CacheBehaviors' => $currentConfig["CacheBehaviors"], //REQUIRED 'CallerReference' => $currentConfig["CallerReference"], // REQUIRED 'Comment' => $currentConfig["Comment"], // REQUIRED 'DefaultCacheBehavior' => $currentConfig["DefaultCacheBehavior"], // REQUIRED 'DefaultRootObject' => $currentConfig["DefaultRootObject"], 'Enabled' => $enabled, // REQUIRED 'Origins' => $currentConfig["Origins"], // REQUIRED 'Aliases' => $currentConfig["Aliases"], 'CustomErrorResponses' => $currentConfig["CustomErrorResponses"], 'HttpVersion' => $currentConfig["HttpVersion"], 'IsIPV6Enabled' => $currentConfig["IsIPV6Enabled"], 'Logging' => $currentConfig["Logging"], 'PriceClass' => $currentConfig["PriceClass"], 'Restrictions' => $currentConfig["Restrictions"], 'ViewerCertificate' => $currentConfig["ViewerCertificate"], 'WebACLId' => $currentConfig["WebACLId"], ]; //var_dump($distribution); try { $result = $client->updateDistribution([ 'DistributionConfig' => $distribution, 'Id' => $id, 'IfMatch' => $ETag ]); print("<p>For The Distribution " . $result['Distribution']['Id'] . " enabled is set to " . $result['Distribution']['DistributionConfig']['Enabled'] . "</p>"); var_dump($result['Distribution']['DistributionConfig']['Enabled']); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

删除 CloudFront 分配

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

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

导入

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

示例代码

$client = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-2' ]); $id = 'E1A2B3C4D5E'; try { $result = $client->getDistribution([ 'Id' => $id, ]); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; } $ETag = $result["ETag"]; try { $result = $client->deleteDistribution([ 'Id' => $id, 'IfMatch' => $ETag ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }