Swift.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Files\ObjectStore;
  25. use Guzzle\Http\Exception\ClientErrorResponseException;
  26. use OCP\Files\ObjectStore\IObjectStore;
  27. use OpenCloud\OpenStack;
  28. use OpenCloud\Rackspace;
  29. class Swift implements IObjectStore {
  30. /**
  31. * @var \OpenCloud\OpenStack
  32. */
  33. private $client;
  34. /**
  35. * @var array
  36. */
  37. private $params;
  38. /**
  39. * @var \OpenCloud\ObjectStore\Service
  40. */
  41. private $objectStoreService;
  42. /**
  43. * @var \OpenCloud\ObjectStore\Resource\Container
  44. */
  45. private $container;
  46. public function __construct($params) {
  47. if (isset($params['bucket'])) {
  48. $params['container'] = $params['bucket'];
  49. }
  50. if (!isset($params['container'])) {
  51. $params['container'] = 'owncloud';
  52. }
  53. if (!isset($params['autocreate'])) {
  54. // should only be true for tests
  55. $params['autocreate'] = false;
  56. }
  57. if (isset($params['apiKey'])) {
  58. $this->client = new Rackspace($params['url'], $params);
  59. } else {
  60. $this->client = new OpenStack($params['url'], $params);
  61. }
  62. $this->params = $params;
  63. }
  64. protected function init() {
  65. if ($this->container) {
  66. return;
  67. }
  68. // the OpenCloud client library will default to 'cloudFiles' if $serviceName is null
  69. $serviceName = null;
  70. if (isset($this->params['serviceName'])) {
  71. $serviceName = $this->params['serviceName'];
  72. }
  73. // the OpenCloud client library will default to 'publicURL' if $urlType is null
  74. $urlType = null;
  75. if (isset($this->params['urlType'])) {
  76. $urlType = $this->params['urlType'];
  77. }
  78. $this->objectStoreService = $this->client->objectStoreService($serviceName, $this->params['region'], $urlType);
  79. try {
  80. $this->container = $this->objectStoreService->getContainer($this->params['container']);
  81. } catch (ClientErrorResponseException $ex) {
  82. // if the container does not exist and autocreate is true try to create the container on the fly
  83. if (isset($this->params['autocreate']) && $this->params['autocreate'] === true) {
  84. $this->container = $this->objectStoreService->createContainer($this->params['container']);
  85. } else {
  86. throw $ex;
  87. }
  88. }
  89. }
  90. /**
  91. * @return string the container name where objects are stored
  92. */
  93. public function getStorageId() {
  94. return $this->params['container'];
  95. }
  96. /**
  97. * @param string $urn the unified resource name used to identify the object
  98. * @param resource $stream stream with the data to write
  99. * @throws Exception from openstack lib when something goes wrong
  100. */
  101. public function writeObject($urn, $stream) {
  102. $this->init();
  103. $this->container->uploadObject($urn, $stream);
  104. }
  105. /**
  106. * @param string $urn the unified resource name used to identify the object
  107. * @return resource stream with the read data
  108. * @throws Exception from openstack lib when something goes wrong
  109. */
  110. public function readObject($urn) {
  111. $this->init();
  112. $object = $this->container->getObject($urn);
  113. // we need to keep a reference to objectContent or
  114. // the stream will be closed before we can do anything with it
  115. /** @var $objectContent \Guzzle\Http\EntityBody * */
  116. $objectContent = $object->getContent();
  117. $objectContent->rewind();
  118. $stream = $objectContent->getStream();
  119. // save the object content in the context of the stream to prevent it being gc'd until the stream is closed
  120. stream_context_set_option($stream, 'swift','content', $objectContent);
  121. return $stream;
  122. }
  123. /**
  124. * @param string $urn Unified Resource Name
  125. * @return void
  126. * @throws Exception from openstack lib when something goes wrong
  127. */
  128. public function deleteObject($urn) {
  129. $this->init();
  130. // see https://github.com/rackspace/php-opencloud/issues/243#issuecomment-30032242
  131. $this->container->dataObject()->setName($urn)->delete();
  132. }
  133. public function deleteContainer($recursive = false) {
  134. $this->init();
  135. $this->container->delete($recursive);
  136. }
  137. }