Overview

Namespaces

  • OpenCloud
    • Autoscale
      • Resource
    • CloudMonitoring
      • Exception
      • Resource
    • Common
      • Collection
      • Constants
      • Exceptions
      • Http
        • Message
      • Identity
      • Log
      • Service
    • Compute
      • Constants
      • Exception
      • Resource
    • Database
      • Resource
    • DNS
      • Resource
    • LoadBalancer
      • Resource
    • ObjectStore
      • Constants
      • Exception
      • Resource
      • Upload
    • Orchestration
    • Queues
      • Exception
      • Resource
    • Volume
      • Resource
  • PHP

Classes

  • AbstractService
  • CDNService
  • Service
  • Overview
  • Namespace
  • Class
  • Tree
  • Download
  1: <?php
  2: /**
  3:  * PHP OpenCloud library.
  4:  * 
  5:  * @copyright 2013 Rackspace Hosting, Inc. See LICENSE for information.
  6:  * @license   https://www.apache.org/licenses/LICENSE-2.0
  7:  * @author    Glen Campbell <glen.campbell@rackspace.com>
  8:  * @author    Jamie Hannaford <jamie.hannaford@rackspace.com>
  9:  */
 10: 
 11: namespace OpenCloud\ObjectStore;
 12: 
 13: use Guzzle\Http\EntityBody;
 14: use OpenCloud\Common\Constants\Header;
 15: use OpenCloud\Common\Constants\Mime;
 16: use OpenCloud\Common\Exceptions;
 17: use OpenCloud\Common\Exceptions\InvalidArgumentError;
 18: use OpenCloud\Common\Http\Client;
 19: use OpenCloud\Common\Http\Message\Formatter;
 20: use OpenCloud\Common\Service\ServiceBuilder;
 21: use OpenCloud\ObjectStore\Resource\Container;
 22: use OpenCloud\ObjectStore\Constants\UrlType;
 23: 
 24: /**
 25:  * The ObjectStore (Cloud Files) service.
 26:  */
 27: class Service extends AbstractService 
 28: {
 29:     const DEFAULT_NAME = 'cloudFiles';
 30:     const DEFAULT_TYPE = 'object-store';
 31: 
 32:     /**
 33:      * This holds the associated CDN service (for Rackspace public cloud)
 34:      * or is NULL otherwise. The existence of an object here is
 35:      * indicative that the CDN service is available.
 36:      */
 37:     private $cdnService;
 38: 
 39:     public function __construct(Client $client, $type = null, $name = null, $region = null, $urlType = null)
 40:     {
 41:         parent::__construct($client, $type, $name, $region, $urlType);
 42: 
 43:         try {
 44:             $this->cdnService = ServiceBuilder::factory($client, 'OpenCloud\ObjectStore\CDNService', array(
 45:                 'region' => $region
 46:             ));
 47:         } catch (Exceptions\EndpointError $e) {}
 48:     }
 49: 
 50:     /**
 51:      * @return CDNService
 52:      */
 53:     public function getCdnService() 
 54:     {
 55:         return $this->cdnService;
 56:     }
 57: 
 58:     /**
 59:      * @param $data
 60:      * @return Container
 61:      */
 62:     public function getContainer($data = null)
 63:     {
 64:         return new Container($this, $data);
 65:     }
 66: 
 67:     /**
 68:      * Create a container for this service.
 69:      *
 70:      * @param       $name     The name of the container
 71:      * @param array $metadata Additional (optional) metadata to associate with the container
 72:      * @return bool|static
 73:      */
 74:     public function createContainer($name, array $metadata = array())
 75:     {
 76:         $this->checkContainerName($name);
 77:         
 78:         $containerHeaders = Container::stockHeaders($metadata);
 79:             
 80:         $response = $this->getClient()
 81:             ->put($this->getUrl($name), $containerHeaders)
 82:             ->send();
 83:         
 84:         if ($response->getStatusCode() == 201) {
 85:             return Container::fromResponse($response, $this);
 86:         }
 87:         
 88:         return false;
 89:     }
 90: 
 91:     /**
 92:      * Check the validity of a potential container name.
 93:      *
 94:      * @param $name
 95:      * @return bool
 96:      * @throws \OpenCloud\Common\Exceptions\InvalidArgumentError
 97:      */
 98:     public function checkContainerName($name)
 99:     {
100:         if (strlen($name) == 0) {
101:             $error = 'Container name cannot be blank';
102:         }
103: 
104:         if (strpos($name, '/') !== false) {
105:             $error = 'Container name cannot contain "/"';
106:         }
107: 
108:         if (strlen($name) > self::MAX_CONTAINER_NAME_LENGTH) {
109:             $error = 'Container name is too long';
110:         }
111:         
112:         if (isset($error)) {
113:             throw new InvalidArgumentError($error);
114:         }
115: 
116:         return true;
117:     }
118: 
119:     /**
120:      * Perform a bulk extraction, expanding an archive file. If the $path is an empty string, containers will be
121:      * auto-created accordingly, and files in the archive that do not map to any container (files in the base directory)
122:      * will be ignored. You can create up to 1,000 new containers per extraction request. Also note that only regular
123:      * files will be uploaded. Empty directories, symlinks, and so on, will not be uploaded.
124:      *
125:      * @param        $path        The path to the archive being extracted
126:      * @param        $archive     The contents of the archive (either string or stream)
127:      * @param string $archiveType The type of archive you're using {@see \OpenCloud\ObjectStore\Constants\UrlType}
128:      * @return \Guzzle\Http\Message\Response
129:      * @throws Exception\BulkOperationException
130:      * @throws \OpenCloud\Common\Exceptions\InvalidArgumentError
131:      */
132:     public function bulkExtract($path = '', $archive, $archiveType = UrlType::TAR_GZ)
133:     {
134:         $entity = EntityBody::factory($archive);
135:         
136:         $acceptableTypes = array(
137:             UrlType::TAR,
138:             UrlType::TAR_GZ,
139:             UrlType::TAR_BZ2
140:         );
141:         
142:         if (!in_array($archiveType, $acceptableTypes)) {
143:             throw new InvalidArgumentError(sprintf(
144:                 'The archive type must be one of the following: [%s]. You provided [%s].',
145:                 implode($acceptableTypes, ','),
146:                 print_r($archiveType, true)
147:             ));
148:         }
149:         
150:         $url = $this->getUrl()->addPath($path)->setQuery(array('extract-archive' => $archiveType));
151:         $response = $this->getClient()->put($url, array(Header::CONTENT_TYPE => ''), $entity)->send();
152:         
153:         $body = Formatter::decode($response);
154: 
155:         if (!empty($body->Errors)) {
156:             throw new Exception\BulkOperationException((array) $body->Errors);
157:         }
158:         
159:         return $response;
160:     }
161: 
162:     /**
163:      * This method will delete multiple objects or containers from their account with a single request.
164:      *
165:      * @param array $paths A two-dimensional array of paths:
166:      *                      array('container_a/file_1', 'container_b/file_78', 'container_c/file_40582')
167:      * @return \Guzzle\Http\Message\Response
168:      * @throws Exception\BulkOperationException
169:      */
170:     public function bulkDelete(array $paths)
171:     {
172:         $entity = EntityBody::factory(implode(PHP_EOL, $paths));
173:         
174:         $url = $this->getUrl()->setQuery(array('bulk-delete' => true));
175:         
176:         $response = $this->getClient()
177:             ->delete($url, array(Header::CONTENT_TYPE => Mime::TEXT), $entity)
178:             ->send();
179: 
180:         try {
181:             $body = Formatter::decode($response);
182:             if (!empty($body->Errors)) {
183:                 throw new Exception\BulkOperationException((array) $body->Errors);
184:             }
185:         } catch (Exceptions\JsonError $e) {}
186:         
187:         return $response;
188:     }
189:     
190: }
PHP OpenCloud API API documentation generated by ApiGen 2.8.0