1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
<?php
/**
* Copyright 2012-2014 Rackspace US, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace OpenCloud\ObjectStore\Resource;
use Guzzle\Http\EntityBody;
use Guzzle\Http\Exception\BadResponseException;
use Guzzle\Http\Exception\ClientErrorResponseException;
use Guzzle\Http\Message\Response;
use Guzzle\Http\Url;
use OpenCloud\Common\Constants\Size;
use OpenCloud\Common\Exceptions;
use OpenCloud\Common\Service\ServiceInterface;
use OpenCloud\ObjectStore\Constants\Header as HeaderConst;
use OpenCloud\ObjectStore\Exception\ContainerException;
use OpenCloud\ObjectStore\Exception\ObjectNotFoundException;
use OpenCloud\ObjectStore\Upload\DirectorySync;
use OpenCloud\ObjectStore\Upload\TransferBuilder;
use OpenCloud\ObjectStore\Enum\ReturnType;
/**
* A container is a storage compartment for your data and provides a way for you
* to organize your data. You can think of a container as a folder in Windows
* or a directory in Unix. The primary difference between a container and these
* other file system concepts is that containers cannot be nested.
*
* A container can also be CDN-enabled (for public access), in which case you
* will need to interact with a CDNContainer object instead of this one.
*/
class Container extends AbstractContainer
{
const METADATA_LABEL = 'Container';
/**
* This is the object that holds all the CDN functionality. This Container therefore acts as a simple wrapper and is
* interested in storage concerns only.
*
* @var CDNContainer|null
*/
private $cdn;
public function __construct(ServiceInterface $service, $data = null)
{
parent::__construct($service, $data);
// Set metadata items for collection listings
if (isset($data->count)) {
$this->metadata->setProperty('Object-Count', $data->count);
}
if (isset($data->bytes)) {
$this->metadata->setProperty('Bytes-Used', $data->bytes);
}
}
/**
* Factory method that instantiates an object from a Response object.
*
* @param Response $response
* @param ServiceInterface $service
* @return static
*/
public static function fromResponse(Response $response, ServiceInterface $service)
{
$self = parent::fromResponse($response, $service);
$segments = Url::factory($response->getEffectiveUrl())->getPathSegments();
$self->name = end($segments);
return $self;
}
/**
* Get the CDN object.
*
* @return null|CDNContainer
* @throws \OpenCloud\Common\Exceptions\CdnNotAvailableError
*/
public function getCdn()
{
if (!$this->isCdnEnabled()) {
throw new Exceptions\CdnNotAvailableError(
'Either this container is not CDN-enabled or the CDN is not available'
);
}
return $this->cdn;
}
/**
* It would be awesome to put these convenience methods (which are identical to the ones in the Account object) in
* a trait, but we have to wait for v5.3 EOL first...
*
* @return null|string|int
*/
public function getObjectCount()
{
return $this->metadata->getProperty('Object-Count');
}
/**
* @return null|string|int
*/
public function getBytesUsed()
{
return $this->metadata->getProperty('Bytes-Used');
}
/**
* @param $value
* @return mixed
*/
public function setCountQuota($value)
{
$this->metadata->setProperty('Quota-Count', $value);
return $this->saveMetadata($this->metadata->toArray());
}
/**
* @return null|string|int
*/
public function getCountQuota()
{
return $this->metadata->getProperty('Quota-Count');
}
/**
* @param $value
* @return mixed
*/
public function setBytesQuota($value)
{
$this->metadata->setProperty('Quota-Bytes', $value);
return $this->saveMetadata($this->metadata->toArray());
}
/**
* @return null|string|int
*/
public function getBytesQuota()
{
return $this->metadata->getProperty('Quota-Bytes');
}
public function delete($deleteObjects = false)
{
if ($deleteObjects === true) {
// Delegate to auxiliary method
return $this->deleteWithObjects();
}
try {
return $this->getClient()->delete($this->getUrl())->send();
} catch (ClientErrorResponseException $e) {
if ($e->getResponse()->getStatusCode() == 409) {
throw new ContainerException(sprintf(
'The API returned this error: %s. You might have to delete all existing objects before continuing.',
(string) $e->getResponse()->getBody()
));
} else {
throw $e;
}
}
}
public function deleteWithObjects($secondsToWait = null)
{
// If container is empty, just delete it
$numObjects = (int) $this->retrieveMetadata()->getProperty('Object-Count');
if (0 === $numObjects) {
return $this->delete();
}
// If timeout ($secondsToWait) is not specified by caller,
// try to estimate it based on number of objects in container
if (null === $secondsToWait) {
$secondsToWait = round($numObjects / 2);
}
// Attempt to delete all objects and container
$endTime = time() + $secondsToWait;
$containerDeleted = false;
while ((time() < $endTime) && !$containerDeleted) {
$this->deleteAllObjects();
try {
$response = $this->delete();
$containerDeleted = true;
} catch (ContainerException $e) {
// Ignore exception and try again
} catch (ClientErrorResponseException $e) {
if ($e->getResponse()->getStatusCode() == 404) {
// Container has been deleted
$containerDeleted = true;
} else {
throw $e;
}
}
}
if (!$containerDeleted) {
throw new ContainerException('Container and all its objects could not be deleted.');
}
return $response;
}
/**
* Deletes all objects that this container currently contains. Useful when doing operations (like a delete) that
* require an empty container first.
*
* @return mixed
*/
public function deleteAllObjects()
{
$paths = array();
$objects = $this->objectList();
foreach ($objects as $object) {
$paths[] = sprintf('/%s/%s', $this->getName(), $object->getName());
}
return $this->getService()->batchDelete($paths);
}
/**
* Delete an object from the API.
*
* @param string $name The name of object you want to delete
* @throws \Guzzle\Http\Exception\BadResponseException When an error occurred
*/
public function deleteObject($name)
{
$this->getClient()
->delete($this->getUrl($name))
->send();
}
/**
* Creates a Collection of objects in the container
*
* @param array $params associative array of parameter values.
* * account/tenant - The unique identifier of the account/tenant.
* * container- The unique identifier of the container.
* * limit (Optional) - The number limit of results.
* * marker (Optional) - Value of the marker, that the object names
* greater in value than are returned.
* * end_marker (Optional) - Value of the marker, that the object names
* less in value than are returned.
* * prefix (Optional) - Value of the prefix, which the returned object
* names begin with.
* * format (Optional) - Value of the serialized response format, either
* json or xml.
* * delimiter (Optional) - Value of the delimiter, that all the object
* names nested in the container are returned.
* @link http://api.openstack.org for a list of possible parameter
* names and values
* @return \OpenCloud\Common\Collection
* @throws ObjFetchError
*/
public function objectList(array $params = array())
{
$params['format'] = 'json';
return $this->getService()->resourceList('DataObject', $this->getUrl(null, $params), $this);
}
/**
* Turn on access logs, which track all the web traffic that your data objects accrue.
*
* @return \Guzzle\Http\Message\Response
*/
public function enableLogging()
{
return $this->saveMetadata($this->appendToMetadata(array(
HeaderConst::ACCESS_LOGS => 'True'
)));
}
/**
* Disable access logs.
*
* @return \Guzzle\Http\Message\Response
*/
public function disableLogging()
{
return $this->saveMetadata($this->appendToMetadata(array(
HeaderConst::ACCESS_LOGS => 'False'
)));
}
/**
* Enable this container for public CDN access.
*
* @param null $ttl
*/
public function enableCdn($ttl = null)
{
$headers = array('X-CDN-Enabled' => 'True');
if ($ttl) {
$headers['X-TTL'] = (int) $ttl;
}
$this->getClient()->put($this->getCdnService()->getUrl($this->name), $headers)->send();
$this->refresh();
}
/**
* Disables the containers CDN function. Note that the container will still
* be available on the CDN until its TTL expires.
*
* @return \Guzzle\Http\Message\Response
*/
public function disableCdn()
{
$headers = array('X-CDN-Enabled' => 'False');
return $this->getClient()
->put($this->getCdnService()->getUrl($this->name), $headers)
->send();
}
public function refresh($id = null, $url = null)
{
$headers = $this->createRefreshRequest()->send()->getHeaders();
$this->setMetadata($headers, true);
}
/**
* Get either a fresh data object (no $info), or get an existing one by passing in data for population.
*
* @param mixed $info
* @return DataObject
*/
public function dataObject($info = null)
{
return new DataObject($this, $info);
}
/**
* Retrieve an object from the API. Apart from using the name as an
* identifier, you can also specify additional headers that will be used
* fpr a conditional GET request. These are
*
* * `If-Match'
* * `If-None-Match'
* * `If-Modified-Since'
* * `If-Unmodified-Since'
* * `Range' For example:
* bytes=-5 would mean the last 5 bytes of the object
* bytes=10-15 would mean 5 bytes after a 10 byte offset
* bytes=32- would mean all dat after first 32 bytes
*
* These are also documented in RFC 2616.
*
* @param string $name
* @param array $headers
* @return DataObject
*/
public function getObject($name, array $headers = array())
{
try {
$response = $this->getClient()
->get($this->getUrl($name), $headers)
->send();
} catch (BadResponseException $e) {
if ($e->getResponse()->getStatusCode() == 404) {
throw ObjectNotFoundException::factory($name, $e);
}
throw $e;
}
return $this->dataObject()
->populateFromResponse($response)
->setName($name);
}
/**
* Essentially the same as {@see getObject()}, except only the metadata is fetched from the API.
* This is useful for cases when the user does not want to fetch the full entity body of the
* object, only its metadata.
*
* @param $name
* @param array $headers
* @return $this
*/
public function getPartialObject($name, array $headers = array())
{
$response = $this->getClient()
->head($this->getUrl($name), $headers)
->send();
return $this->dataObject()
->populateFromResponse($response)
->setName($name);
}
/**
* Check if an object exists inside a container. Uses {@see getPartialObject()}
* to save on bandwidth and time.
*
* @param $name Object name
* @return boolean True, if object exists in this container; false otherwise.
*/
public function objectExists($name)
{
try {
// Send HEAD request to check resource existence
$url = clone $this->getUrl();
$url->addPath((string) $name);
$this->getClient()->head($url)->send();
} catch (ClientErrorResponseException $e) {
// If a 404 was returned, then the object doesn't exist
if ($e->getResponse()->getStatusCode() === 404) {
return false;
} else {
throw $e;
}
}
return true;
}
/**
* Upload a single file to the API.
*
* @param $name Name that the file will be saved as in your container.
* @param $data Either a string or stream representation of the file contents to be uploaded.
* @param array $headers Optional headers that will be sent with the request (useful for object metadata).
* @return DataObject
*/
public function uploadObject($name, $data, array $headers = array())
{
$entityBody = EntityBody::factory($data);
$url = clone $this->getUrl();
$url->addPath($name);
// @todo for new major release: Return response rather than populated DataObject
$response = $this->getClient()->put($url, $headers, $entityBody)->send();
return $this->dataObject()
->populateFromResponse($response)
->setName($name)
->setContent($entityBody);
}
/**
* Upload an array of objects for upload. This method optimizes the upload procedure by batching requests for
* faster execution. This is a very useful procedure when you just have a bunch of unremarkable files to be
* uploaded quickly. Each file must be under 5GB.
*
* @param array $files With the following array structure:
* `name' Name that the file will be saved as in your container. Required.
* `path' Path to an existing file, OR
* `body' Either a string or stream representation of the file contents to be uploaded.
* @param array $headers Optional headers that will be sent with the request (useful for object metadata).
* @param string $returnType One of OpenCloud\ObjectStore\Enum\ReturnType::RESPONSE_ARRAY (to return an array of
* Guzzle\Http\Message\Response objects) or OpenCloud\ObjectStore\Enum\ReturnType::DATA_OBJECT_ARRAY
* (to return an array of OpenCloud\ObjectStore\Resource\DataObject objects).
*
* @throws \OpenCloud\Common\Exceptions\InvalidArgumentError
* @return Guzzle\Http\Message\Response[] or OpenCloud\ObjectStore\Resource\DataObject[] depending on $returnType
*/
public function uploadObjects(array $files, array $commonHeaders = array(), $returnType = ReturnType::RESPONSE_ARRAY)
{
$requests = $entities = array();
foreach ($files as $entity) {
if (empty($entity['name'])) {
throw new Exceptions\InvalidArgumentError('You must provide a name.');
}
if (!empty($entity['path']) && file_exists($entity['path'])) {
$body = fopen($entity['path'], 'r+');
} elseif (!empty($entity['body'])) {
$body = $entity['body'];
} else {
throw new Exceptions\InvalidArgumentError('You must provide either a readable path or a body');
}
$entityBody = $entities[] = EntityBody::factory($body);
// @codeCoverageIgnoreStart
if ($entityBody->getContentLength() >= 5 * Size::GB) {
throw new Exceptions\InvalidArgumentError(
'For multiple uploads, you cannot upload more than 5GB per '
. ' file. Use the UploadBuilder for larger files.'
);
}
// @codeCoverageIgnoreEnd
// Allow custom headers and common
$headers = (isset($entity['headers'])) ? $entity['headers'] : $commonHeaders;
$url = clone $this->getUrl();
$url->addPath($entity['name']);
$requests[] = $this->getClient()->put($url, $headers, $entityBody);
}
$responses = $this->getClient()->send($requests);
if (ReturnType::RESPONSE_ARRAY === $returnType) {
foreach ($entities as $entity) {
$entity->close();
}
return $responses;
} else {
// Convert responses to DataObjects before returning
$dataObjects = array();
foreach ($responses as $index => $response) {
$dataObjects[] = $this->dataObject()
->populateFromResponse($response)
->setName($files[$index]['name'])
->setContent($entities[$index]);
}
return $dataObjects;
}
}
/**
* When uploading large files (+5GB), you need to upload the file as chunks using multibyte transfer. This method
* sets up the transfer, and in order to execute the transfer, you need to call upload() on the returned object.
*
* @param array Options
* @see \OpenCloud\ObjectStore\Upload\UploadBuilder::setOptions for a list of accepted options.
* @throws \OpenCloud\Common\Exceptions\InvalidArgumentError
* @return mixed
*/
public function setupObjectTransfer(array $options = array())
{
// Name is required
if (empty($options['name'])) {
throw new Exceptions\InvalidArgumentError('You must provide a name.');
}
// As is some form of entity body
if (!empty($options['path']) && file_exists($options['path'])) {
$body = fopen($options['path'], 'r+');
} elseif (!empty($options['body'])) {
$body = $options['body'];
} else {
throw new Exceptions\InvalidArgumentError('You must provide either a readable path or a body');
}
// Build upload
$transfer = TransferBuilder::newInstance()
->setOption('objectName', $options['name'])
->setEntityBody(EntityBody::factory($body))
->setContainer($this);
// Add extra options
if (!empty($options['metadata'])) {
$transfer->setOption('metadata', $options['metadata']);
}
if (!empty($options['partSize'])) {
$transfer->setOption('partSize', $options['partSize']);
}
if (!empty($options['concurrency'])) {
$transfer->setOption('concurrency', $options['concurrency']);
}
if (!empty($options['progress'])) {
$transfer->setOption('progress', $options['progress']);
}
return $transfer->build();
}
/**
* Upload the contents of a local directory to a remote container, effectively syncing them.
*
* @param string $path The local path to the directory.
* @param string $targetDir The path (or pseudo-directory) that all files will be nested in.
*/
public function uploadDirectory($path, $targetDir = null)
{
$sync = DirectorySync::factory($path, $this, $targetDir);
$sync->execute();
}
public function isCdnEnabled()
{
// If CDN object is not already populated, try to populate it.
if (null === $this->cdn) {
$this->refreshCdnObject();
}
return ($this->cdn instanceof CDNContainer) && $this->cdn->isCdnEnabled();
}
protected function refreshCdnObject()
{
try {
if (null !== ($cdnService = $this->getService()->getCDNService())) {
$cdn = new CDNContainer($cdnService);
$cdn->setName($this->name);
$response = $cdn->createRefreshRequest()->send();
if ($response->isSuccessful()) {
$this->cdn = $cdn;
$this->cdn->setMetadata($response->getHeaders(), true);
}
} else {
$this->cdn = null;
}
} catch (ClientErrorResponseException $e) {
}
}
}