Search

Feedback

Follow Us



Recent Blog Posts

Amazon CloudFront (2012-05-05)

This guide focuses on the AWS SDK for PHP client for Amazon CloudFront. This guide assumes that you have already downloaded and installed the AWS SDK for PHP. See Installation for more information on getting started.

Note: This guide is for the 2012-05-05 API version of Amazon CloudFront. You may also be interested in the guide for the latest API version of Amazon CloudFront.

Creating a client

First you need to create a client object using one of the following techniques.

Factory method

The easiest way to get up and running quickly is to use the Aws\CloudFront\CloudFrontClient::factory() method and provide your credential profile (via the profile option), which identifies the set of credentials you want to use from your ~/.aws/credentials file (see Using the AWS credentials file and credential profiles).

use Aws\CloudFront\CloudFrontClient;

$client = CloudFrontClient::factory(array(
    'profile' => '<profile in your aws credentials file>',
    'version' => '2012-05-05'
));

You can provide your credential profile like in the preceding example, specify your access keys directly (via key and secret), or you can choose to omit any credential information if you are using AWS Identity and Access Management (IAM) roles for EC2 instances or credentials sourced from the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.

Note

The profile option and AWS credential file support is only available for version 2.6.1 of the SDK and higher. We recommend that all users update their copies of the SDK to take advantage of this feature, which is a safer way to specify credentials than explicitly providing key and secret.

Service builder

A more robust way to connect to Amazon CloudFront is through the service builder. This allows you to specify credentials and other configuration settings in a configuration file. These settings can then be shared across all clients so that you only have to specify your settings once.

use Aws\Common\Aws;

// Create a service builder using a configuration file
$aws = Aws::factory('/path/to/my_config.json');

// Get the client from the builder by namespace
$client = $aws->get('cloudfront_20120505');

For more information about configuration files, see Configuring the SDK.

Signing CloudFront URLs for Private Distributions

Signed URLs allow you to provide users access to your private content. A signed URL includes additional information (e.g., expiration time) that gives you more control over access to your content. This additional information appears in a policy statement, which is based on either a canned policy or a custom policy. For information about how to set up private distributions and why you need to sign URLs, please read the Serving Private Content through CloudFront section of the CloudFront Developer Guide.

You can sign a URL using the CloudFront client in the SDK. First you must make sure to provide your CloudFront Private Key and Key Pair ID to the CloudFront client.

<?php

$cloudFront = CloudFrontClient::factory(array(
    'private_key' => '/path/to/your/cloudfront-private-key.pem',
    'key_pair_id' => '<cloudfront key pair id>',
));

You can alternatively specify the Private Key and Key Pair ID in your AWS config file and use the service builder to instantiate the CloudFront client. The following is an example config file that specifies the CloudFront key information.

<?php return array(
    'includes' => array('_aws'),
    'services' => array(
        'default_settings' => array(
            'params' => array(
                'region' => 'us-west-2'
            )
        ),
        'cloudfront' => array(
            'extends' => 'cloudfront',
            'params'  => array(
                'private_key' => '/path/to/your/cloudfront-private-key.pem',
                'key_pair_id' => '<cloudfront key pair id>'
            )
        )
    )
);

You can sign a CloudFront URL for a video resource using either a canned or custom policy.

// Setup parameter values for the resource
$streamHostUrl = 'rtmp://example-distribution.cloudfront.net';
$resourceKey = 'videos/example.mp4';
$expires = time() + 300;

// Create a signed URL for the resource using the canned policy
$signedUrlCannedPolicy = $cloudFront->getSignedUrl(array(
    'url'     => $streamHostUrl . '/' . $resourceKey,
    'expires' => $expires,
));

For versions of the SDK later than 2.3.1, instead of providing your private key information when you instantiate the client, you can provide it at the time when you sign the URL.

$signedUrlCannedPolicy = $cloudFront->getSignedUrl(array(
    'url'         => $streamHostUrl . '/' . $resourceKey,
    'expires'     => $expires,
    'private_key' => '/path/to/your/cloudfront-private-key.pem',
    'key_pair_id' => '<cloudfront key pair id>'
));

To use a custom policy, provide the policy key instead of expires.

$customPolicy = <<<POLICY
{
    "Statement": [
        {
            "Resource": "{$resourceKey}",
            "Condition": {
                "IpAddress": {"AWS:SourceIp": "{$_SERVER['REMOTE_ADDR']}/32"},
                "DateLessThan": {"AWS:EpochTime": {$expires}}
            }
        }
    ]
}
POLICY;

// Create a signed URL for the resource using a custom policy
$signedUrlCustomPolicy = $cloudFront->getSignedUrl(array(
    'url'    => $streamHostUrl . '/' . $resourceKey,
    'policy' => $customPolicy,
));

The form of the signed URL is actually different depending on if the URL you are signing is using the "http" or "rtmp" scheme. In the case of "http", the full, absolute URL is returned. For "rtmp", only the relative URL is returned for your convenience, because some players require the host and path to be provided as separate parameters.

The following is an example of how you could use the signed URL to construct a web page displaying a video using JWPlayer. The same type of technique would apply to other players like FlowPlayer, but will require different client-side code.

<html>
<head>
    <title>Amazon CloudFront Streaming Example</title>
    <script type="text/javascript" src="https://example.com/jwplayer.js"></script>
</head>
<body>
    <div id="video">The canned policy video will be here.</div>
    <script type="text/javascript">
        jwplayer('video').setup({
            file: "<?= $streamHostUrl ?>/cfx/st/<?= $signedUrlCannedPolicy ?>",
            width: "720",
            height: "480"
        });
    </script>
</body>
</html>