Overview
  • Namespace
  • Class

Namespaces

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

Classes

  • OpenCloud\Volume\Resource\Snapshot
  • OpenCloud\Volume\Resource\Volume
  • OpenCloud\Volume\Resource\VolumeType
  1 <?php
  2 /**
  3  * Copyright 2012-2014 Rackspace US, Inc.
  4  *
  5  * Licensed under the Apache License, Version 2.0 (the "License");
  6  * you may not use this file except in compliance with the License.
  7  * You may obtain a copy of the License at
  8  *
  9  * http://www.apache.org/licenses/LICENSE-2.0
 10  *
 11  * Unless required by applicable law or agreed to in writing, software
 12  * distributed under the License is distributed on an "AS IS" BASIS,
 13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  * See the License for the specific language governing permissions and
 15  * limitations under the License.
 16  */
 17 
 18 namespace OpenCloud\ObjectStore;
 19 
 20 use Guzzle\Http\EntityBody;
 21 use OpenCloud\Common\Constants\Header;
 22 use OpenCloud\Common\Constants\Mime;
 23 use OpenCloud\Common\Exceptions;
 24 use OpenCloud\Common\Exceptions\InvalidArgumentError;
 25 use OpenCloud\Common\Http\Client;
 26 use OpenCloud\Common\Http\Message\Formatter;
 27 use OpenCloud\Common\Log\Logger;
 28 use OpenCloud\Common\Service\ServiceBuilder;
 29 use OpenCloud\ObjectStore\Constants\UrlType;
 30 use OpenCloud\ObjectStore\Resource\Container;
 31 use OpenCloud\ObjectStore\Upload\ContainerMigration;
 32 
 33 /**
 34  * The ObjectStore (Cloud Files) service.
 35  */
 36 class Service extends AbstractService
 37 {
 38     const DEFAULT_NAME = 'cloudFiles';
 39     const DEFAULT_TYPE = 'object-store';
 40     const BATCH_DELETE_MAX = 10000;
 41 
 42     /**
 43      * This holds the associated CDN service (for Rackspace public cloud)
 44      * or is NULL otherwise. The existence of an object here is
 45      * indicative that the CDN service is available.
 46      */
 47     private $cdnService;
 48 
 49     public function __construct(Client $client, $type = null, $name = null, $region = null, $urlType = null)
 50     {
 51         parent::__construct($client, $type, $name, $region, $urlType);
 52 
 53         try {
 54             $this->cdnService = ServiceBuilder::factory($client, 'OpenCloud\ObjectStore\CDNService', array(
 55                 'region' => $region
 56             ));
 57         } catch (Exceptions\EndpointError $e) {
 58         }
 59     }
 60 
 61     /**
 62      * @return CDNService
 63      */
 64     public function getCdnService()
 65     {
 66         return $this->cdnService;
 67     }
 68 
 69     /**
 70      * List all available containers.
 71      *
 72      * @param array $filter
 73      * @return \OpenCloud\Common\Collection\PaginatedIterator
 74      */
 75     public function listContainers(array $filter = array())
 76     {
 77         $filter['format'] = 'json';
 78         return $this->resourceList('Container', $this->getUrl(null, $filter), $this);
 79     }
 80 
 81     /**
 82      * @param $data
 83      * @return Container
 84      */
 85     public function getContainer($data = null)
 86     {
 87         return new Container($this, $data);
 88     }
 89 
 90     /**
 91      * Create a container for this service.
 92      *
 93      * @param       $name     The name of the container
 94      * @param array $metadata Additional (optional) metadata to associate with the container
 95      * @return bool|static
 96      */
 97     public function createContainer($name, array $metadata = array())
 98     {
 99         $this->checkContainerName($name);
100 
101         $containerHeaders = Container::stockHeaders($metadata);
102 
103         $response = $this->getClient()
104             ->put($this->getUrl($name), $containerHeaders)
105             ->send();
106 
107         if ($response->getStatusCode() == 201) {
108             return Container::fromResponse($response, $this);
109         }
110 
111         return false;
112     }
113 
114     /**
115      * Check the validity of a potential container name.
116      *
117      * @param $name
118      * @return bool
119      * @throws \OpenCloud\Common\Exceptions\InvalidArgumentError
120      */
121     public function checkContainerName($name)
122     {
123         if (strlen($name) == 0) {
124             $error = 'Container name cannot be blank';
125         }
126 
127         if (strpos($name, '/') !== false) {
128             $error = 'Container name cannot contain "/"';
129         }
130 
131         if (strlen($name) > self::MAX_CONTAINER_NAME_LENGTH) {
132             $error = 'Container name is too long';
133         }
134 
135         if (isset($error)) {
136             throw new InvalidArgumentError($error);
137         }
138 
139         return true;
140     }
141 
142     /**
143      * Perform a bulk extraction, expanding an archive file. If the $path is an empty string, containers will be
144      * auto-created accordingly, and files in the archive that do not map to any container (files in the base directory)
145      * will be ignored. You can create up to 1,000 new containers per extraction request. Also note that only regular
146      * files will be uploaded. Empty directories, symlinks, and so on, will not be uploaded.
147      *
148      * @param        $path        The path to the archive being extracted
149      * @param        $archive     The contents of the archive (either string or stream)
150      * @param string $archiveType The type of archive you're using {@see \OpenCloud\ObjectStore\Constants\UrlType}
151      * @return \Guzzle\Http\Message\Response
152      * @throws Exception\BulkOperationException
153      * @throws \OpenCloud\Common\Exceptions\InvalidArgumentError
154      */
155     public function bulkExtract($path = '', $archive, $archiveType = UrlType::TAR_GZ)
156     {
157         $entity = EntityBody::factory($archive);
158 
159         $acceptableTypes = array(
160             UrlType::TAR,
161             UrlType::TAR_GZ,
162             UrlType::TAR_BZ2
163         );
164 
165         if (!in_array($archiveType, $acceptableTypes)) {
166             throw new InvalidArgumentError(sprintf(
167                 'The archive type must be one of the following: [%s]. You provided [%s].',
168                 implode($acceptableTypes, ','),
169                 print_r($archiveType, true)
170             ));
171         }
172 
173         $url = $this->getUrl()->addPath($path)->setQuery(array('extract-archive' => $archiveType));
174         $response = $this->getClient()->put($url, array(Header::CONTENT_TYPE => ''), $entity)->send();
175 
176         $body = Formatter::decode($response);
177 
178         if (!empty($body->Errors)) {
179             throw new Exception\BulkOperationException((array) $body->Errors);
180         }
181 
182         return $response;
183     }
184 
185     /**
186      * @deprecated Please use {@see batchDelete()} instead.
187      */
188     public function bulkDelete(array $paths)
189     {
190         $this->getLogger()->warning(Logger::deprecated(__METHOD__, '::batchDelete()'));
191 
192         return $this->executeBatchDeleteRequest($paths);
193     }
194 
195     /**
196      * Batch delete will delete an array of object paths. By default,
197      * the API will only accept a maximum of 10,000 object deletions
198      * per request - so for arrays that exceed this size, it is chunked
199      * and sent as individual requests.
200      *
201      * @param array $paths The objects you want to delete. Each path needs
202      *                     be formatted as /{containerName}/{objectName}. If
203      *                     you are deleting object_1 and object_2 from the
204      *                     photos_container, the array will be:
205      *
206      *                     array(
207      *                        '/photos_container/object_1',
208      *                        '/photos_container/object_2'
209      *                     )
210      *
211      * @return array The array of responses from the API
212      * @throws Exception\BulkOperationException
213      */
214     public function batchDelete(array $paths)
215     {
216         $chunks = array_chunk($paths, self::BATCH_DELETE_MAX);
217 
218         $responses = array();
219 
220         foreach ($chunks as $chunk) {
221             $responses[] = $this->executeBatchDeleteRequest($chunk);
222         }
223 
224         return $responses;
225     }
226 
227     /**
228      * Internal method for dispatching single batch delete requests.
229      *
230      * @param array $paths
231      * @return \Guzzle\Http\Message\Response
232      * @throws Exception\BulkOperationException
233      */
234     private function executeBatchDeleteRequest(array $paths)
235     {
236         $entity = EntityBody::factory(implode(PHP_EOL, $paths));
237 
238         $url = $this->getUrl()->setQuery(array('bulk-delete' => true));
239 
240         $response = $this->getClient()
241             ->delete($url, array(Header::CONTENT_TYPE => Mime::TEXT), $entity)
242             ->send();
243 
244         try {
245             $body = Formatter::decode($response);
246             if (!empty($body->Errors)) {
247                 throw new Exception\BulkOperationException((array) $body->Errors);
248             }
249         } catch (Exceptions\JsonError $e) {
250         }
251 
252         return $response;
253     }
254 
255     /**
256      * Allows files to be transferred from one container to another.
257      *
258      * @param Container $old Where you're moving files from
259      * @param Container $new Where you're moving files to
260      * @return array    Of PUT responses
261      */
262     public function migrateContainer(Container $old, Container $new, array $options = array())
263     {
264         $migration = ContainerMigration::factory($old, $new, $options);
265 
266         return $migration->transfer();
267     }
268 }
269 
API documentation generated by ApiGen