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->setBlobContentType($contentType); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
try { |
197
|
|
|
$this->createContainer($containerName); |
198
|
|
|
|
199
|
|
|
$this->blobProxy->createBlockBlob($containerName, $key, $content, $options); |
200
|
|
|
} catch (ServiceException $e) { |
|
|
|
|
201
|
|
|
$this->failIfContainerNotFound($e, sprintf('write content for key "%s"', $key), $containerName); |
202
|
|
|
|
203
|
|
|
return false; |
204
|
|
|
} |
205
|
|
|
if (is_resource($content)) { |
206
|
|
|
return Util\Size::fromResource($content); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return Util\Size::fromContent($content); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* {@inheritdoc} |
214
|
|
|
* @throws \RuntimeException |
215
|
|
|
* @throws \InvalidArgumentException |
216
|
|
|
*/ |
217
|
|
|
public function exists($key) |
218
|
|
|
{ |
219
|
|
|
$this->init(); |
220
|
|
|
list($containerName, $key) = $this->tokenizeKey($key); |
221
|
|
|
|
222
|
|
|
$listBlobsOptions = new ListBlobsOptions(); |
223
|
|
|
$listBlobsOptions->setPrefix($key); |
224
|
|
|
|
225
|
|
|
try { |
226
|
|
|
$blobsList = $this->blobProxy->listBlobs($containerName, $listBlobsOptions); |
227
|
|
|
|
228
|
|
|
foreach ($blobsList->getBlobs() as $blob) { |
229
|
|
|
if ($key === $blob->getName()) { |
230
|
|
|
return true; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
} catch (ServiceException $e) { |
|
|
|
|
234
|
|
|
$errorCode = $this->getErrorCodeFromServiceException($e); |
235
|
|
|
if ($this->multiContainerMode && self::ERROR_CONTAINER_NOT_FOUND === $errorCode) { |
236
|
|
|
return false; |
237
|
|
|
} |
238
|
|
|
$this->failIfContainerNotFound($e, 'check if key exists', $containerName); |
239
|
|
|
|
240
|
|
|
throw new \RuntimeException(sprintf( |
241
|
|
|
'Failed to check if key "%s" exists in container "%s": %s (%s).', |
242
|
|
|
$key, |
243
|
|
|
$containerName, |
244
|
|
|
$e->getErrorText(), |
245
|
|
|
$errorCode |
246
|
|
|
), $e->getCode()); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return false; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* {@inheritdoc} |
254
|
|
|
* @throws \RuntimeException |
255
|
|
|
*/ |
256
|
|
|
public function keys() |
257
|
|
|
{ |
258
|
|
|
$this->init(); |
259
|
|
|
|
260
|
|
|
try { |
261
|
|
|
if ($this->multiContainerMode) { |
262
|
|
|
$containersList = $this->blobProxy->listContainers(); |
263
|
|
|
return array_map( |
264
|
|
|
function(Container $container) { |
265
|
|
|
return $container->getName(); |
266
|
|
|
}, $containersList->getContainers() |
267
|
|
|
); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$blobList = $this->blobProxy->listBlobs($this->containerName); |
271
|
|
|
|
272
|
|
|
return array_map( |
273
|
|
|
function (Blob $blob) { |
274
|
|
|
return $blob->getName(); |
275
|
|
|
}, |
276
|
|
|
$blobList->getBlobs() |
277
|
|
|
); |
278
|
|
|
} catch (ServiceException $e) { |
|
|
|
|
279
|
|
|
$this->failIfContainerNotFound($e, 'retrieve keys', $this->containerName); |
280
|
|
|
$errorCode = $this->getErrorCodeFromServiceException($e); |
281
|
|
|
|
282
|
|
|
throw new \RuntimeException(sprintf( |
283
|
|
|
'Failed to list keys for the container "%s": %s (%s).', |
284
|
|
|
$this->containerName, |
285
|
|
|
$e->getErrorText(), |
286
|
|
|
$errorCode |
287
|
|
|
), $e->getCode()); |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* {@inheritdoc} |
293
|
|
|
* @throws \RuntimeException |
294
|
|
|
* @throws \InvalidArgumentException |
295
|
|
|
*/ |
296
|
|
View Code Duplication |
public function mtime($key) |
|
|
|
|
297
|
|
|
{ |
298
|
|
|
$this->init(); |
299
|
|
|
list($containerName, $key) = $this->tokenizeKey($key); |
300
|
|
|
|
301
|
|
|
try { |
302
|
|
|
$properties = $this->blobProxy->getBlobProperties($containerName, $key); |
303
|
|
|
|
304
|
|
|
return $properties->getProperties()->getLastModified()->getTimestamp(); |
305
|
|
|
} catch (ServiceException $e) { |
|
|
|
|
306
|
|
|
$this->failIfContainerNotFound($e, sprintf('read mtime for key "%s"', $key), $containerName); |
307
|
|
|
|
308
|
|
|
return false; |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* {@inheritdoc} |
314
|
|
|
* @throws \RuntimeException |
315
|
|
|
* @throws \InvalidArgumentException |
316
|
|
|
*/ |
317
|
|
View Code Duplication |
public function delete($key) |
|
|
|
|
318
|
|
|
{ |
319
|
|
|
$this->init(); |
320
|
|
|
list($containerName, $key) = $this->tokenizeKey($key); |
321
|
|
|
|
322
|
|
|
try { |
323
|
|
|
$this->blobProxy->deleteBlob($containerName, $key); |
324
|
|
|
|
325
|
|
|
return true; |
326
|
|
|
} catch (ServiceException $e) { |
|
|
|
|
327
|
|
|
$this->failIfContainerNotFound($e, sprintf('delete key "%s"', $key), $containerName); |
328
|
|
|
|
329
|
|
|
return false; |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* {@inheritdoc} |
335
|
|
|
* @throws \RuntimeException |
336
|
|
|
* @throws \InvalidArgumentException |
337
|
|
|
*/ |
338
|
|
|
public function rename($sourceKey, $targetKey) |
339
|
|
|
{ |
340
|
|
|
$this->init(); |
341
|
|
|
|
342
|
|
|
list($sourceContainerName, $sourceKey) = $this->tokenizeKey($sourceKey); |
343
|
|
|
list($targetContainerName, $targetKey) = $this->tokenizeKey($targetKey); |
344
|
|
|
|
345
|
|
|
try { |
346
|
|
|
$this->createContainer($targetContainerName); |
347
|
|
|
$this->blobProxy->copyBlob($targetContainerName, $targetKey, $sourceContainerName, $sourceKey); |
348
|
|
|
$this->blobProxy->deleteBlob($sourceContainerName, $sourceKey); |
349
|
|
|
|
350
|
|
|
return true; |
351
|
|
|
} catch (ServiceException $e) { |
|
|
|
|
352
|
|
|
$this->failIfContainerNotFound($e, sprintf('rename key "%s"', $sourceKey), $sourceContainerName); |
353
|
|
|
|
354
|
|
|
return false; |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* {@inheritdoc} |
360
|
|
|
*/ |
361
|
|
|
public function isDirectory($key) |
362
|
|
|
{ |
363
|
|
|
// Windows Azure Blob Storage does not support directories |
364
|
|
|
return false; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* {@inheritdoc} |
369
|
|
|
* @throws \RuntimeException |
370
|
|
|
* @throws \InvalidArgumentException |
371
|
|
|
*/ |
372
|
|
View Code Duplication |
public function setMetadata($key, $content) |
|
|
|
|
373
|
|
|
{ |
374
|
|
|
$this->init(); |
375
|
|
|
list($containerName, $key) = $this->tokenizeKey($key); |
376
|
|
|
|
377
|
|
|
try { |
378
|
|
|
$this->blobProxy->setBlobMetadata($containerName, $key, $content); |
379
|
|
|
} catch (ServiceException $e) { |
|
|
|
|
380
|
|
|
$errorCode = $this->getErrorCodeFromServiceException($e); |
381
|
|
|
|
382
|
|
|
throw new \RuntimeException(sprintf( |
383
|
|
|
'Failed to set metadata for blob "%s" in container "%s": %s (%s).', |
384
|
|
|
$key, |
385
|
|
|
$containerName, |
386
|
|
|
$e->getErrorText(), |
387
|
|
|
$errorCode |
388
|
|
|
), $e->getCode()); |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* {@inheritdoc} |
394
|
|
|
* @throws \RuntimeException |
395
|
|
|
* @throws \InvalidArgumentException |
396
|
|
|
*/ |
397
|
|
View Code Duplication |
public function getMetadata($key) |
|
|
|
|
398
|
|
|
{ |
399
|
|
|
$this->init(); |
400
|
|
|
list($containerName, $key) = $this->tokenizeKey($key); |
401
|
|
|
|
402
|
|
|
try { |
403
|
|
|
$properties = $this->blobProxy->getBlobProperties($containerName, $key); |
404
|
|
|
|
405
|
|
|
return $properties->getMetadata(); |
406
|
|
|
} catch (ServiceException $e) { |
|
|
|
|
407
|
|
|
$errorCode = $this->getErrorCodeFromServiceException($e); |
408
|
|
|
|
409
|
|
|
throw new \RuntimeException(sprintf( |
410
|
|
|
'Failed to get metadata for blob "%s" in container "%s": %s (%s).', |
411
|
|
|
$key, |
412
|
|
|
$containerName, |
413
|
|
|
$e->getErrorText(), |
414
|
|
|
$errorCode |
415
|
|
|
), $e->getCode()); |
416
|
|
|
} |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Lazy initialization, automatically called when some method is called after construction. |
421
|
|
|
*/ |
422
|
|
|
protected function init() |
423
|
|
|
{ |
424
|
|
|
if ($this->blobProxy === null) { |
425
|
|
|
$this->blobProxy = $this->blobProxyFactory->create(); |
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Throws a runtime exception if a give ServiceException derived from a "container not found" error. |
431
|
|
|
* |
432
|
|
|
* @param ServiceException $exception |
433
|
|
|
* @param string $action |
434
|
|
|
* @param string $containerName |
435
|
|
|
* |
436
|
|
|
* @throws \RuntimeException |
437
|
|
|
*/ |
438
|
|
|
protected function failIfContainerNotFound(ServiceException $exception, $action, $containerName) |
439
|
|
|
{ |
440
|
|
|
$errorCode = $this->getErrorCodeFromServiceException($exception); |
441
|
|
|
|
442
|
|
|
if ($errorCode === self::ERROR_CONTAINER_NOT_FOUND) { |
443
|
|
|
throw new \RuntimeException(sprintf( |
444
|
|
|
'Failed to %s: container "%s" not found.', |
445
|
|
|
$action, |
446
|
|
|
$containerName |
447
|
|
|
), $exception->getCode()); |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Extracts the error code from a service exception. |
453
|
|
|
* |
454
|
|
|
* @param ServiceException $exception |
455
|
|
|
* |
456
|
|
|
* @return string |
457
|
|
|
*/ |
458
|
|
|
protected function getErrorCodeFromServiceException(ServiceException $exception) |
459
|
|
|
{ |
460
|
|
|
$xml = @simplexml_load_string($exception->getResponse()->getBody()); |
461
|
|
|
|
462
|
|
|
if ($xml && isset($xml->Code)) { |
463
|
|
|
return (string) $xml->Code; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
return $exception->getErrorText(); |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* @param string|resource $content |
471
|
|
|
* |
472
|
|
|
* @return string |
473
|
|
|
*/ |
474
|
|
View Code Duplication |
private function guessContentType($content) |
|
|
|
|
475
|
|
|
{ |
476
|
|
|
$fileInfo = new \finfo(FILEINFO_MIME_TYPE); |
477
|
|
|
|
478
|
|
|
if (is_resource($content)) { |
479
|
|
|
return $fileInfo->file(stream_get_meta_data($content)['uri']); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
return $fileInfo->buffer($content); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* @param string $key |
487
|
|
|
* |
488
|
|
|
* @return array |
489
|
|
|
* @throws \InvalidArgumentException |
490
|
|
|
*/ |
491
|
|
|
private function tokenizeKey($key) |
492
|
|
|
{ |
493
|
|
|
$containerName = $this->containerName; |
494
|
|
|
if (true === $this->multiContainerMode) { |
495
|
|
|
if (false === ($index = strpos($key, '/'))) { |
496
|
|
|
throw new \InvalidArgumentException(sprintf( |
497
|
|
|
'Failed to establish container name from key "%s", container name is required in multi-container mode', |
498
|
|
|
$key |
499
|
|
|
)); |
500
|
|
|
} |
501
|
|
|
$containerName = substr($key, 0, $index); |
502
|
|
|
$key = substr($key, $index + 1); |
503
|
|
|
} |
504
|
|
|
return [$containerName, $key]; |
505
|
|
|
} |
506
|
|
|
} |
507
|
|
|
|