使用 CloudFront API 和第 3 AWS SDK for PHP 版管理 Amazon CloudFront 分發 - AWS SDK for PHP

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 CloudFront API 和第 3 AWS SDK for PHP 版管理 Amazon CloudFront 分發

Amazon 會在全球節點 CloudFront 快取內容,以加快您存放在自己的伺服器或 Amazon S3 和 Amazon EC2 等 Amazon 服務上的靜態和動態檔案的分發速度。當使用者從您的網站要求內容時,如果檔案已快取,請從最近的邊緣位置 CloudFront 提供內容。否則, CloudFront 檢索文件的副本,提供它,然後緩存它的下一個請求。在節點上快取內容會降低在該區域類似使用者請求的延遲。

對於您建立的每個發 CloudFront 佈,您可以指定內容的位置,以及在使用者提出要求時如何散佈內容。這個主題著重於靜態和動態檔案 (例如 HTML、CSS、JSON 和映像檔案) 的分佈。F 或有關隨選視訊 CloudFront 搭配使用的資訊,請參閱隨選和即時串流視訊 CloudFront。

下列範例示範如何:

所有的範例程式碼都可以AWS SDK for PHP在這裡取得 GitHub。

登入資料

在執行範例程式碼之前,請依照中的說明設定您的AWS認證憑證。然後匯入AWS SDK for PHP,如中所述基本使用

如需使用 Amazon 的詳細資訊 CloudFront,請參閱 Amazon 開 CloudFront 發人員指南

創建一個 CloudFront 分佈

從 Amazon S3 儲存貯體建立分發。在以下範例中,已將可選參數標示為註解,但會顯示預設值。若要將自訂新增到分佈,請同時取消 $distribution 中值和參數的註解。

若要建立 CloudFront 發佈,請使用此CreateDistribution作業。

匯入

require 'vendor/autoload.php'; 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\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 分佈

使用ListDistributions操作從您的當前帳戶獲取指定AWS區域中的現有 CloudFront 分配列表。

匯入

require 'vendor/autoload.php'; 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\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\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();