33 namespace HPCloud\Storage;
35 use HPCloud\Storage\ObjectStorage\Container;
36 use HPCloud\Storage\ObjectStorage\ACL;
142 'X-Auth-Key' =>
$key,
145 $client = \HPCloud\Transport::instance();
149 $res = $client->doRequest(
$url,
'GET', $headers);
159 $token = $res->header(
'X-Auth-Token');
160 $newUrl = $res->header(
'X-Storage-Url');
182 $cat = $identity->serviceCatalog();
183 $tok = $identity->token();
208 for ($i = 0; $i < $c; ++$i) {
209 if (
$catalog[$i][
'type'] == self::SERVICE_TYPE) {
211 if (isset($endpoint[
'publicURL']) && $endpoint[
'region'] == $region) {
237 $this->
token = $authToken;
262 throw new \HPCloud\Exception(
'Cannot use CDN: No CDN provided.');
265 $containers = $cdn->containers(TRUE);
268 foreach ($containers as $item) {
271 if ($item[
'cdn_enabled'] == 1) {
272 $buffer[$item[
'name']] = array(
273 'url' => $item[
'x-cdn-uri'],
274 'sslUrl' => $item[
'x-cdn-ssl-uri'],
278 $this->cdnContainers = $buffer;
284 return !empty($this->cdnContainers);
303 public function cdnUrl($containerName, $ssl = TRUE) {
304 if (!empty($this->cdnContainers[$containerName])) {
305 $key = $ssl ?
'sslUrl' :
'url';
306 return $this->cdnContainers[$containerName][
$key];
371 $url = $this->
url() .
'?format=json';
374 $url .= sprintf(
'&limit=%d', $limit);
376 if (!empty($marker)) {
377 $url .= sprintf(
'&marker=%d', $marker);
380 $containers = $this->
get(
$url);
382 $containerList = array();
384 $cname = $container[
'name'];
387 if (!empty($this->cdnContainers[$cname])) {
388 $cdnList = $this->cdnContainers[$cname];
389 $containerList[$cname]->useCDN($cdnList[
'url'], $cdnList[
'sslUrl']);
393 return $containerList;
412 $data = $this->
req(
$url,
'HEAD', FALSE);
414 $status = $data->status();
415 if ($status == 204) {
418 if (isset($this->cdnContainers[
$name])) {
419 $cdnList = $this->cdnContainers[
$name];
420 $container->useCDN($cdnList[
'url'], $cdnList[
'sslUrl']);
427 throw new \HPCloud\Exception(
"Unknown status: $status");
449 catch (\HPCloud\
Transport\FileNotFoundException $fnfe) {
521 'X-Auth-Token' => $this->
token(),
524 if (!empty($metadata)) {
529 $client = \HPCloud\Transport::instance();
532 $headers += $acl->headers();
535 $data = $client->doRequest(
$url,
'PUT', $headers);
538 $status = $data->status();
540 if ($status == 201) {
542 } elseif ($status == 202) {
547 throw new \HPCloud\Exception(
'Server returned unexpected code: ' . $status);
614 $data = $this->
req(
$url,
'DELETE', FALSE);
616 catch (\HPCloud\
Transport\FileNotFoundException $e) {
621 catch (\HPCloud\
Transport\ConflictException $e) {
622 throw new ObjectStorage\ContainerNotEmptyException(
"Non-empty container cannot be deleted.");
625 $status = $data->status();
628 if ($status == 204) {
634 throw new \HPCloud\Exception(
'Server returned unexpected code: ' . $status);
657 $data = $this->
req(
$url,
'HEAD', FALSE);
660 'bytes' => $data->header(
'X-Account-Bytes-Used', 0),
661 'containers' => $data->header(
'X-Account-Container-Count', 0),
662 'objects' => $data->header(
'X-Account-Container-Count', 0),
674 protected function get(
$url, $jsonDecode = TRUE) {
675 return $this->
req(
$url,
'GET', $jsonDecode);
681 protected function req(
$url, $method =
'GET', $jsonDecode = TRUE, $body =
'') {
682 $client = \HPCloud\Transport::instance();
684 'X-Auth-Token' => $this->
token(),
687 $raw = $client->doRequest(
$url, $method, $headers, $body);
691 return json_decode($raw->content(), TRUE);