Azure.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Files\ObjectStore;
  24. use MicrosoftAzure\Storage\Blob\BlobRestProxy;
  25. use MicrosoftAzure\Storage\Blob\Models\CreateBlockBlobOptions;
  26. use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
  27. use OCP\Files\ObjectStore\IObjectStore;
  28. class Azure implements IObjectStore {
  29. /** @var string */
  30. private $containerName;
  31. /** @var string */
  32. private $accountName;
  33. /** @var string */
  34. private $accountKey;
  35. /** @var BlobRestProxy|null */
  36. private $blobClient = null;
  37. /** @var string|null */
  38. private $endpoint = null;
  39. /** @var bool */
  40. private $autoCreate = false;
  41. /**
  42. * @param array $parameters
  43. */
  44. public function __construct($parameters) {
  45. $this->containerName = $parameters['container'];
  46. $this->accountName = $parameters['account_name'];
  47. $this->accountKey = $parameters['account_key'];
  48. if (isset($parameters['endpoint'])) {
  49. $this->endpoint = $parameters['endpoint'];
  50. }
  51. if (isset($parameters['autocreate'])) {
  52. $this->autoCreate = $parameters['autocreate'];
  53. }
  54. }
  55. /**
  56. * @return BlobRestProxy
  57. */
  58. private function getBlobClient() {
  59. if (!$this->blobClient) {
  60. $protocol = $this->endpoint ? substr($this->endpoint, 0, strpos($this->endpoint, ':')) : 'https';
  61. $connectionString = "DefaultEndpointsProtocol=" . $protocol . ";AccountName=" . $this->accountName . ";AccountKey=" . $this->accountKey;
  62. if ($this->endpoint) {
  63. $connectionString .= ';BlobEndpoint=' . $this->endpoint;
  64. }
  65. $this->blobClient = BlobRestProxy::createBlobService($connectionString);
  66. if ($this->autoCreate) {
  67. try {
  68. $this->blobClient->createContainer($this->containerName);
  69. } catch (ServiceException $e) {
  70. if ($e->getCode() === 409) {
  71. // already exists
  72. } else {
  73. throw $e;
  74. }
  75. }
  76. }
  77. }
  78. return $this->blobClient;
  79. }
  80. /**
  81. * @return string the container or bucket name where objects are stored
  82. */
  83. public function getStorageId() {
  84. return 'azure::blob::' . $this->containerName;
  85. }
  86. /**
  87. * @param string $urn the unified resource name used to identify the object
  88. * @return resource stream with the read data
  89. * @throws \Exception when something goes wrong, message will be logged
  90. */
  91. public function readObject($urn) {
  92. $blob = $this->getBlobClient()->getBlob($this->containerName, $urn);
  93. return $blob->getContentStream();
  94. }
  95. public function writeObject($urn, $stream, string $mimetype = null) {
  96. $options = new CreateBlockBlobOptions();
  97. if ($mimetype) {
  98. $options->setContentType($mimetype);
  99. }
  100. $this->getBlobClient()->createBlockBlob($this->containerName, $urn, $stream, $options);
  101. }
  102. /**
  103. * @param string $urn the unified resource name used to identify the object
  104. * @return void
  105. * @throws \Exception when something goes wrong, message will be logged
  106. */
  107. public function deleteObject($urn) {
  108. $this->getBlobClient()->deleteBlob($this->containerName, $urn);
  109. }
  110. public function objectExists($urn) {
  111. try {
  112. $this->getBlobClient()->getBlobMetadata($this->containerName, $urn);
  113. return true;
  114. } catch (ServiceException $e) {
  115. if ($e->getCode() === 404) {
  116. return false;
  117. } else {
  118. throw $e;
  119. }
  120. }
  121. }
  122. public function copyObject($from, $to) {
  123. $this->getBlobClient()->copyBlob($this->containerName, $to, $this->containerName, $from);
  124. }
  125. }