forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateDistributionS3.php
More file actions
125 lines (112 loc) · 3.8 KB
/
Copy pathCreateDistributionS3.php
File metadata and controls
125 lines (112 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[cloudfront.php.creates3distribution.complete]
// snippet-start:[cloudfront.php.creates3distribution.import]
require 'vendor/autoload.php';
use Aws\Exception\AwsException;
// snippet-end:[cloudfront.php.creates3distribution.import]
/* ////////////////////////////////////////////////////////////////////////////
* Purpose: Creates a distribution in Amazon CloudFront.
*
* Inputs:
* - $cloudFrontClient: An initialized AWS SDK for PHP SDK client
* for CloudFront.
* - $distribution: A collection of settings for the distribution to
* be created.
*
* Returns: Information about the distribution that was created;
* otherwise, the error message.
* ///////////////////////////////////////////////////////////////////////// */
// snippet-start:[cloudfront.php.creates3distribution.main]
function createS3Distribution($cloudFrontClient, $distribution)
{
try {
$result = $cloudFrontClient->createDistribution([
'DistributionConfig' => $distribution
]);
$message = '';
if (isset($result['Distribution']['Id'])) {
$message = 'Distribution created with the ID of ' .
$result['Distribution']['Id'];
}
$message .= ' and an effective URI of ' .
$result['@metadata']['effectiveUri'] . '.';
return $message;
} catch (AwsException $e) {
return 'Error: ' . $e['message'];
}
}
function createsTheS3Distribution()
{
$originName = 'my-unique-origin-name';
$s3BucketURL = 'amzn-s3-demo-bucket.s3.amazonaws.com';
$callerReference = 'my-unique-caller-reference';
$comment = 'my-comment-about-this-distribution';
$defaultCacheBehavior = [
'AllowedMethods' => [
'CachedMethods' => [
'Items' => ['HEAD', 'GET'],
'Quantity' => 2
],
'Items' => ['HEAD', 'GET'],
'Quantity' => 2
],
'Compress' => false,
'DefaultTTL' => 0,
'FieldLevelEncryptionId' => '',
'ForwardedValues' => [
'Cookies' => [
'Forward' => 'none'
],
'Headers' => [
'Quantity' => 0
],
'QueryString' => false,
'QueryStringCacheKeys' => [
'Quantity' => 0
]
],
'LambdaFunctionAssociations' => ['Quantity' => 0],
'MaxTTL' => 0,
'MinTTL' => 0,
'SmoothStreaming' => false,
'TargetOriginId' => $originName,
'TrustedSigners' => [
'Enabled' => false,
'Quantity' => 0
],
'ViewerProtocolPolicy' => 'allow-all'
];
$enabled = false;
$origin = [
'Items' => [
[
'DomainName' => $s3BucketURL,
'Id' => $originName,
'OriginPath' => '',
'CustomHeaders' => ['Quantity' => 0],
'S3OriginConfig' => ['OriginAccessIdentity' => '']
]
],
'Quantity' => 1
];
$distribution = [
'CallerReference' => $callerReference,
'Comment' => $comment,
'DefaultCacheBehavior' => $defaultCacheBehavior,
'Enabled' => $enabled,
'Origins' => $origin
];
$cloudFrontClient = new Aws\CloudFront\CloudFrontClient([
'profile' => 'default',
'version' => '2018-06-18',
'region' => 'us-east-1'
]);
echo createS3Distribution($cloudFrontClient, $distribution);
}
// Uncomment the following line to run this code in an AWS account.
// createsTheS3Distribution();
// snippet-end:[cloudfront.php.creates3distribution.main]
// snippet-end:[cloudfront.php.creates3distribution.complete]
// snippet-sourceauthor:[pccornel (AWS)]