Azure.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Files\ObjectStore;
  7. use MicrosoftAzure\Storage\Blob\BlobRestProxy;
  8. use MicrosoftAzure\Storage\Blob\Models\CreateBlockBlobOptions;
  9. use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
  10. use OCP\Files\ObjectStore\IObjectStore;
  11. class Azure implements IObjectStore {
  12. /** @var string */
  13. private $containerName;
  14. /** @var string */
  15. private $accountName;
  16. /** @var string */
  17. private $accountKey;
  18. /** @var BlobRestProxy|null */
  19. private $blobClient = null;
  20. /** @var string|null */
  21. private $endpoint = null;
  22. /** @var bool */
  23. private $autoCreate = false;
  24. /**
  25. * @param array $parameters
  26. */
  27. public function __construct(array $parameters) {
  28. $this->containerName = $parameters['container'];
  29. $this->accountName = $parameters['account_name'];
  30. $this->accountKey = $parameters['account_key'];
  31. if (isset($parameters['endpoint'])) {
  32. $this->endpoint = $parameters['endpoint'];
  33. }
  34. if (isset($parameters['autocreate'])) {
  35. $this->autoCreate = $parameters['autocreate'];
  36. }
  37. }
  38. /**
  39. * @return BlobRestProxy
  40. */
  41. private function getBlobClient() {
  42. if (!$this->blobClient) {
  43. $protocol = $this->endpoint ? substr($this->endpoint, 0, strpos($this->endpoint, ':')) : 'https';
  44. $connectionString = 'DefaultEndpointsProtocol=' . $protocol . ';AccountName=' . $this->accountName . ';AccountKey=' . $this->accountKey;
  45. if ($this->endpoint) {
  46. $connectionString .= ';BlobEndpoint=' . $this->endpoint;
  47. }
  48. $this->blobClient = BlobRestProxy::createBlobService($connectionString);
  49. if ($this->autoCreate) {
  50. try {
  51. $this->blobClient->createContainer($this->containerName);
  52. } catch (ServiceException $e) {
  53. if ($e->getCode() === 409) {
  54. // already exists
  55. } else {
  56. throw $e;
  57. }
  58. }
  59. }
  60. }
  61. return $this->blobClient;
  62. }
  63. /**
  64. * @return string the container or bucket name where objects are stored
  65. */
  66. public function getStorageId() {
  67. return 'azure::blob::' . $this->containerName;
  68. }
  69. /**
  70. * @param string $urn the unified resource name used to identify the object
  71. * @return resource stream with the read data
  72. * @throws \Exception when something goes wrong, message will be logged
  73. */
  74. public function readObject($urn) {
  75. $blob = $this->getBlobClient()->getBlob($this->containerName, $urn);
  76. return $blob->getContentStream();
  77. }
  78. public function writeObject($urn, $stream, ?string $mimetype = null) {
  79. $options = new CreateBlockBlobOptions();
  80. if ($mimetype) {
  81. $options->setContentType($mimetype);
  82. }
  83. $this->getBlobClient()->createBlockBlob($this->containerName, $urn, $stream, $options);
  84. }
  85. /**
  86. * @param string $urn the unified resource name used to identify the object
  87. * @return void
  88. * @throws \Exception when something goes wrong, message will be logged
  89. */
  90. public function deleteObject($urn) {
  91. $this->getBlobClient()->deleteBlob($this->containerName, $urn);
  92. }
  93. public function objectExists($urn) {
  94. try {
  95. $this->getBlobClient()->getBlobMetadata($this->containerName, $urn);
  96. return true;
  97. } catch (ServiceException $e) {
  98. if ($e->getCode() === 404) {
  99. return false;
  100. } else {
  101. throw $e;
  102. }
  103. }
  104. }
  105. public function copyObject($from, $to) {
  106. $this->getBlobClient()->copyBlob($this->containerName, $to, $this->containerName, $from);
  107. }
  108. }