使用 CloudFront API 和版 CloudFront 本 3 管理 Amazon 無效 AWS SDK for PHP - AWS SDK for PHP

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

使用 CloudFront API 和版 CloudFront 本 3 管理 Amazon 無效 AWS SDK for PHP

Amazon 會在全球節點 CloudFront 快取靜態和動態檔案的副本。若要在所有節點移除或更新檔案,請為每個檔案或一組檔案建立失效。

每個日曆月的前 1,000 個失效都是免費的。若要深入瞭解如何從 CloudFront 邊緣位置移除內容,請參閱使檔案無效

下列範例示範如何:

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

登入資料

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

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

建立散發無效驗證

通過指定您需要刪除的文件的路徑位置來創建 CloudFront 分發失效。此範例會使分佈中的所有檔案失效,但您可以識別 Items 下的特定檔案。

若要建立 CloudFront 分配失效,請使用作CreateInvalidation業。

匯入

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

範例程式碼

function createInvalidation( $cloudFrontClient, $distributionId, $callerReference, $paths, $quantity ) { try { $result = $cloudFrontClient->createInvalidation([ 'DistributionId' => $distributionId, 'InvalidationBatch' => [ 'CallerReference' => $callerReference, 'Paths' => [ 'Items' => $paths, 'Quantity' => $quantity, ], ] ]); $message = ''; if (isset($result['Location'])) { $message = 'The invalidation location is: ' . $result['Location']; } $message .= ' and the effective URI is ' . $result['@metadata']['effectiveUri'] . '.'; return $message; } catch (AwsException $e) { return 'Error: ' . $e->getAwsErrorMessage(); } } function createTheInvalidation() { $distributionId = 'E17G7YNEXAMPLE'; $callerReference = 'my-unique-value'; $paths = ['/*']; $quantity = 1; $cloudFrontClient = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-1' ]); echo createInvalidation( $cloudFrontClient, $distributionId, $callerReference, $paths, $quantity ); } // Uncomment the following line to run this code in an AWS account. // createTheInvalidation();

取得散佈失效

若要擷取有關 CloudFront 發佈失效的狀態和詳細資訊,請使用GetInvalidation作業。

匯入

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

範例程式碼

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

列表分配無效

若要列出所有目前的 CloudFront 分配無效,請使用作ListInvalidations業。

匯入

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

範例程式碼

function listInvalidations($cloudFrontClient, $distributionId) { try { $result = $cloudFrontClient->listInvalidations([ 'DistributionId' => $distributionId ]); return $result; } catch (AwsException $e) { exit('Error: ' . $e->getAwsErrorMessage()); } } function listTheInvalidations() { $distributionId = 'E1WICG1EXAMPLE'; $cloudFrontClient = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-1' ]); $invalidations = listInvalidations( $cloudFrontClient, $distributionId ); if (isset($invalidations['InvalidationList'])) { if ($invalidations['InvalidationList']['Quantity'] > 0) { foreach ($invalidations['InvalidationList']['Items'] as $invalidation) { echo 'The invalidation with the ID of ' . $invalidation['Id'] . ' has the status of ' . $invalidation['Status'] . '.' . "\n"; } } else { echo 'Could not find any invalidations for the specified distribution.'; } } else { echo 'Error: Could not get invalidation information. Could not get ' . 'information about the specified distribution.'; } } // Uncomment the following line to run this code in an AWS account. // listTheInvalidations();