|
| 1 | +<?php |
| 2 | +namespace phpbu\App\Backup\Collector; |
| 3 | + |
| 4 | +use MicrosoftAzure\Storage\Blob\BlobRestProxy; |
| 5 | +use MicrosoftAzure\Storage\Blob\Models\ListBlobsOptions; |
| 6 | +use phpbu\App\Backup\Collector; |
| 7 | +use phpbu\App\Backup\File\AzureBlob as BlobFile; |
| 8 | +use phpbu\App\Backup\Path; |
| 9 | +use phpbu\App\Backup\Target; |
| 10 | +use phpbu\App\Util; |
| 11 | + |
| 12 | +/** |
| 13 | + * AzureBlob class. |
| 14 | + * |
| 15 | + * @package phpbu |
| 16 | + * @subpackage Backup |
| 17 | + * @author Sebastian Feldmann <sebastian@phpbu.de> |
| 18 | + * @author Jonathan Bouzekri <jonathan.bouzekri@gmail.com> |
| 19 | + * @copyright Sebastian Feldmann <sebastian@phpbu.de> |
| 20 | + * @license https://opensource.org/licenses/MIT The MIT License (MIT) |
| 21 | + * @link http://phpbu.de/ |
| 22 | + * @since Class available since Release 5.2.7 |
| 23 | + */ |
| 24 | +class AzureBlob extends Remote implements Collector |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var \MicrosoftAzure\Storage\Blob\BlobRestProxy |
| 28 | + */ |
| 29 | + protected $client; |
| 30 | + |
| 31 | + /** |
| 32 | + * Azure Blob Storage Container name |
| 33 | + * |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + protected $containerName; |
| 37 | + |
| 38 | + /** |
| 39 | + * Amazon S3 constructor. |
| 40 | + * |
| 41 | + * @param \phpbu\App\Backup\Target $target |
| 42 | + * @param \phpbu\App\Backup\Path $path |
| 43 | + * @param \MicrosoftAzure\Storage\Blob\BlobRestProxy $client |
| 44 | + * @param string $containerName |
| 45 | + */ |
| 46 | + public function __construct(Target $target, Path $path, BlobRestProxy $client, string $containerName) |
| 47 | + { |
| 48 | + $this->setUp($target, $path); |
| 49 | + $this->client = $client; |
| 50 | + $this->containerName = $containerName; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Collect all created backups. |
| 55 | + */ |
| 56 | + protected function collectBackups() |
| 57 | + { |
| 58 | + $listBlobsOptions = new ListBlobsOptions(); |
| 59 | + $listBlobsOptions->setPrefix($this->getPrefix($this->path->getPathThatIsNotChanging())); |
| 60 | + $listBlobsOptions->setMaxResults(10); |
| 61 | + |
| 62 | + do { |
| 63 | + $blobList = $this->client->listBlobs($this->containerName, $listBlobsOptions); |
| 64 | + foreach ($blobList->getBlobs() as $blob) { |
| 65 | + if ($this->isFileMatch($blob->getName())) { |
| 66 | + $file = new BlobFile($this->client, $this->containerName, $blob); |
| 67 | + $index = $this->getFileIndex($file); |
| 68 | + $this->files[$index] = $file; |
| 69 | + } |
| 70 | + } |
| 71 | + $listBlobsOptions->setContinuationToken($blobList->getContinuationToken()); |
| 72 | + } while ($blobList->getContinuationToken()); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Return prefix for querying remote files and folders |
| 77 | + * |
| 78 | + * @param string $path |
| 79 | + * @return string |
| 80 | + */ |
| 81 | + protected function getPrefix($path): string |
| 82 | + { |
| 83 | + $prefix = Util\Path::withoutLeadingSlash($path); |
| 84 | + $prefix = $prefix ? Util\Path::withTrailingSlash($prefix) : ''; |
| 85 | + return $prefix; |
| 86 | + } |
| 87 | +} |
0 commit comments