CloudFront

Amazon CloudFront CDN Service (http://aws.amazon.com/cloudfront)

Version

2010.01.25

Copyright

2006-2010 Ryan Parman, Foleeo, Inc., and contributors.

License

Simplified BSD Licensehttp://opensource.org/licenses/bsd-license.php

See Also

CloudFusionhttp://getcloudfusion.com
Amazon CloudFronthttp://aws.amazon.com/cloudfront

Constants

CDN_DEFAULT_URL

Specify the default queue URL.

AmazonCloudFront

Container for all Amazon CloudFront-related methods.  Inherits additional methods from CloudFusion.

Extends

CloudFusion

Properties

base_standard_xml

The base content to use for generating the DistributionConfig XML.

base_streaming_xml

The base content to use for generating the StreamingDistributionConfig XML.

Functions

__construct()

public function __construct($key =  null,
$secret_key =  null)

The constructor

Access

public

Parameters

keystring (Optional) Your Amazon API Key.  If blank, it will look for the AWS_KEY constant.
secret_keystring (Optional) Your Amazon API Secret Key.  If blank, it will look for the AWS_SECRET_KEY constant.

Returns

boolean false if no valid values are set, otherwise true.

authenticate()

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.

Access

public

Parameters

methodstring (Required) The HTTP method to use to connect.  Accepts HTTP_GET, HTTP_POST, HTTP_PUT, HTTP_DELETE, and HTTP_HEAD.
pathstring (Optional) The endpoint path to make requests to.
optarray (Optional) Associative array of parameters for authenticating.  See the individual methods for allowed keys.
xmlstring (Optional) The XML body content to send along in the request.
etagstring (Optional) The ETag value to pass along with the If-Match HTTP header.

Returns

ResponseCore object

See Also

http://docs.amazonwebservices.com/AmazonCloudFront/latest/DeveloperGuide/RESTAuthentication.html

disable_ssl()

public function disable_ssl()

Throws an error because SSL is required for the CloudFront service.

Access

public

Returns

void

generate_config_xml()

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().

Access

public

Parameters

originstring (Required) The source S3 bucket to use for the CloudFront distribution.
caller_referencestring (Required) A unique identifier for the request.  Must be generated on your own.  A time stamp or hash is a good example.
optarray (Optional) Associative array of parameters which can have the following keys:

Keys for the $opt parameter

CNAMEstring | 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.
Commentinteger (Optional) A comment to apply to the distribution.  Cannot exceed 128 characters.
Enabledstring (Optional) Defaults to true.  Use this to set Enabled to false.
Streamingboolean (Optional) Whether this should be for a streaming distribution or not.  Defaults to false.

Returns

String DistributionConfig XML document.

Examples

CloudFront: generate_config_xml() testing enabled, comments, and single CNAME
<?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] 
*/
?>
CloudFront: generate_config_xml() testing enabled, comments, and multiple CNAMEs
<?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] 
*/
?>
CloudFront: generate_config_xml() testing logging
<?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] 
*/
?>
CloudFront: generate_config_xml() testing enabled, comments, and single CNAME
<?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] 
*/
?>
CloudFront: generate_config_xml() testing enabled, comments, and multiple CNAMEs
<?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] 
*/
?>

See Also

Relatedgenerate_config_xml(), update_config_xml(), remove_cname()

update_config_xml()

public function update_config_xml($xml,  
$opt =  null)

Used to update an existing DistributionConfig XML document.

Access

public

Parameters

xmlSimpleXMLElement | 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().
optarray (Optional) Associative array of parameters which can have the following keys:

Keys for the $opt parameter

CNAMEstring | array (Optional) This (these) value(s) will be ADDED to the existing list of CNAME values.  To remove a CNAME value, see remove_cname().
Commentinteger (Optional) This value will replace the existing value for ‘Comment’.  Cannot exceed 128 characters.
Enabledstring (Optional) This value will replace the existing value for ‘Enabled’.
Streamingboolean (Optional) Whether this should be for a streaming distribution or not.  Defaults to false.

Returns

String DistributionConfig XML document.

Examples

CloudFront: update_config_xml() testing enabled, comments, and single CNAME using a ResponseCore object.
<?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] 
*/
?>
CloudFront: update_config_xml() testing enabled, comments, and single CNAME using a ResponseCore object, with streaming.
<?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] 
*/
?>

See Also

Relatedgenerate_config_xml(), update_config_xml(), remove_cname()

remove_cname()

public function remove_cname($xml,
$cname)

Used to remove one or more CNAMEs from a DistributionConfig XML document.

Access

public

Parameters

xmlSimpleXMLElement | 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().
cnamestring | 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().

Returns

String DistributionConfig XML document.

Examples

CloudFront: remove_cname() using full ResponseCore object
<?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] 
*/
?>

See Also

Relatedgenerate_config_xml(), update_config_xml(), remove_cname()

create_distribution()

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.

Access

public

Parameters

originstring (Required) The source S3 bucket to use for the CloudFront distribution.
caller_referenceinteger (Required) A unique identifier for the request.  Must be generated on your own.  A timestamp could be good.
optarray (Optional) Associative array of parameters which can have the following keys:

Keys for the $opt parameter

CNAMEstring | 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.
Commentinteger (Optional) A comment to apply to the distribution.  Cannot exceed 128 characters.
Enabledstring (Optional) Defaults to true.  Use this to set Enabled to false.
Streamingboolean (Optional) Whether this should be for a streaming distribution or not.  Defaults to false.
returnCurlHandleboolean (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.

Returns

ResponseCore object

Examples

CloudFront: create_distribution()
<?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)
*/
?>
CloudFront: create_distribution(), with streaming.
<?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)
*/
?>

See Also

AWS Methodhttp://docs.amazonwebservices.com/AmazonCloudFront/latest/DeveloperGuide/CreateDistribution.html
Relatedcreate_distribution(), list_distributions(), get_distribution_info(), delete_distribution()

list_distributions()

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.

Access

public

Parameters

optarray (Required) Associative array of parameters which can have the following keys:

Keys for the $opt parameter

Markerstring (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).
MaxItemsinteger (Optional) The maximum number of distributions you want in the response body.  Maximum of 100.
Streamingboolean (Optional) Whether this should be for a streaming distribution or not.  Defaults to false.
returnCurlHandleboolean (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.

Returns

ResponseCore object

Examples

CloudFront: list_distributions()
<?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)
*/
?>
CloudFront: list_distributions() with MaxResults
<?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)
*/
?>
CloudFront: list_distributions() with MaxResults and streaming.
<?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)
*/
?>

See Also

AWS Methodhttp://docs.amazonwebservices.com/AmazonCloudFront/latest/DeveloperGuide/ListDistributions.html
Relatedcreate_distribution(), list_distributions(), get_distribution_info(), delete_distribution()

get_distribution_info()

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.

Access

public

Parameters

distribution_idstring (Required) The distribution ID returned from create_distribution() or list_distributions().
optarray (Required) Associative array of parameters which can have the following keys:

Keys for the $opt parameter

Streamingboolean (Optional) Whether this should be for a streaming distribution or not.  Defaults to false.
returnCurlHandleboolean (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.

Returns

ResponseCore object

Examples

CloudFront: get_distribution_info()
<?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)
*/
?>
CloudFront: get_distribution_info() with streaming.
<?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)
*/
?>

See Also

AWS Methodhttp://docs.amazonwebservices.com/AmazonCloudFront/latest/DeveloperGuide/GetDistribution.html
Relatedcreate_distribution(), list_distributions(), get_distribution_info(), delete_distribution()

delete_distribution()

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.

Access

public

Parameters

distribution_idstring (Required) The distribution ID returned from create_distribution() or list_distributions().
etagstring (Required) The ETag header value retrieved from a call to get_distribution_config().
optarray (Optional) Associative array of parameters which can have the following keys:

Keys for the $opt parameter

Streamingboolean (Optional) Whether this should be for a streaming distribution or not.  Defaults to false.
returnCurlHandleboolean (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.

Returns

ResponseCore object

Examples

CloudFront: delete_distribution()
<?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)
*/
?>
CloudFront: delete_distribution() with streaming.
<?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)
*/
?>

See Also

AWS Methodhttp://docs.amazonwebservices.com/AmazonCloudFront/latest/DeveloperGuide/DeleteDistribution.html
Relatedcreate_distribution(), list_distributions(), get_distribution_info(), delete_distribution()

get_distribution_config()

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.

Access

public

Parameters

distribution_idstring (Required) The distribution ID returned from create_distribution() or list_distributions().
optarray (Required) Associative array of parameters which can have the following keys:

Keys for the $opt parameter

Streamingboolean (Optional) Whether this should be for a streaming distribution or not.  Defaults to false.
returnCurlHandleboolean (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.

Returns

ResponseCore object

Examples

CloudFront: get_distribution_config()
<?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)
*/
?>
CloudFront: get_distribution_config() with returnCurlHandle
<?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)
*/
?>

See Also

AWS Methodhttp://docs.amazonwebservices.com/AmazonCloudFront/latest/DeveloperGuide/GetConfig.html
Relatedget_distribution_config(), set_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.

Access

public

Parameters

distribution_idstring (Required) The distribution ID returned from create_distribution() or list_distributions().
xmlstring (Required) The DistributionConfig XML generated by generate_config_xml() or update_config_xml().
etagstring (Required) The ETag header value retrieved from a call to get_distribution_config().
optarray (Required) Associative array of parameters which can have the following keys:

Keys for the $opt parameter

Streamingboolean (Optional) Whether this should be for a streaming distribution or not.  Defaults to false.
returnCurlHandleboolean (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.

Returns

ResponseCore object

Examples

CloudFront: set_distribution_config()
<?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)
*/
?>
CloudFront: set_distribution_config() with streaming.
<?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)
*/
?>

See Also

AWS Methodhttp://docs.amazonwebservices.com/AmazonCloudFront/latest/DeveloperGuide/PutConfig.html
Relatedget_distribution_config(), set_distribution_config()