Skip to content

Commit

Permalink
Update and spark support
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Jul 19, 2012
1 parent 06d427b commit d238237
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 9 deletions.
87 changes: 78 additions & 9 deletions libraries/s3.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public static function getBucket($bucket, $prefix = null, $marker = null, $maxKe
$rest->setParameter('marker', $nextMarker);
if ($delimiter !== null && $delimiter !== '') $rest->setParameter('delimiter', $delimiter);

if (($response = $rest->getResponse(true)) == false || $response->code !== 200) break;
if (($response = $rest->getResponse()) == false || $response->code !== 200) break;

if (isset($response->body, $response->body->Contents))
foreach ($response->body->Contents as $c)
Expand Down Expand Up @@ -484,7 +484,7 @@ public static function putObject($input, $bucket, $uri, $acl = self::ACL_PRIVATE
if ($input === false) return false;
$rest = new S3Request('PUT', $bucket, $uri, self::$endpoint);

if (is_string($input)) $input = array(
if (!is_array($input)) $input = array(
'data' => $input, 'size' => strlen($input),
'md5sum' => base64_encode(md5($input, true))
);
Expand Down Expand Up @@ -665,7 +665,7 @@ public static function copyObject($srcBucket, $srcUri, $bucket, $uri, $acl = sel
foreach ($metaHeaders as $h => $v) $rest->setAmzHeader('x-amz-meta-'.$h, $v);
if ($storageClass !== self::STORAGE_CLASS_STANDARD) // Storage class
$rest->setAmzHeader('x-amz-storage-class', $storageClass);
$rest->setAmzHeader('x-amz-acl', $acl); // Added rawurlencode() for $srcUri (thanks a.yamanoi)
$rest->setAmzHeader('x-amz-acl', $acl);
$rest->setAmzHeader('x-amz-copy-source', sprintf('/%s/%s', $srcBucket, rawurlencode($srcUri)));
if (sizeof($requestHeaders) > 0 || sizeof($metaHeaders) > 0)
$rest->setAmzHeader('x-amz-metadata-directive', 'REPLACE');
Expand Down Expand Up @@ -741,7 +741,7 @@ public static function setBucketLogging($bucket, $targetBucket, $targetPrefix =
$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
if ($rest->error !== false)
{
self::__triggerError(sprintf("S3::setBucketLogging({$bucket}, {$uri}): [%s] %s",
self::__triggerError(sprintf("S3::setBucketLogging({$bucket}, {$targetBucket}): [%s] %s",
$rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
return false;
}
Expand Down Expand Up @@ -978,7 +978,7 @@ public static function deleteObject($bucket, $uri)
public static function getAuthenticatedURL($bucket, $uri, $lifetime, $hostBucket = false, $https = false)
{
$expires = time() + $lifetime;
$uri = str_replace(array('%2F', '%2B'), array('/', '+'), rawurlencode($uri)); // URI should be encoded (thanks Sean O'Dea)
$uri = str_replace(array('%2F', '%2B'), array('/', '+'), rawurlencode($uri));
return sprintf(($https ? 'https' : 'http').'://%s/%s?AWSAccessKeyId=%s&Expires=%u&Signature=%s',
// $hostBucket ? $bucket : $bucket.'.s3.amazonaws.com', $uri, self::$__accessKey, $expires,
$hostBucket ? $bucket : 's3.amazonaws.com/'.$bucket, $uri, self::$__accessKey, $expires,
Expand Down Expand Up @@ -1417,6 +1417,57 @@ private static function __getCloudFrontInvalidationBatchXML($paths, $callerRefer
}


/**
* List your invalidation batches for invalidateDistribution() in a CloudFront distribution
*
* http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/ListInvalidation.html
* returned array looks like this:
* Array
* (
* [I31TWB0CN9V6XD] => InProgress
* [IT3TFE31M0IHZ] => Completed
* [I12HK7MPO1UQDA] => Completed
* [I1IA7R6JKTC3L2] => Completed
* )
*
* @param string $distributionId Distribution ID from listDistributions()
* @return array
*/
public static function getDistributionInvalidationList($distributionId)
{
if (!extension_loaded('openssl'))
{
self::__triggerError(sprintf("S3::getDistributionInvalidationList(): [%s] %s",
"CloudFront functionality requires SSL"), __FILE__, __LINE__);
return false;
}

$useSSL = self::$useSSL;
self::$useSSL = true; // CloudFront requires SSL
$rest = new S3Request('GET', '', '2010-11-01/distribution/'.$distributionId.'/invalidation', 'cloudfront.amazonaws.com');
$rest = self::__getCloudFrontResponse($rest);
self::$useSSL = $useSSL;

if ($rest->error === false && $rest->code !== 200)
$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
if ($rest->error !== false)
{
trigger_error(sprintf("S3::getDistributionInvalidationList('{$distributionId}'): [%s]",
$rest->error['code'], $rest->error['message']), E_USER_WARNING);
return false;
}
elseif ($rest->body instanceof SimpleXMLElement && isset($rest->body->InvalidationSummary))
{
$list = array();
foreach ($rest->body->InvalidationSummary as $summary)
$list[(string)$summary->Id] = (string)$summary->Status;

return $list;
}
return array();
}


/**
* Get a DistributionConfig DOMDocument
*
Expand Down Expand Up @@ -1749,7 +1800,6 @@ public function getResponse()
$query = substr($this->uri, -1) !== '?' ? '?' : '&';
foreach ($this->parameters as $var => $value)
if ($value == null || $value == '') $query .= $var.'&';
// Parameters should be encoded (thanks Sean O'Dea)
else $query .= $var.'='.rawurlencode($value).'&';
$query = substr($query, 0, -1);
$this->uri .= $query;
Expand Down Expand Up @@ -1786,7 +1836,7 @@ public function getResponse()
{
curl_setopt($curl, CURLOPT_PROXY, S3::$proxy['host']);
curl_setopt($curl, CURLOPT_PROXYTYPE, S3::$proxy['type']);
if (isset(S3::$proxy['user'], S3::$proxy['pass']) && $proxy['user'] != null && $proxy['pass'] != null)
if (isset(S3::$proxy['user'], S3::$proxy['pass']) && S3::$proxy['user'] != null && S3::$proxy['pass'] != null)
curl_setopt($curl, CURLOPT_PROXYUSERPWD, sprintf('%s:%s', S3::$proxy['user'], S3::$proxy['pass']));
}

Expand All @@ -1804,7 +1854,8 @@ public function getResponse()
// AMZ headers must be sorted
if (sizeof($amz) > 0)
{
sort($amz);
//sort($amz);
usort($amz, array(&$this, '__sortMetaHeadersCmp'));
$amz = "\n".implode("\n", $amz);
} else $amz = '';

Expand Down Expand Up @@ -1900,6 +1951,24 @@ public function getResponse()
return $this->response;
}

/**
* Sort compare for meta headers
*
* @internal Used to sort x-amz meta headers
* @param string $a String A
* @param string $b String B
* @return integer
*/
private function __sortMetaHeadersCmp($a, $b)
{
$lenA = strpos($a, ':');
$lenB = strpos($b, ':');
$minLen = min($lenA, $lenB);
$ncmp = strncmp($a, $b, $minLen);
if ($lenA == $lenB) return $ncmp;
if (0 == $ncmp) return $lenA < $lenB ? -1 : 1;
return $ncmp;
}

/**
* CURL write callback
Expand Down Expand Up @@ -1961,7 +2030,7 @@ private function __responseHeaderCallback(&$curl, &$data)
elseif ($header == 'ETag')
$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;
$this->response->headers[$header] = $value;
}
return $strlen;
}
Expand Down
26 changes: 26 additions & 0 deletions spark.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This is the spark-sdk specification. It's in a magical format without a name.
# Use this format while developing your own sparks!

# This is the spark name. This should be the registered name of the spark.
# It's only used for informational purposes.
name: s3

# This is the current version of this spark. All sparks should be in
# x.x.x format. Validation will fail otherwise. This is the version
# that will appear on the website.
version: 1.0.1

# This is the version of CodeIgniter this spark is compatible up to. It should
# be in x.x.x format
compatibility: 2.1.2

# These are other sparks which this spark needs in order to work correctly.
# Dependencies should be in NAME: VERSION format, where NAME is an existing
# spark name, and VERSION is a version in x.x.x format.
# This is optional, so comment it out if it's unneeded
#dependencies:
# spark-1: 1.0.0
# spark-2: 1.0.0

# These are tags to associate your spark with
tags: ["amazon", "s3"]

0 comments on commit d238237

Please sign in to comment.