管理 Amazon CloudFront 失效使用 CloudFront API 和Amazon SDK for PHP版本 3 - Amazon SDK for PHP
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

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

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

亚马逊 CloudFront 将静态和动态文件的副本缓存在全球边缘站点中。要在所有边缘站点上删除或更新文件,请为每个文件或一组文件创建失效。

每个日历月中,您的前 1,000 个失效是免费的。要了解有关从中删除内容的更多信息 CloudFront 边缘站点,请参阅使文件失效.

以下示例演示如何:

Amazon SDK for PHPGitHub 上提供了的所有示例代码。

凭证

运行示例代码之前,请配置Amazon凭证,如中所述设置 凭证. 然后导入Amazon SDK for PHP,如中所述基本用法.

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

创建分配失效

创建 CloudFront 通过指定需要删除的文件的路径位置来使分配失效。此示例将使分配中的所有文件均失效,但您可以在 Items 下标识特定文件。

创建 CloudFront 分配失效,请使用CreateInvalidationoperation.

导入

require 'vendor/autoload.php'; use Aws\CloudFront\CloudFrontClient; 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 = 'E1WICG14DUW2AP'; // '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 分配失效,请使用GetInvalidationoperation.

导入

require 'vendor/autoload.php'; use Aws\CloudFront\CloudFrontClient; 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 分发失效,请使用ListInvalidationsoperation.

导入

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