Azure.php 3.6 KB

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