Amazon CloudFront CDN Service (http://aws.amazon.com/cloudfront)
2010.01.25
2006-2010 Ryan Parman, Foleeo, Inc., and contributors.
| Simplified BSD License | http://opensource.org/licenses/bsd-license.php |
| CloudFusion | http://getcloudfusion.com |
| Amazon CloudFront | http://aws.amazon.com/cloudfront |
Specify the default queue URL.
Container for all Amazon CloudFront-related methods. Inherits additional methods from CloudFusion.
CloudFusion
The base content to use for generating the DistributionConfig XML.
The base content to use for generating the StreamingDistributionConfig XML.
public function __construct( $key = null, $secret_key = null )
The constructor
public
| key | string (Optional) Your Amazon API Key. If blank, it will look for the AWS_KEY constant. |
| secret_key | string (Optional) Your Amazon API Secret Key. If blank, it will look for the AWS_SECRET_KEY constant. |
boolean false if no valid values are set, otherwise true.
public function authenticate( $method = HTTP_GET, $path = null, $opt = null, $xml = null, $etag = null )
Authenticates a connection to CloudFront. This should not be used directly unless you’re writing custom methods for this class.
public
| method | string (Required) The HTTP method to use to connect. Accepts HTTP_GET, HTTP_POST, HTTP_PUT, HTTP_DELETE, and HTTP_HEAD. |
| path | string (Optional) The endpoint path to make requests to. |
| opt | array (Optional) Associative array of parameters for authenticating. See the individual methods for allowed keys. |
| xml | string (Optional) The XML body content to send along in the request. |
| etag | string (Optional) The ETag value to pass along with the If-Match HTTP header. |
ResponseCore object
http://docs.amazonwebservices.com
public function disable_ssl()
Throws an error because SSL is required for the CloudFront service.
public
void
public function generate_config_xml( $origin, $caller_reference, $opt = null )
Used to generate the Distribution Config XML used in create_distribution() and set_distribution_config().
public
| origin | string (Required) The source S3 bucket to use for the CloudFront distribution. |
| caller_reference | string (Required) A unique identifier for the request. Must be generated on your own. A time stamp or hash is a good example. |
| opt | array (Optional) Associative array of parameters which can have the following keys: |
| CNAME | string | array (Optional) A DNS CNAME to use to map to the CloudFront distribution. If setting more than one, use an indexed array. Supports 1-10 CNAMEs. |
| Comment | integer (Optional) A comment to apply to the distribution. Cannot exceed 128 characters. |
| Enabled | string (Optional) Defaults to true. Use this to set Enabled to false. |
| Streaming | boolean (Optional) Whether this should be for a streaming distribution or not. Defaults to false. |
String DistributionConfig XML document.
<?php
// Dependencies
require_once 'cloudfusion.class.php';
// Generate configuration XML
$cdn = new AmazonCloudFront();
$response = $cdn->generate_config_xml('warpshare.test.s3.amazonaws.com', 'WarpShareS3', array(
'Enabled' => true,
'Comment' => 'This is my sample comment',
'CNAME' => 'cdn1.warpshare.com'
));
// Success?
var_dump($response);
/** [Expected output]
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Generate configuration XML
$cdn = new AmazonCloudFront();
$response = $cdn->generate_config_xml('warpshare.test.s3.amazonaws.com', 'WarpShareS3', array(
'Enabled' => true,
'Comment' => 'This is my sample comment',
'CNAME' => array(
'cdn1.warpshare.com',
'cdn2.warpshare.com',
'cdn3.warpshare.com'
)
));
// Success?
var_dump($response);
/** [Expected output]
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Generate configuration XML
$cdn = new AmazonCloudFront();
$response = $cdn->generate_config_xml('warpshare.test.s3.amazonaws.com', 'WarpShareS3', array(
'Logging' => array(
'Bucket' => 'warpshare.logging',
'Prefix' => 'wslog_'
)
));
// Success?
var_dump($response);
/** [Expected output]
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Generate configuration XML
$cdn = new AmazonCloudFront();
$response = $cdn->generate_config_xml('warpshare.test.s3.amazonaws.com', 'WarpShareS3', array(
'Enabled' => true,
'Comment' => 'This is my sample comment',
'CNAME' => 'cdn1.warpshare.com',
'Streaming' => true
));
// Success?
var_dump($response);
/** [Expected output]
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Generate configuration XML
$cdn = new AmazonCloudFront();
$response = $cdn->generate_config_xml('warpshare.test.s3.amazonaws.com', 'WarpShareS3', array(
'Enabled' => true,
'Comment' => 'This is my sample comment',
'CNAME' => array(
'cdn1.warpshare.com',
'cdn2.warpshare.com',
'cdn3.warpshare.com'
),
'Streaming' => true
));
// Success?
var_dump($response);
/** [Expected output]
*/
?>| Related | generate_config_xml(), update_config_xml(), remove_cname() |
public function update_config_xml( $xml, $opt = null )
Used to update an existing DistributionConfig XML document.
public
| xml | SimpleXMLElement | ResponseCore | string (Required) The source DistributionConfig XML to make updates to. Can be the SimpleXMLElement body of a get_distribution_config() response, the entire ResponseCore of a get_distribution_config() response, or a string of XML generated by generate_config_xml() or update_config_xml(). |
| opt | array (Optional) Associative array of parameters which can have the following keys: |
| CNAME | string | array (Optional) This (these) value(s) will be ADDED to the existing list of CNAME values. To remove a CNAME value, see remove_cname(). |
| Comment | integer (Optional) This value will replace the existing value for ‘Comment’. Cannot exceed 128 characters. |
| Enabled | string (Optional) This value will replace the existing value for ‘Enabled’. |
| Streaming | boolean (Optional) Whether this should be for a streaming distribution or not. Defaults to false. |
String DistributionConfig XML document.
<?php
// Dependencies
require_once 'cloudfusion.class.php';
$cdn = new AmazonCloudFront();
// Get a full <ResponseCore> object instead of just the SimpleXML body.
$config_xml = $cdn->get_distribution_config('E2L6A3OZHQT5W4');
// Update the XML content
$response = $cdn->update_config_xml($config_xml, array(
'Enabled' => true,
'Comment' => 'This is my sample comment',
'CNAME' => 'cdn1.warpshare.com'
));
// Success?
var_dump($response);
/** [Expected output]
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
$cdn = new AmazonCloudFront();
// Get a full <ResponseCore> object instead of just the SimpleXML body.
$config_xml = $cdn->get_distribution_config('E2L6A3OZHQT5W4');
// Update the XML content
$response = $cdn->update_config_xml($config_xml, array(
'Enabled' => true,
'Comment' => 'This is my sample comment',
'CNAME' => 'cdn1.warpshare.com',
'Streaming' => true
));
// Success?
var_dump($response);
/** [Expected output]
*/
?>| Related | generate_config_xml(), update_config_xml(), remove_cname() |
public function remove_cname( $xml, $cname )
Used to remove one or more CNAMEs from a DistributionConfig XML document.
public
| xml | SimpleXMLElement | ResponseCore | string (Required) The source DistributionConfig XML to make updates to. Can be the SimpleXMLElement body of a get_distribution_config() response, the entire ResponseCore of a get_distribution_config() response, or a string of XML generated by generate_config_xml() or update_config_xml(). |
| cname | string | array (Optional) This (these) value(s) will be REMOVED from the existing list of CNAME values. To add a CNAME value, see update_config_xml(). |
String DistributionConfig XML document.
<?php
// Dependencies
require_once 'cloudfusion.class.php';
$cdn = new AmazonCloudFront();
// Retrieve the cached Distribution ID. See create_distribution() for why we're doing this.
$distribution_id = file_get_contents('create_distribution.cache');
// Sample XML content
$config_xml = $cdn->get_distribution_config($distribution_id);
// Update the XML content
$response = $cdn->remove_cname($config_xml, 'unit-test.warpshare.com');
// Success?
var_dump($response);
/** [Expected output]
*/
?>| Related | generate_config_xml(), update_config_xml(), remove_cname() |
public function create_distribution( $origin, $caller_reference, $opt = null )
The response echoes the DistributionConfig element and returns other metadata about the distribution. For more information, see Parts of a Basic Distribution. It takes a short time for CloudFront to propagate your new distribution’s information throughout the CloudFront system. For more information, see Eventual Consistency. You can have up to 100 distributions in the Amazon CloudFront system.
For an Adobe Real-Time Messaging Protocol (RTMP) streaming distribution, set the Streaming option to true.
public
| origin | string (Required) The source S3 bucket to use for the CloudFront distribution. |
| caller_reference | integer (Required) A unique identifier for the request. Must be generated on your own. A timestamp could be good. |
| opt | array (Optional) Associative array of parameters which can have the following keys: |
| CNAME | string | array (Optional) A DNS CNAME to use to map to the CloudFront distribution. If setting more than one, use an indexed array. Supports 1-10 CNAMEs. |
| Comment | integer (Optional) A comment to apply to the distribution. Cannot exceed 128 characters. |
| Enabled | string (Optional) Defaults to true. Use this to set Enabled to false. |
| Streaming | boolean (Optional) Whether this should be for a streaming distribution or not. Defaults to false. |
| returnCurlHandle | boolean (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests. |
ResponseCore object
<?php
// Dependencies
require_once 'cloudfusion.class.php';
// Create a new CloudFront distribution from an S3 bucket.
$cdn = new AmazonCloudFront();
$response = $cdn->create_distribution('warpshare.test', 'TarzanDemo', array(
'Enabled' => true,
'Comment' => 'This is my sample comment',
'CNAME' => 'unit-test.warpshare.com',
'Logging' => array(
'Bucket' => 'warpshare.logging',
'Prefix' => 'wslog_'
)
));
// Store the Distribution ID so that we can re-use it later. This is useful for unit testing.
// This is The Wrong Way To Do It™
file_put_contents('create_distribution.cache', (string) $response->body->Id);
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Create a new CloudFront distribution from an S3 bucket.
$cdn = new AmazonCloudFront();
$response = $cdn->create_distribution('warpshare.test', 'TarzanDemo2', array(
'Enabled' => true,
'Comment' => 'This is my sample comment',
'CNAME' => 'unit-test2.warpshare.com',
'Logging' => array(
'Bucket' => 'warpshare.logging',
'Prefix' => 'wslog_'
),
'Streaming' => true
));
// Store the Distribution ID so that we can re-use it later. This is useful for unit testing.
// This is The Wrong Way To Do It™
file_put_contents('create_distribution_stream.cache', (string) $response->body->Id);
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?>
public function list_distributions( $opt = null )
Gets a list of your distributions. By default, your entire list of distributions is returned in one single page. If the list is long, you can paginate it using the MaxItems and Marker parameters.
Standard distributions are listed separately from streaming distributions. For streaming distributions, set the Streaming option to true.
public
| opt | array (Required) Associative array of parameters which can have the following keys: |
| Marker | string (Optional) Use this when paginating results to indicate where in your list of distributions to begin. The results include distributions in the list that occur after the marker. To get the next page of results, set the Marker to the value of the NextMarker from the current page’s response (which is also the ID of the last distribution on that page). |
| MaxItems | integer (Optional) The maximum number of distributions you want in the response body. Maximum of 100. |
| Streaming | boolean (Optional) Whether this should be for a streaming distribution or not. Defaults to false. |
| returnCurlHandle | boolean (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests. |
ResponseCore object
<?php // Dependencies require_once 'cloudfusion.class.php'; // Update the XML content $cdn = new AmazonCloudFront(); $response = $cdn->list_distributions(); // Success? var_dump($response->isOK()); /** [Expected output] bool(true) */ ?>
<?php // Dependencies require_once 'cloudfusion.class.php'; // Update the XML content $cdn = new AmazonCloudFront(); $response = $cdn->list_distributions(array( 'MaxItems' => 1 )); // Success? var_dump($response->isOK()); /** [Expected output] bool(true) */ ?>
<?php // Dependencies require_once 'cloudfusion.class.php'; // Update the XML content $cdn = new AmazonCloudFront(); $response = $cdn->list_distributions(array( 'MaxItems' => 1, 'Streaming' => true )); // Success? var_dump($response->isOK()); /** [Expected output] bool(true) */ ?>
public function get_distribution_info( $distribution_id, $opt = null )
Gets information about a given distribution.
Standard distributions are handled separately from streaming distributions. For streaming distributions, set the Streaming option to true.
public
| distribution_id | string (Required) The distribution ID returned from create_distribution() or list_distributions(). |
| opt | array (Required) Associative array of parameters which can have the following keys: |
| Streaming | boolean (Optional) Whether this should be for a streaming distribution or not. Defaults to false. |
| returnCurlHandle | boolean (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests. |
ResponseCore object
<?php
// Dependencies
require_once 'cloudfusion.class.php';
// Retrieve the cached Distribution ID. See create_distribution() for why we're doing this.
$distribution_id = file_get_contents('create_distribution.cache');
// Get distribution info
$cdn = new AmazonCloudFront();
$response = $cdn->get_distribution_info($distribution_id);
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Retrieve the cached Distribution ID. See create_distribution() for why we're doing this.
$distribution_id = file_get_contents('create_distribution_stream.cache');
// Get distribution info
$cdn = new AmazonCloudFront();
$response = $cdn->get_distribution_info($distribution_id, array(
'Streaming' => true
));
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?>
public function delete_distribution( $distribution_id, $etag = null, $opt = null )
Deletes a disabled distribution. If you haven’t disabled the distribution, Amazon CloudFront returns a DistributionNotDisabled error. Use set_distribution_config() to disable a distribution before attempting to delete.
For an Adobe Real-Time Messaging Protocol (RTMP) streaming distribution, set the Streaming option to true.
public
| distribution_id | string (Required) The distribution ID returned from create_distribution() or list_distributions(). |
| etag | string (Required) The ETag header value retrieved from a call to get_distribution_config(). |
| opt | array (Optional) Associative array of parameters which can have the following keys: |
| Streaming | boolean (Optional) Whether this should be for a streaming distribution or not. Defaults to false. |
| returnCurlHandle | boolean (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests. |
ResponseCore object
<?php
// Dependencies
require_once 'cloudfusion.class.php';
// Retrieve the cached Distribution ID. See create_distribution() for why we're doing this.
$distribution_id = file_get_contents('create_distribution.cache');
$cdn = new AmazonCloudFront();
// Fetch an updated ETag value
$etag = $cdn->get_distribution_config($distribution_id)->header['etag'];
// Set the updated config XML to the distribution.
$response = $cdn->delete_distribution($distribution_id, $etag);
// Success?
var_dump($response->isOK());
// If the delete request was successful, delete the cached file.
if ($response->isOK())
{
unlink('create_distribution.cache');
}
/** [Expected output]
bool(true)
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Retrieve the cached Distribution ID. See create_distribution() for why we're doing this.
$distribution_id = file_get_contents('create_distribution_stream.cache');
$cdn = new AmazonCloudFront();
// Fetch an updated ETag value
$etag = $cdn->get_distribution_config($distribution_id)->header['etag'];
// Set the updated config XML to the distribution.
$response = $cdn->delete_distribution($distribution_id, $etag);
// Success?
var_dump($response->isOK());
// If the delete request was successful, delete the cached file.
if ($response->isOK())
{
unlink('create_distribution_stream.cache');
}
/** [Expected output]
bool(true)
*/
?>
public function get_distribution_config( $distribution_id, $opt = null )
Gets the current distribution config information for a given distribution ID.
Standard distributions are handled separately from streaming distributions. For streaming distributions, set the Streaming option to true.
public
| distribution_id | string (Required) The distribution ID returned from create_distribution() or list_distributions(). |
| opt | array (Required) Associative array of parameters which can have the following keys: |
| Streaming | boolean (Optional) Whether this should be for a streaming distribution or not. Defaults to false. |
| returnCurlHandle | boolean (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests. |
ResponseCore object
<?php
// Dependencies
require_once 'cloudfusion.class.php';
// Retrieve the cached Distribution ID. See create_distribution() for why we're doing this.
$distribution_id = file_get_contents('create_distribution.cache');
// Get distribution info
$cdn = new AmazonCloudFront();
$response = $cdn->get_distribution_config($distribution_id);
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Get distribution info
$cdn = new AmazonCloudFront();
$response = $cdn->get_distribution_config('E2L6A3OZHQT5W4', array(
'returnCurlHandle' => true
));
// Success?
var_dump($response);
/** [Expected output]
resource(9) of type (curl)
*/
?>| AWS Method | http://docs.amazonwebservices.com |
| Related | get_distribution_config(), set_distribution_config() |
public function set_distribution_config( $distribution_id, $xml, $etag, $opt = null )
Sets a new distribution config for a given distribution ID.
Standard distributions are handled separately from streaming distributions. For streaming distributions, set the Streaming option to true.
public
| distribution_id | string (Required) The distribution ID returned from create_distribution() or list_distributions(). |
| xml | string (Required) The DistributionConfig XML generated by generate_config_xml() or update_config_xml(). |
| etag | string (Required) The ETag header value retrieved from a call to get_distribution_config(). |
| opt | array (Required) Associative array of parameters which can have the following keys: |
| Streaming | boolean (Optional) Whether this should be for a streaming distribution or not. Defaults to false. |
| returnCurlHandle | boolean (Optional) A private toggle that will return the CURL handle for the request rather than actually completing the request. This is useful for MultiCURL requests. |
ResponseCore object
<?php
// Dependencies
require_once 'cloudfusion.class.php';
// Retrieve the cached Distribution ID. See create_distribution() for why we're doing this.
$distribution_id = file_get_contents('create_distribution.cache');
$cdn = new AmazonCloudFront();
// Pull existing config XML...
$existing_xml = $cdn->get_distribution_config($distribution_id);
// Generate an updated XML config...
$updated_xml = $cdn->update_config_xml($existing_xml, array(
'Enabled' => false
));
// Fetch an updated ETag value
$etag = $cdn->get_distribution_config($distribution_id)->header['etag'];
// Set the updated config XML to the distribution.
$response = $cdn->set_distribution_config($distribution_id, $updated_xml, $etag);
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?> <?php
// Dependencies
require_once 'cloudfusion.class.php';
// Retrieve the cached Distribution ID. See create_distribution() for why we're doing this.
$distribution_id = file_get_contents('create_distribution_stream.cache');
$cdn = new AmazonCloudFront();
// Pull existing config XML...
$existing_xml = $cdn->get_distribution_config($distribution_id, array(
'Streaming' => true
));
// Generate an updated XML config...
$updated_xml = $cdn->update_config_xml($existing_xml, array(
'Enabled' => false,
'Streaming' => true
));
// Fetch an updated ETag value
$etag = $cdn->get_distribution_config($distribution_id, array(
'Streaming' => true
))->header['etag'];
// Set the updated config XML to the distribution.
$response = $cdn->set_distribution_config($distribution_id, $updated_xml, $etag, array(
'Streaming' => true
));
// Success?
var_dump($response->isOK());
/** [Expected output]
bool(true)
*/
?>| AWS Method | http://docs.amazonwebservices.com |
| Related | get_distribution_config(), set_distribution_config() |