2018-09-14 17:57:46 -07:00
< ? php
2020-04-21 16:46:08 -07:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2024-02-05 10:49:20 -07:00
2019-01-18 00:02:57 -08:00
// snippet-start:[cloudfront.php.createinvalidation.complete]
// snippet-start:[cloudfront.php.createinvalidation.import]
2018-09-14 17:57:46 -07:00
require 'vendor/autoload.php' ;
2024-02-05 10:49:20 -07:00
2018-09-14 17:57:46 -07:00
use Aws\Exception\AwsException ;
2024-02-05 10:49:20 -07:00
2019-01-18 00:02:57 -08:00
// snippet-end:[cloudfront.php.createinvalidation.import]
2024-02-05 10:49:20 -07:00
2020-04-21 16:46:08 -07:00
/* ////////////////////////////////////////////////////////////////////////////
* Purpose: Invalidates a cached object in an Amazon CloudFront distribution.
*
* Prerequisites: An existing Amazon CloudFront distribution.
*
* Inputs:
* - $cloudFrontClient: An initialized AWS SDK for PHP SDK client
* for CloudFront.
* - $distributionId: The distribution's ID.
* - $callerReference: Any value that uniquely identifies this request.
* - $paths: The list of paths to the cached objects you want to invalidate.
* For more information, see "Specifying the Objects to Invalidate" in the
* Amazon CloudFront Developer Guide.
* - $quantity: The number of invalidation paths specified.
2018-09-14 17:57:46 -07:00
*
2020-04-21 16:46:08 -07:00
* Returns: Information about the invalidation request; otherwise,
* the error message.
* ///////////////////////////////////////////////////////////////////////// */
2024-02-05 10:49:20 -07:00
2019-01-18 00:02:57 -08:00
// snippet-start:[cloudfront.php.createinvalidation.main]
2020-04-21 16:46:08 -07:00
function createInvalidation (
$cloudFrontClient ,
$distributionId ,
$callerReference ,
$paths ,
$quantity
) {
try {
$result = $cloudFrontClient -> createInvalidation ([
'DistributionId' => $distributionId ,
'InvalidationBatch' => [
'CallerReference' => $callerReference ,
'Paths' => [
'Items' => $paths ,
'Quantity' => $quantity ,
],
]
]);
2024-02-05 10:49:20 -07:00
2020-04-27 16:17:12 -07:00
$message = '' ;
2024-02-05 10:49:20 -07:00
2020-04-27 16:17:12 -07:00
if ( isset ( $result [ 'Location' ])) {
$message = 'The invalidation location is: ' . $result [ 'Location' ];
}
2024-02-05 10:49:20 -07:00
2020-04-27 16:17:12 -07:00
$message .= ' and the effective URI is ' . $result [ '@metadata' ][ 'effectiveUri' ] . '.' ;
2024-02-05 10:49:20 -07:00
2020-04-27 16:17:12 -07:00
return $message ;
2020-04-21 16:46:08 -07:00
} catch ( AwsException $e ) {
return 'Error: ' . $e -> getAwsErrorMessage ();
2024-02-05 10:49:20 -07:00
}
2020-04-21 16:46:08 -07:00
}
2024-02-05 10:49:20 -07:00
2020-04-21 16:46:08 -07:00
function createTheInvalidation ()
{
2020-04-27 16:17:12 -07:00
$distributionId = 'E17G7YNEXAMPLE' ;
2020-04-21 16:46:08 -07:00
$callerReference = 'my-unique-value' ;
$paths = [ '/*' ];
$quantity = 1 ;
2024-02-05 10:49:20 -07:00
2020-04-21 16:46:08 -07:00
$cloudFrontClient = new Aws\CloudFront\CloudFrontClient ([
'profile' => 'default' ,
'version' => '2018-06-18' ,
'region' => 'us-east-1'
2018-09-14 17:57:46 -07:00
]);
2024-02-05 10:49:20 -07:00
2020-04-21 16:46:08 -07:00
echo createInvalidation (
$cloudFrontClient ,
$distributionId ,
$callerReference ,
$paths ,
$quantity
);
}
2024-02-05 10:49:20 -07:00
2020-04-21 16:46:08 -07:00
// Uncomment the following line to run this code in an AWS account.
// createTheInvalidation();
2019-01-18 00:02:57 -08:00
// snippet-end:[cloudfront.php.createinvalidation.main]
// snippet-end:[cloudfront.php.createinvalidation.complete]