Swift.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. * @author William Pain <pain.william@gmail.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Files\ObjectStore;
  26. use function GuzzleHttp\Psr7\stream_for;
  27. use Icewind\Streams\RetryWrapper;
  28. use OCP\Files\ObjectStore\IObjectStore;
  29. use OCP\Files\StorageAuthException;
  30. class Swift implements IObjectStore {
  31. /**
  32. * @var array
  33. */
  34. private $params;
  35. /** @var SwiftFactory */
  36. private $swiftFactory;
  37. public function __construct($params, SwiftFactory $connectionFactory = null) {
  38. $this->swiftFactory = $connectionFactory ?: new SwiftFactory(
  39. \OC::$server->getMemCacheFactory()->createDistributed('swift::'),
  40. $params,
  41. \OC::$server->getLogger()
  42. );
  43. $this->params = $params;
  44. }
  45. /**
  46. * @return \OpenStack\ObjectStore\v1\Models\Container
  47. * @throws StorageAuthException
  48. * @throws \OCP\Files\StorageNotAvailableException
  49. */
  50. private function getContainer() {
  51. return $this->swiftFactory->getContainer();
  52. }
  53. /**
  54. * @return string the container name where objects are stored
  55. */
  56. public function getStorageId() {
  57. if (isset($this->params['bucket'])) {
  58. return $this->params['bucket'];
  59. }
  60. return $this->params['container'];
  61. }
  62. /**
  63. * @param string $urn the unified resource name used to identify the object
  64. * @param resource $stream stream with the data to write
  65. * @throws \Exception from openstack lib when something goes wrong
  66. */
  67. public function writeObject($urn, $stream) {
  68. $this->getContainer()->createObject([
  69. 'name' => $urn,
  70. 'stream' => stream_for($stream)
  71. ]);
  72. }
  73. /**
  74. * @param string $urn the unified resource name used to identify the object
  75. * @return resource stream with the read data
  76. * @throws \Exception from openstack lib when something goes wrong
  77. */
  78. public function readObject($urn) {
  79. $object = $this->getContainer()->getObject($urn);
  80. // we need to keep a reference to objectContent or
  81. // the stream will be closed before we can do anything with it
  82. $objectContent = $object->download();
  83. $objectContent->rewind();
  84. $stream = $objectContent->detach();
  85. // save the object content in the context of the stream to prevent it being gc'd until the stream is closed
  86. stream_context_set_option($stream, 'swift', 'content', $objectContent);
  87. return RetryWrapper::wrap($stream);
  88. }
  89. /**
  90. * @param string $urn Unified Resource Name
  91. * @return void
  92. * @throws \Exception from openstack lib when something goes wrong
  93. */
  94. public function deleteObject($urn) {
  95. $this->getContainer()->getObject($urn)->delete();
  96. }
  97. /**
  98. * @return void
  99. * @throws \Exception from openstack lib when something goes wrong
  100. */
  101. public function deleteContainer() {
  102. $this->getContainer()->delete();
  103. }
  104. }