|
9 | 9 | use Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface;
|
10 | 10 | use MicrosoftAzure\Storage\Blob\Models\Blob;
|
11 | 11 | use MicrosoftAzure\Storage\Blob\Models\CreateBlockBlobOptions;
|
12 |
| -use MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions; |
13 | 12 | use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
|
14 | 13 |
|
15 | 14 | /**
|
@@ -46,104 +45,18 @@ class AzureBlobStorage implements Adapter, MetadataSupporter, SizeCalculator, Ch
|
46 | 45 | */
|
47 | 46 | protected $blobProxy;
|
48 | 47 |
|
49 |
| - /** |
50 |
| - * @var bool |
51 |
| - */ |
52 |
| - protected $multiContainerMode = false; |
53 |
| - |
54 |
| - /** |
55 |
| - * @var CreateContainerOptions |
56 |
| - */ |
57 |
| - protected $createContainerOptions; |
58 |
| - |
59 | 48 | /**
|
60 | 49 | * @param AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory
|
61 |
| - * @param string|null $containerName |
62 |
| - * @param bool $create |
| 50 | + * @param string $containerName |
63 | 51 | * @param bool $detectContentType
|
64 | 52 | *
|
65 | 53 | * @throws \RuntimeException
|
66 | 54 | */
|
67 |
| - public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $containerName = null, $create = false, $detectContentType = true) |
| 55 | + public function __construct(BlobProxyFactoryInterface $blobProxyFactory, string $containerName, bool $detectContentType = true) |
68 | 56 | {
|
69 | 57 | $this->blobProxyFactory = $blobProxyFactory;
|
70 | 58 | $this->containerName = $containerName;
|
71 | 59 | $this->detectContentType = $detectContentType;
|
72 |
| - |
73 |
| - if (null === $containerName) { |
74 |
| - $this->multiContainerMode = true; |
75 |
| - } elseif ($create) { |
76 |
| - $this->createContainer($containerName); |
77 |
| - } |
78 |
| - } |
79 |
| - |
80 |
| - /** |
81 |
| - * @return CreateContainerOptions |
82 |
| - */ |
83 |
| - public function getCreateContainerOptions() |
84 |
| - { |
85 |
| - return $this->createContainerOptions; |
86 |
| - } |
87 |
| - |
88 |
| - /** |
89 |
| - * @param CreateContainerOptions $options |
90 |
| - */ |
91 |
| - public function setCreateContainerOptions(CreateContainerOptions $options) |
92 |
| - { |
93 |
| - $this->createContainerOptions = $options; |
94 |
| - } |
95 |
| - |
96 |
| - /** |
97 |
| - * Creates a new container. |
98 |
| - * |
99 |
| - * @param string $containerName |
100 |
| - * @param \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions $options |
101 |
| - * |
102 |
| - * @throws StorageFailure if cannot create the container |
103 |
| - */ |
104 |
| - public function createContainer($containerName, CreateContainerOptions $options = null) |
105 |
| - { |
106 |
| - $this->init(); |
107 |
| - |
108 |
| - if (null === $options) { |
109 |
| - $options = $this->getCreateContainerOptions(); |
110 |
| - } |
111 |
| - |
112 |
| - try { |
113 |
| - $this->blobProxy->createContainer($containerName, $options); |
114 |
| - } catch (ServiceException $e) { |
115 |
| - $errorCode = $this->getErrorCodeFromServiceException($e); |
116 |
| - |
117 |
| - // We don't care if the container was created between check and creation attempt |
118 |
| - // it might be due to a parallel execution creating it. |
119 |
| - if ($errorCode !== self::ERROR_CONTAINER_ALREADY_EXISTS) { |
120 |
| - throw StorageFailure::unexpectedFailure('createContainer', [ |
121 |
| - 'containerName' => $containerName, |
122 |
| - 'options' => $options, |
123 |
| - ], $e); |
124 |
| - } |
125 |
| - } |
126 |
| - } |
127 |
| - |
128 |
| - /** |
129 |
| - * Deletes a container. |
130 |
| - * |
131 |
| - * @param string $containerName |
132 |
| - * @param DeleteContainerOptions $options |
133 |
| - * |
134 |
| - * @throws StorageFailure if cannot delete the container |
135 |
| - */ |
136 |
| - public function deleteContainer($containerName, DeleteContainerOptions $options = null) |
137 |
| - { |
138 |
| - $this->init(); |
139 |
| - |
140 |
| - try { |
141 |
| - $this->blobProxy->deleteContainer($containerName, $options); |
142 |
| - } catch (ServiceException $e) { |
143 |
| - throw StorageFailure::unexpectedFailure('deleteContainer', [ |
144 |
| - 'containerName' => $containerName, |
145 |
| - ], $e); |
146 |
| - } |
147 | 60 | }
|
148 | 61 |
|
149 | 62 | /**
|
@@ -187,10 +100,6 @@ public function write($key, $content)
|
187 | 100 | }
|
188 | 101 |
|
189 | 102 | try {
|
190 |
| - if ($this->multiContainerMode) { |
191 |
| - $this->createContainer($containerName); |
192 |
| - } |
193 |
| - |
194 | 103 | $this->blobProxy->createBlockBlob($containerName, $key, $content, $options);
|
195 | 104 | } catch (ServiceException $e) {
|
196 | 105 | throw StorageFailure::unexpectedFailure('write', [
|
@@ -231,76 +140,10 @@ public function keys()
|
231 | 140 | {
|
232 | 141 | $this->init();
|
233 | 142 |
|
234 |
| - if ($this->multiContainerMode) { |
235 |
| - return $this->keysForMultiContainerMode(); |
236 |
| - } |
237 |
| - |
238 |
| - return $this->keysForSingleContainerMode(); |
239 |
| - } |
240 |
| - |
241 |
| - /** |
242 |
| - * List objects stored when the adapter is used in multi container mode. |
243 |
| - * |
244 |
| - * @return array |
245 |
| - * |
246 |
| - * @throws \RuntimeException |
247 |
| - */ |
248 |
| - private function keysForMultiContainerMode() |
249 |
| - { |
250 |
| - try { |
251 |
| - $containersList = $this->blobProxy->listContainers()->getContainers(); |
252 |
| - $lists = []; |
253 |
| - |
254 |
| - foreach ($containersList as $container) { |
255 |
| - $lists[] = $this->fetchContainerKeysAndIgnore404($container->getName()); |
256 |
| - } |
257 |
| - |
258 |
| - return !empty($lists) ? array_merge(...$lists) : []; |
259 |
| - } catch (ServiceException $e) { |
260 |
| - throw StorageFailure::unexpectedFailure('keys', [ |
261 |
| - 'multiContainerMode' => $this->multiContainerMode, |
262 |
| - 'containerName' => $this->containerName, |
263 |
| - ], $e); |
264 |
| - } |
265 |
| - } |
266 |
| - |
267 |
| - /** |
268 |
| - * List the keys in a container and ignore any 404 error. |
269 |
| - * |
270 |
| - * This prevent race conditions happening when a container is deleted after calling listContainers() and |
271 |
| - * before the container keys are fetched. |
272 |
| - * |
273 |
| - * @param string $containerName |
274 |
| - * |
275 |
| - * @return array |
276 |
| - */ |
277 |
| - private function fetchContainerKeysAndIgnore404($containerName) |
278 |
| - { |
279 |
| - try { |
280 |
| - return $this->fetchBlobs($containerName, $containerName); |
281 |
| - } catch (ServiceException $e) { |
282 |
| - if ($e->getResponse()->getStatusCode() === 404) { |
283 |
| - return []; |
284 |
| - } |
285 |
| - |
286 |
| - throw $e; |
287 |
| - } |
288 |
| - } |
289 |
| - |
290 |
| - /** |
291 |
| - * List objects stored when the adapter is not used in multi container mode. |
292 |
| - * |
293 |
| - * @return array |
294 |
| - * |
295 |
| - * @throws \RuntimeException |
296 |
| - */ |
297 |
| - private function keysForSingleContainerMode() |
298 |
| - { |
299 | 143 | try {
|
300 | 144 | return $this->fetchBlobs($this->containerName);
|
301 | 145 | } catch (ServiceException $e) {
|
302 | 146 | throw StorageFailure::unexpectedFailure('keys', [
|
303 |
| - 'multiContainerMode' => $this->multiContainerMode, |
304 | 147 | 'containerName' => $this->containerName,
|
305 | 148 | ], $e);
|
306 | 149 | }
|
@@ -413,10 +256,6 @@ public function rename($sourceKey, $targetKey)
|
413 | 256 | list($targetContainerName, $targetKey) = $this->tokenizeKey($targetKey);
|
414 | 257 |
|
415 | 258 | try {
|
416 |
| - if ($this->multiContainerMode) { |
417 |
| - $this->createContainer($targetContainerName); |
418 |
| - } |
419 |
| - |
420 | 259 | $this->blobProxy->copyBlob($targetContainerName, $targetKey, $sourceContainerName, $sourceKey);
|
421 | 260 | $this->blobProxy->deleteBlob($sourceContainerName, $sourceKey);
|
422 | 261 | } catch (ServiceException $e) {
|
@@ -535,22 +374,7 @@ private function guessContentType($content)
|
535 | 374 | */
|
536 | 375 | private function tokenizeKey($key)
|
537 | 376 | {
|
538 |
| - $containerName = $this->containerName; |
539 |
| - if (false === $this->multiContainerMode) { |
540 |
| - return [$containerName, $key]; |
541 |
| - } |
542 |
| - |
543 |
| - if (false === ($index = strpos($key, '/'))) { |
544 |
| - throw new InvalidKey(sprintf( |
545 |
| - 'Failed to establish container name from key "%s", container name is required in multi-container mode', |
546 |
| - $key |
547 |
| - )); |
548 |
| - } |
549 |
| - |
550 |
| - $containerName = substr($key, 0, $index); |
551 |
| - $key = substr($key, $index + 1); |
552 |
| - |
553 |
| - return [$containerName, $key]; |
| 377 | + return [$this->containerName, $key]; |
554 | 378 | }
|
555 | 379 |
|
556 | 380 | /**
|
|
0 commit comments