Skip to content

Commit

Permalink
Merge pull request tpyo#40 from daveajones/master
Browse files Browse the repository at this point in the history
New function: setBucketRedirect()
  • Loading branch information
tpyo committed Mar 19, 2013
2 parents 5677037 + 458b558 commit 66c2eae
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions S3.php
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,47 @@ public static function copyObject($srcBucket, $srcUri, $bucket, $uri, $acl = sel
}


/**
* Set up a bucket redirection
*
* @param string $bucket Bucket name
* @param string $location Target host name
* @return boolean
*/
public static function setBucketRedirect($bucket = NULL, $location = NULL)
{
$rest = new S3Request('PUT', $bucket, '', self::$endpoint);

if( empty($bucket) || empty($location) ) {
self::__triggerError("S3::setBucketRedirect({$bucket}, {$location}): Empty parameter.", __FILE__, __LINE__);
return false;
}

$dom = new DOMDocument;
$websiteConfiguration = $dom->createElement('WebsiteConfiguration');
$redirectAllRequestsTo = $dom->createElement('RedirectAllRequestsTo');
$hostName = $dom->createElement('HostName', $location);
$redirectAllRequestsTo->appendChild($hostName);
$websiteConfiguration->appendChild($redirectAllRequestsTo);
$dom->appendChild($websiteConfiguration);
$rest->setParameter('website', null);
$rest->data = $dom->saveXML();
$rest->size = strlen($rest->data);
$rest->setHeader('Content-Type', 'application/xml');
$rest = $rest->getResponse();

if ($rest->error === false && $rest->code !== 200)
$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
if ($rest->error !== false)
{
self::__triggerError(sprintf("S3::setBucketRedirect({$bucket}, {$location}): [%s] %s",
$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
return false;
}
return true;
}


/**
* Set logging for a bucket
*
Expand Down

0 comments on commit 66c2eae

Please sign in to comment.