Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support for CloudFront and updates to setBucketLogging()

  • Loading branch information...
commit ed0ef403c09ae1624bcb639928e0c8f8aad618c1 1 parent 15c3383
@tpyo authored
Showing with 374 additions and 135 deletions.
  1. +0 −100 README.txt
  2. +288 −32 S3.php
  3. +80 −0 example-cloudfront.php
  4. +6 −3 example.php
View
100 README.txt
@@ -1,100 +0,0 @@
-AMAZON S3 PHP CLASS
-
-
-USING THE CLASS
-
-
-OO method (e,g; $s3->getObject(...)):
-$s3 = new S3(awsAccessKey, awsSecretKey);
-
-Statically (e,g; S3::getObject(...)):
-S3::setAuth(awsAccessKey, awsSecretKey);
-
-
-
-OBJECTS
-
-
-Put an object from a string:
- $s3->putObject($string, $bucketName, $uploadName, S3::ACL_PUBLIC_READ)
- Legacy function: $s3->putObjectString($string, $bucketName, $uploadName, S3::ACL_PUBLIC_READ)
-
-
-Put an object from a file:
- $s3->putObject($s3->inputFile($file, false), $bucketName, $uploadName, S3::ACL_PUBLIC_READ)
- Legacy function: $s3->putObjectFile($uploadFile, $bucketName, $uploadName, S3::ACL_PUBLIC_READ)
-
-
-Put an object from a resource (buffer/file size is required):
- Please note: the resource will be fclose()'d automatically
- $s3->putObject($s3->inputResource(fopen($file, 'rb'), filesize($file)), $bucketName, $uploadName, S3::ACL_PUBLIC_READ)
-
-
-Get an object:
- $s3->getObject($bucketName, $uploadName)
-
-
-Save an object to file:
- $s3->getObject($bucketName, $uploadName, $saveName)
-
-
-Save an object to a resource of any type:
- $s3->getObject($bucketName, $uploadName, fopen('savefile.txt', 'wb'))
-
-
-Copy an object:
- $s3->copyObject($srcBucket, $srcName, $bucketName, $saveName)
-
-
-Delete an object:
- $s3->deleteObject($bucketName, $uploadName)
-
-
-
-BUCKETS
-
-
-Get a list of buckets:
- $s3->listBuckets() // Simple bucket list
- $s3->listBuckets(true) // Detailed bucket list
-
-
-Create a public-read bucket:
- $s3->putBucket($bucketName, S3::ACL_PUBLIC_READ)
- $s3->putBucket($bucketName, S3::ACL_PUBLIC_READ, 'EU') // EU-hosted bucket
-
-
-Get the contents of a bucket:
- $s3->getBucket($bucketName)
-
-
-Get a bucket's location:
- $s3->getBucketLocation($bucketName)
-
-
-Delete a bucket:
- $s3->deleteBucket($bucketName)
-
-
-
-
-KNOWN ISSUES
-
- Files larger than 2GB are not supported on 32 bit systems due to PHP’s signed integer problem
-
-
-
-MORE INFORMATION
-
-
- Project URL (please report bugs here):
- http://undesigned.org.za/2007/10/22/amazon-s3-php-class
-
-
- Amazon S3 documentation:
- http://docs.amazonwebservices.com/AmazonS3/2006-03-01/
-
-
-
-
-EOF!
View
320 S3.php
@@ -32,7 +32,7 @@
* Amazon S3 PHP class
*
* @link http://undesigned.org.za/2007/10/22/amazon-s3-php-class
-* @version 0.3.6
+* @version 0.3.7
*/
class S3 {
// ACL flags
@@ -158,8 +158,9 @@ public static function getBucket($bucket, $prefix = null, $marker = null, $maxKe
if ($maxKeys == null && $lastMarker !== null && (string)$response->body->IsTruncated == 'true')
do {
$rest = new S3Request('GET', $bucket, '');
- if ($prefix !== null) $rest->setParameter('prefix', $prefix);
+ if ($prefix !== null && $prefix !== '') $rest->setParameter('prefix', $prefix);
$rest->setParameter('marker', $lastMarker);
+ if ($delimiter !== null && $delimiter !== '') $rest->setParameter('delimiter', $delimiter);
if (($response = $rest->getResponse(true)) == false || $response->code !== 200) break;
if (isset($response->body, $response->body->Contents))
@@ -391,8 +392,10 @@ public static function getObject($bucket, $uri, $saveTo = false) {
if (is_resource($saveTo))
$rest->fp =& $saveTo;
else
- if (($rest->fp = @fopen($saveTo, 'wb')) == false)
- $rest->response->error = array('code' => 0, 'message' => 'Unable to open save file for writing: '.$saveTo);
+ if (($rest->fp = @fopen($saveTo, 'wb')) !== false)
+ $rest->file = realpath($saveTo);
+ else
+ $rest->response->error = array('code' => 0, 'message' => 'Unable to open save file for writing: '.$saveTo);
}
if ($rest->response->error === false) $rest->getResponse();
@@ -403,7 +406,6 @@ public static function getObject($bucket, $uri, $saveTo = false) {
$rest->response->error['code'], $rest->response->error['message']), E_USER_WARNING);
return false;
}
- $rest->file = realpath($saveTo);
return $rest->response;
}
@@ -467,19 +469,37 @@ public static function copyObject($srcBucket, $srcUri, $bucket, $uri, $acl = sel
* @param string $targetPrefix Log prefix (e,g; domain.com-)
* @return boolean
*/
- public static function setBucketLogging($bucket, $targetBucket, $targetPrefix) {
+ public static function setBucketLogging($bucket, $targetBucket, $targetPrefix = null) {
+ // The S3 log delivery group has to be added to the target bucket's ACP
+ if ($targetBucket !== null && ($acp = self::getAccessControlPolicy($targetBucket, '')) !== false) {
+ // Only add permissions to the target bucket when they do not exist
+ $aclWriteSet = false;
+ $aclReadSet = false;
+ foreach ($acp['acl'] as $acl)
+ if ($acl['type'] == 'Group' && $acl['uri'] == 'http://acs.amazonaws.com/groups/s3/LogDelivery') {
+ if ($acl['permission'] == 'WRITE') $aclWriteSet = true;
+ elseif ($acl['permission'] == 'READ_ACP') $aclReadSet = true;
+ }
+ if (!$aclWriteSet) $acp['acl'][] = array(
+ 'type' => 'Group', 'uri' => 'http://acs.amazonaws.com/groups/s3/LogDelivery', 'permission' => 'WRITE'
+ );
+ if (!$aclReadSet) $acp['acl'][] = array(
+ 'type' => 'Group', 'uri' => 'http://acs.amazonaws.com/groups/s3/LogDelivery', 'permission' => 'READ_ACP'
+ );
+ if (!$aclReadSet || !$aclWriteSet) self::setAccessControlPolicy($targetBucket, '', $acp);
+ }
+
$dom = new DOMDocument;
$bucketLoggingStatus = $dom->createElement('BucketLoggingStatus');
$bucketLoggingStatus->setAttribute('xmlns', 'http://s3.amazonaws.com/doc/2006-03-01/');
-
- $loggingEnabled = $dom->createElement('LoggingEnabled');
-
- $loggingEnabled->appendChild($dom->createElement('TargetBucket', $targetBucket));
- $loggingEnabled->appendChild($dom->createElement('TargetPrefix', $targetPrefix));
-
- // TODO: Add TargetGrants
-
- $bucketLoggingStatus->appendChild($loggingEnabled);
+ if ($targetBucket !== null) {
+ if ($targetPrefix == null) $targetPrefix = $bucket . '-';
+ $loggingEnabled = $dom->createElement('LoggingEnabled');
+ $loggingEnabled->appendChild($dom->createElement('TargetBucket', $targetBucket));
+ $loggingEnabled->appendChild($dom->createElement('TargetPrefix', $targetPrefix));
+ // TODO: Add TargetGrants?
+ $bucketLoggingStatus->appendChild($loggingEnabled);
+ }
$dom->appendChild($bucketLoggingStatus);
$rest = new S3Request('PUT', $bucket, '');
@@ -528,6 +548,17 @@ public static function getBucketLogging($bucket) {
/**
+ * Disable bucket logging
+ *
+ * @param string $bucket Bucket name
+ * @return boolean
+ */
+ public static function disableBucketLogging($bucket) {
+ return self::setBucketLogging($bucket, null);
+ }
+
+
+ /**
* Get a bucket's location
*
* @param string $bucket Bucket name
@@ -610,8 +641,6 @@ public static function setAccessControlPolicy($bucket, $uri = '', $acp = array()
/**
* Get object or bucket Access Control Policy
*
- * Currently this will trigger an error if there is no ACL on an object (will fix soon)
- *
* @param string $bucket Bucket name
* @param string $uri Object URI
* @return mixed | false
@@ -670,7 +699,7 @@ public static function getAccessControlPolicy($bucket, $uri = '') {
*
* @param string $bucket Bucket name
* @param string $uri Object URI
- * @return mixed
+ * @return boolean
*/
public static function deleteObject($bucket, $uri) {
$rest = new S3Request('DELETE', $bucket, $uri);
@@ -703,6 +732,231 @@ public static function getAuthenticatedURL($bucket, $uri, $lifetime, $hostBucket
/**
+ * Create a CloudFront distribution
+ *
+ * @param string $bucket Bucket name
+ * @param boolean $enabled Enabled (true/false)
+ * @param array $cnames Array containing CNAME aliases
+ * @param string $comment Use the bucket name as the hostname
+ * @return array | false
+ */
+ public static function createDistribution($bucket, $enabled = true, $cnames = array(), $comment = '') {
+ self::$useSSL = true; // CloudFront requires SSL
+ $rest = new S3Request('POST', '', '2008-06-30/distribution', 'cloudfront.amazonaws.com');
+ $rest->data = self::__getCloudFrontDistributionConfigXML($bucket.'.s3.amazonaws.com', $enabled, $comment, (string)microtime(true), $cnames);
+ $rest->size = strlen($rest->data);
+ $rest->setHeader('Content-Type', 'application/xml');
+ $rest = self::__getCloudFrontResponse($rest);
+
+ if ($rest->error === false && $rest->code !== 201)
+ $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
+ if ($rest->error !== false) {
+ trigger_error(sprintf("CloudFront::createDistribution({$bucket}, ".(int)$enabled.", '$comment'): [%s] %s",
+ $rest->error['code'], $rest->error['message']), E_USER_WARNING);
+ return false;
+ } elseif ($rest->body instanceof SimpleXMLElement)
+ return self::__parseCloudFrontDistributionConfig($rest->body);
+ return false;
+ }
+
+
+ /**
+ * Get CloudFront distribution info
+ *
+ * @param string $distributionId Distribution ID from listDistributions()
+ * @return array | false
+ */
+ public static function getDistribution($distributionId) {
+ S3::$useSSL = true; // CloudFront requires SSL
+ $rest = new S3Request('GET', '', '2008-06-30/distribution/'.$distributionId, 'cloudfront.amazonaws.com');
+ $rest = self::__getCloudFrontResponse($rest);
+
+ if ($rest->error === false && $rest->code !== 200)
+ $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
+ if ($rest->error !== false) {
+ trigger_error(sprintf("CloudFront::getDistribution($distributionId): [%s] %s",
+ $rest->error['code'], $rest->error['message']), E_USER_WARNING);
+ return false;
+ } elseif ($rest->body instanceof SimpleXMLElement) {
+ $dist = self::__parseCloudFrontDistributionConfig($rest->body);
+ $dist['hash'] = $rest->headers['hash'];
+ return $dist;
+ }
+ return false;
+ }
+
+
+ /**
+ * Update a CloudFront distribution
+ *
+ * @param array $dist Distribution array info identical to output of getDistribution()
+ * @return array | false
+ */
+ public static function updateDistribution($dist) {
+ S3::$useSSL = true; // CloudFront requires SSL
+ $rest = new S3Request('PUT', '', '2008-06-30/distribution/'.$dist['id'].'/config', 'cloudfront.amazonaws.com');
+ $rest->data = self::__getCloudFrontDistributionConfigXML($dist['origin'], $dist['enabled'], $dist['comment'], $dist['callerReference'], $dist['cnames']);
+ $rest->size = strlen($rest->data);
+ $rest->setHeader('If-Match', $dist['hash']);
+ $rest = self::__getCloudFrontResponse($rest);
+
+ if ($rest->error === false && $rest->code !== 200)
+ $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
+ if ($rest->error !== false) {
+ trigger_error(sprintf("CloudFront::updateDistribution({$dist['id']}, ".(int)$enabled.", '$comment'): [%s] %s",
+ $rest->error['code'], $rest->error['message']), E_USER_WARNING);
+ return false;
+ } else {
+ $dist = self::__parseCloudFrontDistributionConfig($rest->body);
+ $dist['hash'] = $rest->headers['hash'];
+ return $dist;
+ }
+ return false;
+ }
+
+
+ /**
+ * Delete a CloudFront distribution
+ *
+ * @param array $dist Distribution array info identical to output of getDistribution()
+ * @return boolean
+ */
+ public static function deleteDistribution($dist) {
+ S3::$useSSL = true; // CloudFront requires SSL
+ $rest = new S3Request('DELETE', '', '2008-06-30/distribution/'.$dist['id'], 'cloudfront.amazonaws.com');
+ $rest->setHeader('If-Match', $dist['hash']);
+ $rest = self::__getCloudFrontResponse($rest);
+
+ if ($rest->error === false && $rest->code !== 204)
+ $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
+ if ($rest->error !== false) {
+ trigger_error(sprintf("CloudFront::deleteDistribution({$dist['id']}): [%s] %s",
+ $rest->error['code'], $rest->error['message']), E_USER_WARNING);
+ return false;
+ }
+ return true;
+ }
+
+
+ /**
+ * Get a list of CloudFront distributions
+ *
+ * @return array
+ */
+ public static function listDistributions() {
+ S3::$useSSL = true; // CloudFront requires SSL
+ $rest = new S3Request('GET', '', '2008-06-30/distribution', 'cloudfront.amazonaws.com');
+ $rest = self::__getCloudFrontResponse($rest);
+
+ if ($rest->error === false && $rest->code !== 200)
+ $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
+ if ($rest->error !== false) {
+ trigger_error(sprintf("CloudFront::listDistributions(): [%s] %s",
+ $rest->error['code'], $rest->error['message']), E_USER_WARNING);
+ return false;
+ } elseif ($rest->body instanceof SimpleXMLElement && isset($rest->body->DistributionSummary)) {
+ $list = array();
+ if (isset($rest->body->Marker, $rest->body->MaxItems, $rest->body->IsTruncated)) {
+ //$info['marker'] = (string)$rest->body->Marker;
+ //$info['maxItems'] = (int)$rest->body->MaxItems;
+ //$info['isTruncated'] = (string)$rest->body->IsTruncated == 'true' ? true : false;
+ }
+ foreach ($rest->body->DistributionSummary as $summary) {
+ $list[(string)$summary->Id] = self::__parseCloudFrontDistributionConfig($summary);
+ }
+ return $list;
+ }
+ return array();
+ }
+
+
+ /**
+ * Get a DistributionConfig DOMDocument
+ *
+ * @internal Used to create XML in createDistribution() and updateDistribution()
+ * @param string $bucket Origin bucket
+ * @param boolean $enabled Enabled (true/false)
+ * @param string $comment Comment to append
+ * @param string $callerReference Caller reference
+ * @param array $cnames Array of CNAME aliases
+ * @return string
+ */
+ private static function __getCloudFrontDistributionConfigXML($bucket, $enabled, $comment, $callerReference = '0', $cnames = array()) {
+ $dom = new DOMDocument('1.0', 'UTF-8');
+ $dom->formatOutput = true;
+ $distributionConfig = $dom->createElement('DistributionConfig');
+ $distributionConfig->setAttribute('xmlns', 'http://cloudfront.amazonaws.com/doc/2008-06-30/');
+ $distributionConfig->appendChild($dom->createElement('Origin', $bucket));
+ $distributionConfig->appendChild($dom->createElement('CallerReference', $callerReference));
+ foreach ($cnames as $cname)
+ $distributionConfig->appendChild($dom->createElement('CNAME', $cname));
+ if ($comment !== '') $distributionConfig->appendChild($dom->createElement('Comment', $comment));
+ $distributionConfig->appendChild($dom->createElement('Enabled', $enabled ? 'true' : 'false'));
+ $dom->appendChild($distributionConfig);
+ return $dom->saveXML();
+ }
+
+
+ /**
+ * Parse a CloudFront distribution config
+ *
+ * @internal Used to parse the CloudFront DistributionConfig node to an array
+ * @param object &$node DOMNode
+ * @return array
+ */
+ private static function __parseCloudFrontDistributionConfig(&$node) {
+ $dist = array();
+ if (isset($node->Id, $node->Status, $node->LastModifiedTime, $node->DomainName)) {
+ $dist['id'] = (string)$node->Id;
+ $dist['status'] = (string)$node->Status;
+ $dist['time'] = strtotime((string)$node->LastModifiedTime);
+ $dist['domain'] = (string)$node->DomainName;
+ }
+ if (isset($node->CallerReference))
+ $dist['callerReference'] = (string)$node->CallerReference;
+ if (isset($node->Comment))
+ $dist['comment'] = (string)$node->Comment;
+ if (isset($node->Enabled, $node->Origin)) {
+ $dist['origin'] = (string)$node->Origin;
+ $dist['enabled'] = (string)$node->Enabled == 'true' ? true : false;
+ } elseif (isset($node->DistributionConfig)) {
+ $dist = array_merge($dist, self::__parseCloudFrontDistributionConfig($node->DistributionConfig));
+ }
+ if (isset($node->CNAME)) {
+ $dist['cnames'] = array();
+ foreach ($node->CNAME as $cname) $dist['cnames'][(string)$cname] = (string)$cname;
+ }
+ return $dist;
+ }
+
+
+ /**
+ * Grab CloudFront response
+ *
+ * @internal Used to parse the CloudFront S3Request::getResponse() output
+ * @param object &$rest S3Request instance
+ * @return object
+ */
+ private static function __getCloudFrontResponse(&$rest) {
+ $rest->getResponse();
+ if ($rest->response->error === false && isset($rest->response->body) &&
+ is_string($rest->response->body) && substr($rest->response->body, 0, 5) == '<?xml') {
+ $rest->response->body = simplexml_load_string($rest->response->body);
+ // Grab CloudFront errors
+ if (isset($rest->response->body->Error, $rest->response->body->Error->Code,
+ $rest->response->body->Error->Message)) {
+ $rest->response->error = array(
+ 'code' => (string)$rest->response->body->Error->Code,
+ 'message' => (string)$rest->response->body->Error->Message
+ );
+ unset($rest->response->body);
+ }
+ }
+ return $rest->response;
+ }
+
+
+ /**
* Get MIME type for file
*
* @internal Used to get mime types
@@ -739,6 +993,7 @@ public static function __getMimeType(&$file) {
'tar' => 'application/x-tar', 'bz' => 'application/x-bzip',
'bz2' => 'application/x-bzip2', 'txt' => 'text/plain',
'asc' => 'text/plain', 'htm' => 'text/html', 'html' => 'text/html',
+ 'css' => 'text/css', 'js' => 'text/javascript',
'xml' => 'text/xml', 'xsl' => 'application/xsl+xml',
'ogg' => 'application/ogg', 'mp3' => 'audio/mpeg', 'wav' => 'audio/x-wav',
'avi' => 'video/x-msvideo', 'mpg' => 'video/mpeg', 'mpeg' => 'video/mpeg',
@@ -796,17 +1051,18 @@ private static function __getHash($string) {
* @param string $uri Object URI
* @return mixed
*/
- function __construct($verb, $bucket = '', $uri = '') {
+ function __construct($verb, $bucket = '', $uri = '', $defaultHost = 's3.amazonaws.com') {
$this->verb = $verb;
$this->bucket = strtolower($bucket);
$this->uri = $uri !== '' ? '/'.$uri : '/';
if ($this->bucket !== '') {
+ $this->headers['Host'] = $this->bucket.'.'.$defaultHost;
$this->resource = '/'.$this->bucket.$this->uri;
- $this->headers['Host'] = $this->bucket.'.s3.amazonaws.com';
} else {
- $this->headers['Host'] = 's3.amazonaws.com';
- $this->resource = strlen($this->uri) > 1 ? '/'.$this->bucket.$this->uri : $this->uri;
+ $this->headers['Host'] = $defaultHost;
+ //$this->resource = strlen($this->uri) > 1 ? '/'.$this->bucket.$this->uri : $this->uri;
+ $this->resource = $this->uri;
}
$this->headers['Date'] = gmdate('D, d M Y H:i:s T');
@@ -885,6 +1141,7 @@ public function getResponse() {
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
}
+
curl_setopt($curl, CURLOPT_URL, $url);
// Headers
@@ -898,18 +1155,17 @@ public function getResponse() {
foreach ($this->amzHeaders as $header => $value)
if (strlen($value) > 0) $amz[] = strToLower($header).':'.$value;
- // AMZ headers must be sorted (thanks Malone)
+ // AMZ headers must be sorted
if (sizeof($amz) > 0) {
sort($amz);
$amz = "\n".implode("\n", $amz);
} else $amz = '';
- // Authorization string
+ // Authorization string (CloudFront stringToSign should only contain a date)
$headers[] = 'Authorization: ' . S3::__getSignature(
- $this->verb."\n".
- $this->headers['Content-MD5']."\n".
- $this->headers['Content-Type']."\n".
- $this->headers['Date'].$amz."\n".$this->resource
+ $this->headers['Host'] == 'cloudfront.amazonaws.com' ? $this->headers['Date'] :
+ $this->verb."\n".$this->headers['Content-MD5']."\n".
+ $this->headers['Content-Type']."\n".$this->headers['Date'].$amz."\n".$this->resource
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
@@ -922,19 +1178,19 @@ public function getResponse() {
// Request types
switch ($this->verb) {
case 'GET': break;
- case 'PUT':
+ case 'PUT': case 'POST': // POST only used for CloudFront
if ($this->fp !== false) {
curl_setopt($curl, CURLOPT_PUT, true);
curl_setopt($curl, CURLOPT_INFILE, $this->fp);
if ($this->size > 0)
curl_setopt($curl, CURLOPT_INFILESIZE, $this->size);
} elseif ($this->data !== false) {
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
+ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb);
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data);
if ($this->size > 0)
curl_setopt($curl, CURLOPT_BUFFERSIZE, $this->size);
} else
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
+ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb);
break;
case 'HEAD':
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
@@ -1019,7 +1275,7 @@ private function __responseHeaderCallback(&$curl, &$data) {
elseif ($header == 'Content-Type')
$this->response->headers['type'] = $value;
elseif ($header == 'ETag')
- $this->response->headers['hash'] = substr($value, 1, -1);
+ $this->response->headers['hash'] = $value{0} == '"' ? substr($value, 1, -1) : $value;
elseif (preg_match('/^x-amz-meta-.*$/', $header))
$this->response->headers[$header] = is_numeric($value) ? (int)$value : $value;
}
View
80 example-cloudfront.php
@@ -0,0 +1,80 @@
+#!/usr/local/bin/php
+<?php
+/**
+* $Id$
+*
+* S3 class - CloudFront usage
+*/
+
+if (!class_exists('S3')) require_once 'S3.php';
+
+// AWS access info
+if (!defined('awsAccessKey')) define('awsAccessKey', 'change-this');
+if (!defined('awsSecretKey')) define('awsSecretKey', 'change-this');
+
+
+// Check for CURL
+if (!extension_loaded('curl') && !@dl(PHP_SHLIB_SUFFIX == 'so' ? 'curl.so' : 'php_curl.dll'))
+ exit("\nERROR: CURL extension not loaded\n\n");
+
+// Pointless without your keys!
+if (awsAccessKey == 'change-this' || awsSecretKey == 'change-this')
+ exit("\nERROR: AWS access information required\n\nPlease edit the following lines in this file:\n\n".
+ "define('awsAccessKey', 'change-me');\ndefine('awsSecretKey', 'change-me');\n\n");
+
+
+S3::setAuth(awsAccessKey, awsSecretKey);
+
+
+function test_createDistribution($bucket, $cnames = array()) {
+ if (($dist = S3::createDistribution($bucket, true, $cnames, 'New distribution created')) !== false) {
+ echo "createDistribution($bucket): "; var_dump($dist);
+ } else {
+ echo "createDistribution($bucket): Failed to create distribution\n";
+ }
+}
+
+function test_listDistributions() {
+ if (($dists = S3::listDistributions()) !== false) {
+ if (sizeof($dists) == 0) echo "listDistributions(): No distributions\n";
+ foreach ($dists as $dist) {
+ var_dump($dist);
+ }
+ } else {
+ echo "listDistributions(): Failed to get distribution list\n";
+ }
+}
+
+function test_updateDistribution($distributionId, $enabled = false, $cnames = array()) {
+ // To enable/disable a distribution configuration:
+ if (($dist = S3::getDistribution($distributionId)) !== false) {
+ $dist['enabled'] = $enabled;
+ $dist['comment'] = $enabled ? 'Enabled' : 'Disabled';
+ if (!isset($dist['cnames'])) $dist['cnames'] = array();
+ foreach ($cnames as $cname) $dist['cnames'][$cname] = $cname;
+
+ echo "updateDistribution($distributionId): "; var_dump(S3::updateDistribution($dist));
+ } else {
+ echo "getDistribution($distributionId): Failed to get distribution information for update\n";
+ }
+}
+
+function test_deleteDistribution($distributionId) {
+ // To delete a distribution configuration you must first set enable=false with
+ // the updateDistrubution() method and wait for status=Deployed:
+ if (($dist = S3::getDistribution($distributionId)) !== false) {
+ if ($dist['status'] == 'Deployed') {
+ echo "deleteDistribution($distributionId): "; var_dump(S3::deleteDistribution($dist));
+ } else {
+ echo "deleteDistribution($distributionId): Distribution not ready for deletion (status is not 'Deployed')\n";
+ var_dump($dist);
+ }
+ }
+}
+
+
+//test_createDistribution($bucketName, array('my-optional-cname-alias.com'));
+//test_listDistributions();
+// "E4S5USZY109S8" is the distribution ID:
+//test_updateDistribution('E4S5USZY109S8', false);
+//test_deleteDistribution('E4S5USZY109S8');
View
9 example.php
@@ -73,14 +73,14 @@
// $acp = $s3->getAccessControlPolicy($bucketName);
// echo "S3::getAccessControlPolicy(): {$bucketName}: ".print_r($acp, 1);
- // Update an access control policy ($acp should be the data returned by S3::getAccessControlPolicy())
+ // Update an access control policy ($acp should be the same as the data returned by S3::getAccessControlPolicy())
// $s3->setAccessControlPolicy($bucketName, '', $acp);
// $acp = $s3->getAccessControlPolicy($bucketName);
// echo "S3::getAccessControlPolicy(): {$bucketName}: ".print_r($acp, 1);
- // Enable logging for a bucket (remember, logbucket must be writable by the log group too):
- // $s3->enableBucketLogging($bucketName, 'logbucket', 'prefix');
+ // Enable logging for a bucket:
+ // $s3->setBucketLogging($bucketName, 'logbucket', 'prefix');
// if (($logging = $s3->getBucketLogging($bucketName)) !== false) {
// echo "S3::getBucketLogging(): Logging for {$bucketName}: ".print_r($contents, 1);
@@ -88,6 +88,9 @@
// echo "S3::getBucketLogging(): Logging for {$bucketName} not enabled\n";
// }
+ // Disable bucket logging:
+ // var_dump($s3->disableBucketLogging($bucketName));
+
// Delete our file
if ($s3->deleteObject($bucketName, baseName($uploadFile))) {
Please sign in to comment.
Something went wrong with that request. Please try again.