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