generate_config_xml ( $origin, $caller_reference, $opt )

Generates the distribution configuration XML used with create_distribution() and set_distribution_config().

Access

public

Parameters

Parameter

Type

Required

Description

$origin

string

Required

The source to use for the Amazon CloudFront distribution. Use an Amazon S3 bucket name, or a fully-qualified non-S3 domain name prefixed with http:// or https://.

$caller_reference

string

Required

A unique identifier for the request. A timestamp-appended string is recommended.

$opt

array

Optional

An associative array of parameters that can have the following keys:

  • CNAME - string|array - Optional - A DNS CNAME to use to map to the Amazon CloudFront distribution. If setting more than one, use an indexed array. Supports 1-10 CNAMEs.
  • Comment - string - Optional - A comment to apply to the distribution. Cannot exceed 128 characters.
  • DefaultRootObject - string - Optional - The file to load when someone accesses the root of your Amazon CloudFront domain (e.g., index.html).
  • Enabled - string - Optional - A value of true enables the distribution. A value of false disables it. The default value is true.
  • Logging - array - Optional - An array that contains two keys: Bucket, specifying where logs are written to, and Prefix, specifying a prefix to append to log file names.
  • OriginAccessIdentity - string - Optional - The origin access identity (OAI) associated with this distribution. Use the Identity ID from the OAI, not the CanonicalId. Requires an S3 origin.
  • OriginProtocolPolicy - string - Optional - The origin protocol policy to apply to your origin. If you specify http-only, CloudFront will use HTTP only to access the origin. If you specify match-viewer, CloudFront will fetch from your origin using HTTP or HTTPS, based on the protocol of the viewer request. It has a default value of match-viewer. [Allowed values: http-only, match-viewer]
  • Streaming - boolean - Optional - Whether or not this should be for a streaming distribution. A value of true will create a streaming distribution. A value of false will create a standard distribution. The default value is false.
  • TrustedSigners - array - Optional - An array of AWS account numbers for users who are trusted signers. Explicity add the value Self to the array to add your own account as a trusted signer.

Returns

Type

Description

string

An XML document to be used as the distribution configuration.

Examples

Generate the configuration XML document.

// Generate configuration XML
$cdn = new AmazonCloudFront();

$response = $cdn->generate_config_xml('my-bucket', 'CFdemo3');

// Success?
var_dump($response);

Generate the configuration XML document, enabling logging and using a fully-qualified domain name for Amazon S3.

// Generate configuration XML
$cdn = new AmazonCloudFront();

$response = $cdn->generate_config_xml('my-bucket.s3.amazonaws.com', 'CFdemo3', array(
	'Logging' => array(
		'Bucket' => 'my-bucket-logs',
		'Prefix' => 'log_'
	)
));

// Success?
var_dump($response);

Generate the configuration XML document, enabling streaming and multiple CNAMEs, and using a fully-qualified domain name for Amazon S3.

// Generate configuration XML
$cdn = new AmazonCloudFront();

$response = $cdn->generate_config_xml('my-bucket.s3.amazonaws.com', 'CFdemo3', array(
	'Enabled' => true,
	'Comment' => 'This is my sample comment',
	'CNAME' => array(
		'cname.example.com',
		'cname2.example.com',
		'cname3.example.com'
	),
	'Streaming' => true,
	'DefaultRootObject' => 'text/sample.txt',
	'CachingBehavior' => array(
		'MinTTL' => '30 minutes',
	),
));

// Success?
var_dump($response);

Related Methods

Source

Method defined in services/cloudfront.class.php | Toggle source view (145 lines) | View on GitHub

public function generate_config_xml($origin, $caller_reference, $opt = null)
{
    // Default, empty XML
    $xml = simplexml_load_string(sprintf($this->base_xml, (
        (isset($opt['Streaming']) && $opt['Streaming'] == (bool) true) ? 'StreamingDistributionConfig' : 'DistributionConfig')
    ));

    if (substr($origin, 0, 7) === 'http://' || substr($origin, 0, 8) === 'https://')
    {
        // Custom Origin
        $custom_origin = $xml->addChild('CustomOrigin');
        $custom_origin->addChild('DNSName', str_replace(array('http://', 'https://'), '', $origin));

        if (isset($opt['OriginProtocolPolicy']))
        {
            $custom_origin->addChild('OriginProtocolPolicy', $opt['OriginProtocolPolicy']);
        }
        else
        {
            $custom_origin->addChild('OriginProtocolPolicy', 'match-viewer');
        }
    }
    else
    {
        // S3 Origin
        $s3_origin = $xml->addChild('S3Origin');
        $s3_origin->addChild('DNSName', $origin . ((stripos($origin, '.s3.amazonaws.com') === false) ? '.s3.amazonaws.com' : ''));

        // Origin Access Identity
        if (isset($opt['OriginAccessIdentity']))
        {
            $s3_origin->addChild('OriginAccessIdentity', 'origin-access-identity/cloudfront/' . $opt['OriginAccessIdentity']);
        }
    }

    // CallerReference
    $xml->addChild('CallerReference', $caller_reference);

    // CNAME
    if (isset($opt['CNAME']))
    {
        if (is_array($opt['CNAME']))
        {
            foreach ($opt['CNAME'] as $cname)
            {
                $xml->addChild('CNAME', $cname);
            }
        }
        else
        {
            $xml->addChild('CNAME', $opt['CNAME']);
        }
    }

    // Comment
    if (isset($opt['Comment']))
    {
        $xml->addChild('Comment', $opt['Comment']);
    }

    // Enabled
    if (isset($opt['Enabled']))
    {
        $xml->addChild('Enabled', $opt['Enabled'] ? 'true' : 'false');
    }
    else
    {
        $xml->addChild('Enabled', 'true');
    }

    // Logging
    if (isset($opt['Logging']))
    {
        if (is_array($opt['Logging']))
        {
            $logging = $xml->addChild('Logging');
            $bucket_name = $opt['Logging']['Bucket'];

            // Origin
            $logging->addChild('Bucket', $bucket_name . (
                (stripos($bucket_name, '.s3.amazonaws.com') === false) ? '.s3.amazonaws.com' : ''
            ));

            $logging->addChild('Prefix', $opt['Logging']['Prefix']);
        }
    }

    // Required Protocols
    if (isset($opt['RequiredProtocols']))
    {
        $required_protocols = $xml->addChild('RequiredProtocols');
        $required_protocols->addChild('Protocol', $opt['RequiredProtocols']);
    }

    // Caching Behavior
    if (isset($opt['CachingBehavior']) && is_array($opt['CachingBehavior']))
    {
        $caching_behavior = $xml->addChild('CachingBehavior');

        if (isset($opt['CachingBehavior']['MinTTL']))
        {
            $min_ttl = $opt['CachingBehavior']['MinTTL'];

            if (is_string($min_ttl))
            {
                $min_ttl = strtotime($min_ttl);
            }

            $caching_behavior->addChild('MinTTL', $min_ttl);
        }
    }

    // Trusted Signers
    if (isset($opt['TrustedSigners']))
    {
        $trusted_signers = $xml->addChild('TrustedSigners');

        // Not an array? Convert to one.
        if (!is_array($opt['TrustedSigners']))
        {
            $opt['TrustedSigners'] = array($opt['TrustedSigners']);
        }

        // Handle 'Self' vs. everything else
        foreach ($opt['TrustedSigners'] as $signer)
        {
            if (strtolower($signer) === 'self')
            {
                $trusted_signers->addChild('Self');
            }
            else
            {
                $trusted_signers->addChild('AwsAccountNumber', $signer);
            }
        }
    }

    // DefaultRootObject
    if (isset($opt['DefaultRootObject']))
    {
        $xml->addChild('DefaultRootObject', $opt['DefaultRootObject']);
    }

    return $xml->asXML();
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback