1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gaufrette\Adapter; |
4
|
|
|
|
5
|
|
|
use Gaufrette\Adapter; |
6
|
|
|
use Gaufrette\Util; |
7
|
|
|
use Gaufrette\Adapter\AzureBlobStorage\BlobProxyFactoryInterface; |
8
|
|
|
use MicrosoftAzure\Storage\Blob\Models\Blob; |
9
|
|
|
use MicrosoftAzure\Storage\Blob\Models\Container; |
10
|
|
|
use MicrosoftAzure\Storage\Blob\Models\CreateBlobOptions; |
11
|
|
|
use MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions; |
12
|
|
|
use MicrosoftAzure\Storage\Blob\Models\DeleteContainerOptions; |
13
|
|
|
use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions; |
14
|
|
|
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Microsoft Azure Blob Storage adapter. |
18
|
|
|
* |
19
|
|
|
* @author Luciano Mammino <[email protected]> |
20
|
|
|
* @author Paweł Czyżewski <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class AzureBlobStorage implements Adapter, |
|
|
|
|
23
|
|
|
MetadataSupporter |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Error constants. |
27
|
|
|
*/ |
28
|
|
|
const ERROR_CONTAINER_ALREADY_EXISTS = 'ContainerAlreadyExists'; |
29
|
|
|
const ERROR_CONTAINER_NOT_FOUND = 'ContainerNotFound'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var AzureBlobStorage\BlobProxyFactoryInterface |
33
|
|
|
*/ |
34
|
|
|
protected $blobProxyFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $containerName; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $detectContentType; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var \MicrosoftAzure\Storage\Blob\Internal\IBlob |
48
|
|
|
*/ |
49
|
|
|
protected $blobProxy; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
protected $multiContainerMode = false; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var CreateContainerOptions |
58
|
|
|
*/ |
59
|
|
|
protected $createContainerOptions; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param AzureBlobStorage\BlobProxyFactoryInterface $blobProxyFactory |
63
|
|
|
* @param string|null $containerName |
64
|
|
|
* @param bool $create |
65
|
|
|
* @param bool $detectContentType |
66
|
|
|
* |
67
|
|
|
* @throws \RuntimeException |
68
|
|
|
*/ |
69
|
|
|
public function __construct(BlobProxyFactoryInterface $blobProxyFactory, $containerName = null, $create = false, $detectContentType = true) |
70
|
|
|
{ |
71
|
|
|
$this->blobProxyFactory = $blobProxyFactory; |
72
|
|
|
$this->containerName = $containerName; |
73
|
|
|
$this->detectContentType = $detectContentType; |
74
|
|
|
if (null === $containerName) { |
75
|
|
|
$this->multiContainerMode = true; |
76
|
|
|
} elseif ($create) { |
77
|
|
|
$this->createContainer($containerName); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return CreateContainerOptions |
83
|
|
|
*/ |
84
|
|
|
public function getCreateContainerOptions() |
85
|
|
|
{ |
86
|
|
|
return $this->createContainerOptions; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param CreateContainerOptions $options |
91
|
|
|
*/ |
92
|
|
|
public function setCreateContainerOptions(CreateContainerOptions $options) |
93
|
|
|
{ |
94
|
|
|
$this->createContainerOptions = $options; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Creates a new container. |
99
|
|
|
* |
100
|
|
|
* @param string $containerName |
101
|
|
|
* @param \MicrosoftAzure\Storage\Blob\Models\CreateContainerOptions $options |
102
|
|
|
* |
103
|
|
|
* @throws \RuntimeException if cannot create the container |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public function createContainer($containerName, CreateContainerOptions $options = null) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$this->init(); |
108
|
|
|
|
109
|
|
|
if (null === $options) { |
110
|
|
|
$options = $this->getCreateContainerOptions(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
try { |
114
|
|
|
$this->blobProxy->createContainer($containerName, $options); |
115
|
|
|
} catch (ServiceException $e) { |
116
|
|
|
$errorCode = $this->getErrorCodeFromServiceException($e); |
117
|
|
|
|
118
|
|
|
if ($errorCode !== self::ERROR_CONTAINER_ALREADY_EXISTS) { |
119
|
|
|
throw new \RuntimeException(sprintf( |
120
|
|
|
'Failed to create the configured container "%s": %s (%s).', |
121
|
|
|
$containerName, |
122
|
|
|
$e->getErrorText(), |
123
|
|
|
$errorCode |
124
|
|
|
)); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Deletes a container. |
131
|
|
|
* |
132
|
|
|
* @param string $containerName |
133
|
|
|
* @param DeleteContainerOptions $options |
134
|
|
|
* |
135
|
|
|
* @throws \RuntimeException if cannot delete the container |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public function deleteContainer($containerName, DeleteContainerOptions $options = null) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$this->init(); |
140
|
|
|
|
141
|
|
|
try { |
142
|
|
|
$this->blobProxy->deleteContainer($containerName, $options); |
143
|
|
|
} catch (ServiceException $e) { |
144
|
|
|
$errorCode = $this->getErrorCodeFromServiceException($e); |
145
|
|
|
|
146
|
|
|
if ($errorCode !== self::ERROR_CONTAINER_NOT_FOUND) { |
147
|
|
|
throw new \RuntimeException(sprintf( |
148
|
|
|
'Failed to delete the configured container "%s": %s (%s).', |
149
|
|
|
$containerName, |
150
|
|
|
$e->getErrorText(), |
151
|
|
|
$errorCode |
152
|
|
|
), $e->getCode()); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
* @throws \RuntimeException |
160
|
|
|
* @throws \InvalidArgumentException |
161
|
|
|
*/ |
162
|
|
View Code Duplication |
public function read($key) |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
$this->init(); |
165
|
|
|
list($containerName, $key) = $this->tokenizeKey($key); |
166
|
|
|
|
167
|
|
|
try { |
168
|
|
|
$blob = $this->blobProxy->getBlob($containerName, $key); |
169
|
|
|
|
170
|
|
|
return stream_get_contents($blob->getContentStream()); |
171
|
|
|
} catch (ServiceException $e) { |
172
|
|
|
$this->failIfContainerNotFound($e, sprintf('read key "%s"', $key), $containerName); |
173
|
|
|
|
174
|
|
|
return false; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* {@inheritdoc} |
180
|
|
|
* @throws \RuntimeException |
181
|
|
|
* @throws \InvalidArgumentException |
182
|
|
|
*/ |
183
|
|
|
public function write($key, $content) |
184
|
|
|
{ |
185
|
|
|
$this->init(); |
186
|
|
|
list($containerName, $key) = $this->tokenizeKey($key); |
187
|
|
|
|
188
|
|
|
$options = new CreateBlobOptions(); |
189
|
|
|
|
190
|
|
|
if ($this->detectContentType) { |
191
|
|
|
$contentType = $this->guessContentType($content); |
192
|
|
|
|
193
|
|
|
$options->setContentType($contentType); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
try { |
197
|
|
|
if ($this->multiContainerMode) { |
198
|
|
|
$this->createContainer($containerName); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$this->blobProxy->createBlockBlob($containerName, $key, $content, $options); |
202
|
|
|
} catch (ServiceException $e) { |
203
|
|
|
$this->failIfContainerNotFound($e, sprintf('write content for key "%s"', $key), $containerName); |
204
|
|
|
|
205
|
|
|
return false; |
206
|
|
|
} |
207
|
|
|
if (is_resource($content)) { |
208
|
|
|
return Util\Size::fromResource($content); |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return Util\Size::fromContent($content); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* {@inheritdoc} |
216
|
|
|
* @throws \RuntimeException |
217
|
|
|
* @throws \InvalidArgumentException |
218
|
|
|
*/ |
219
|
|
|
public function exists($key) |
220
|
|
|
{ |
221
|
|
|
$this->init(); |
222
|
|
|
list($containerName, $key) = $this->tokenizeKey($key); |
223
|
|
|
|
224
|
|
|
$listBlobsOptions = new ListBlobsOptions(); |
225
|
|
|
$listBlobsOptions->setPrefix($key); |
226
|
|
|
|
227
|
|
|
try { |
228
|
|
|
$blobsList = $this->blobProxy->listBlobs($containerName, $listBlobsOptions); |
229
|
|
|
|
230
|
|
|
foreach ($blobsList->getBlobs() as $blob) { |
231
|
|
|
if ($key === $blob->getName()) { |
232
|
|
|
return true; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
} catch (ServiceException $e) { |
236
|
|
|
$errorCode = $this->getErrorCodeFromServiceException($e); |
237
|
|
|
if ($this->multiContainerMode && self::ERROR_CONTAINER_NOT_FOUND === $errorCode) { |
238
|
|
|
return false; |
239
|
|
|
} |
240
|
|
|
$this->failIfContainerNotFound($e, 'check if key exists', $containerName); |
241
|
|
|
|
242
|
|
|
throw new \RuntimeException(sprintf( |
243
|
|
|
'Failed to check if key "%s" exists in container "%s": %s (%s).', |
244
|
|
|
$key, |
245
|
|
|
$containerName, |
246
|
|
|
$e->getErrorText(), |
247
|
|
|
$errorCode |
248
|
|
|
), $e->getCode()); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return false; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* {@inheritdoc} |
256
|
|
|
* @throws \RuntimeException |
257
|
|
|
*/ |
258
|
|
|
public function keys() |
259
|
|
|
{ |
260
|
|
|
$this->init(); |
261
|
|
|
|
262
|
|
|
try { |
263
|
|
|
if ($this->multiContainerMode) { |
264
|
|
|
$containersList = $this->blobProxy->listContainers(); |
265
|
|
|
return call_user_func_array('array_merge', array_map( |
266
|
|
|
function(Container $container) { |
267
|
|
|
$containerName = $container->getName(); |
268
|
|
|
return $this->fetchBlobs($containerName, $containerName); |
269
|
|
|
}, |
270
|
|
|
$containersList->getContainers() |
271
|
|
|
)); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
return $this->fetchBlobs($this->containerName); |
275
|
|
|
} catch (ServiceException $e) { |
276
|
|
|
$this->failIfContainerNotFound($e, 'retrieve keys', $this->containerName); |
277
|
|
|
$errorCode = $this->getErrorCodeFromServiceException($e); |
278
|
|
|
|
279
|
|
|
throw new \RuntimeException(sprintf( |
280
|
|
|
'Failed to list keys for the container "%s": %s (%s).', |
281
|
|
|
$this->containerName, |
282
|
|
|
$e->getErrorText(), |
283
|
|
|
$errorCode |
284
|
|
|
), $e->getCode()); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* {@inheritdoc} |
290
|
|
|
* @throws \RuntimeException |
291
|
|
|
* @throws \InvalidArgumentException |
292
|
|
|
*/ |
293
|
|
View Code Duplication |
public function mtime($key) |
|
|
|
|
294
|
|
|
{ |
295
|
|
|
$this->init(); |
296
|
|
|
list($containerName, $key) = $this->tokenizeKey($key); |
297
|
|
|
|
298
|
|
|
try { |
299
|
|
|
$properties = $this->blobProxy->getBlobProperties($containerName, $key); |
300
|
|
|
|
301
|
|
|
return $properties->getProperties()->getLastModified()->getTimestamp(); |
302
|
|
|
} catch (ServiceException $e) { |
303
|
|
|
$this->failIfContainerNotFound($e, sprintf('read mtime for key "%s"', $key), $containerName); |
304
|
|
|
|
305
|
|
|
return false; |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* {@inheritdoc} |
311
|
|
|
* @throws \RuntimeException |
312
|
|
|
* @throws \InvalidArgumentException |
313
|
|
|
*/ |
314
|
|
View Code Duplication |
public function delete($key) |
|
|
|
|
315
|
|
|
{ |
316
|
|
|
$this->init(); |
317
|
|
|
list($containerName, $key) = $this->tokenizeKey($key); |
318
|
|
|
|
319
|
|
|
try { |
320
|
|
|
$this->blobProxy->deleteBlob($containerName, $key); |
321
|
|
|
|
322
|
|
|
return true; |
323
|
|
|
} catch (ServiceException $e) { |
324
|
|
|
$this->failIfContainerNotFound($e, sprintf('delete key "%s"', $key), $containerName); |
325
|
|
|
|
326
|
|
|
return false; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* {@inheritdoc} |
332
|
|
|
* @throws \RuntimeException |
333
|
|
|
* @throws \InvalidArgumentException |
334
|
|
|
*/ |
335
|
|
|
public function rename($sourceKey, $targetKey) |
336
|
|
|
{ |
337
|
|
|
$this->init(); |
338
|
|
|
|
339
|
|
|
list($sourceContainerName, $sourceKey) = $this->tokenizeKey($sourceKey); |
340
|
|
|
list($targetContainerName, $targetKey) = $this->tokenizeKey($targetKey); |
341
|
|
|
|
342
|
|
|
try { |
343
|
|
|
if ($this->multiContainerMode) { |
344
|
|
|
$this->createContainer($targetContainerName); |
345
|
|
|
} |
346
|
|
|
$this->blobProxy->copyBlob($targetContainerName, $targetKey, $sourceContainerName, $sourceKey); |
347
|
|
|
$this->blobProxy->deleteBlob($sourceContainerName, $sourceKey); |
348
|
|
|
|
349
|
|
|
return true; |
350
|
|
|
} catch (ServiceException $e) { |
351
|
|
|
$this->failIfContainerNotFound($e, sprintf('rename key "%s"', $sourceKey), $sourceContainerName); |
352
|
|
|
|
353
|
|
|
return false; |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* {@inheritdoc} |
359
|
|
|
*/ |
360
|
|
|
public function isDirectory($key) |
361
|
|
|
{ |
362
|
|
|
// Windows Azure Blob Storage does not support directories |
363
|
|
|
return false; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* {@inheritdoc} |
368
|
|
|
* @throws \RuntimeException |
369
|
|
|
* @throws \InvalidArgumentException |
370
|
|
|
*/ |
371
|
|
View Code Duplication |
public function setMetadata($key, $content) |
|
|
|
|
372
|
|
|
{ |
373
|
|
|
$this->init(); |
374
|
|
|
list($containerName, $key) = $this->tokenizeKey($key); |
375
|
|
|
|
376
|
|
|
try { |
377
|
|
|
$this->blobProxy->setBlobMetadata($containerName, $key, $content); |
378
|
|
|
} catch (ServiceException $e) { |
379
|
|
|
$errorCode = $this->getErrorCodeFromServiceException($e); |
380
|
|
|
|
381
|
|
|
throw new \RuntimeException(sprintf( |
382
|
|
|
'Failed to set metadata for blob "%s" in container "%s": %s (%s).', |
383
|
|
|
$key, |
384
|
|
|
$containerName, |
385
|
|
|
$e->getErrorText(), |
386
|
|
|
$errorCode |
387
|
|
|
), $e->getCode()); |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* {@inheritdoc} |
393
|
|
|
* @throws \RuntimeException |
394
|
|
|
* @throws \InvalidArgumentException |
395
|
|
|
*/ |
396
|
|
View Code Duplication |
public function getMetadata($key) |
|
|
|
|
397
|
|
|
{ |
398
|
|
|
$this->init(); |
399
|
|
|
list($containerName, $key) = $this->tokenizeKey($key); |
400
|
|
|
|
401
|
|
|
try { |
402
|
|
|
$properties = $this->blobProxy->getBlobProperties($containerName, $key); |
403
|
|
|
|
404
|
|
|
return $properties->getMetadata(); |
405
|
|
|
} catch (ServiceException $e) { |
406
|
|
|
$errorCode = $this->getErrorCodeFromServiceException($e); |
407
|
|
|
|
408
|
|
|
throw new \RuntimeException(sprintf( |
409
|
|
|
'Failed to get metadata for blob "%s" in container "%s": %s (%s).', |
410
|
|
|
$key, |
411
|
|
|
$containerName, |
412
|
|
|
$e->getErrorText(), |
413
|
|
|
$errorCode |
414
|
|
|
), $e->getCode()); |
415
|
|
|
} |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Lazy initialization, automatically called when some method is called after construction. |
420
|
|
|
*/ |
421
|
|
|
protected function init() |
422
|
|
|
{ |
423
|
|
|
if ($this->blobProxy === null) { |
424
|
|
|
$this->blobProxy = $this->blobProxyFactory->create(); |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Throws a runtime exception if a give ServiceException derived from a "container not found" error. |
430
|
|
|
* |
431
|
|
|
* @param ServiceException $exception |
432
|
|
|
* @param string $action |
433
|
|
|
* @param string $containerName |
434
|
|
|
* |
435
|
|
|
* @throws \RuntimeException |
436
|
|
|
*/ |
437
|
|
|
protected function failIfContainerNotFound(ServiceException $exception, $action, $containerName) |
438
|
|
|
{ |
439
|
|
|
$errorCode = $this->getErrorCodeFromServiceException($exception); |
440
|
|
|
|
441
|
|
|
if ($errorCode === self::ERROR_CONTAINER_NOT_FOUND) { |
442
|
|
|
throw new \RuntimeException(sprintf( |
443
|
|
|
'Failed to %s: container "%s" not found.', |
444
|
|
|
$action, |
445
|
|
|
$containerName |
446
|
|
|
), $exception->getCode()); |
447
|
|
|
} |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* Extracts the error code from a service exception. |
452
|
|
|
* |
453
|
|
|
* @param ServiceException $exception |
454
|
|
|
* |
455
|
|
|
* @return string |
456
|
|
|
*/ |
457
|
|
|
protected function getErrorCodeFromServiceException(ServiceException $exception) |
458
|
|
|
{ |
459
|
|
|
$xml = @simplexml_load_string($exception->getResponse()->getBody()); |
460
|
|
|
|
461
|
|
|
if ($xml && isset($xml->Code)) { |
462
|
|
|
return (string) $xml->Code; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
return $exception->getErrorText(); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* @param string|resource $content |
470
|
|
|
* |
471
|
|
|
* @return string |
472
|
|
|
*/ |
473
|
|
View Code Duplication |
private function guessContentType($content) |
|
|
|
|
474
|
|
|
{ |
475
|
|
|
$fileInfo = new \finfo(FILEINFO_MIME_TYPE); |
476
|
|
|
|
477
|
|
|
if (is_resource($content)) { |
478
|
|
|
return $fileInfo->file(stream_get_meta_data($content)['uri']); |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
return $fileInfo->buffer($content); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* @param string $key |
486
|
|
|
* |
487
|
|
|
* @return array |
488
|
|
|
* @throws \InvalidArgumentException |
489
|
|
|
*/ |
490
|
|
|
private function tokenizeKey($key) |
491
|
|
|
{ |
492
|
|
|
$containerName = $this->containerName; |
493
|
|
|
if (false === $this->multiContainerMode) { |
494
|
|
|
return [$containerName, $key]; |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
if (false === ($index = strpos($key, '/'))) { |
498
|
|
|
throw new \InvalidArgumentException(sprintf( |
499
|
|
|
'Failed to establish container name from key "%s", container name is required in multi-container mode', |
500
|
|
|
$key |
501
|
|
|
)); |
502
|
|
|
} |
503
|
|
|
$containerName = substr($key, 0, $index); |
504
|
|
|
$key = substr($key, $index + 1); |
505
|
|
|
|
506
|
|
|
return [$containerName, $key]; |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
/** |
510
|
|
|
* @param string $containerName |
511
|
|
|
* @param null $prefix |
512
|
|
|
* |
513
|
|
|
* @return array |
514
|
|
|
*/ |
515
|
|
|
private function fetchBlobs($containerName, $prefix = null) |
516
|
|
|
{ |
517
|
|
|
$blobList = $this->blobProxy->listBlobs($containerName); |
518
|
|
|
return array_map( |
519
|
|
|
function (Blob $blob) use ($prefix) { |
520
|
|
|
$name = $blob->getName(); |
521
|
|
|
if (null !== $prefix) { |
522
|
|
|
$name = $prefix .'/'. $name; |
523
|
|
|
} |
524
|
|
|
return $name; |
525
|
|
|
}, |
526
|
|
|
$blobList->getBlobs() |
527
|
|
|
); |
528
|
|
|
} |
529
|
|
|
} |
530
|
|
|
|