11<?php
22namespace phpbu \App \Backup \Sync ;
33
4- use MicrosoftAzure \Storage \Blob \BlobRestProxy ;
4+ use AzureOss \Storage \Blob \BlobContainerClient ;
5+ use AzureOss \Storage \Blob \BlobServiceClient ;
56use phpbu \App \Backup \Collector ;
67use phpbu \App \Backup \Path ;
78use phpbu \App \Backup \Target ;
@@ -25,12 +26,11 @@ class AzureBlob implements Simulator
2526 use Cleanable;
2627
2728 /**
28- * Azure Blob client.
29+ * Azure Blob container client.
2930 *
30- * @var BlobRestProxy;
31+ * @var BlobContainerClient
3132 */
32- private $ client ;
33-
33+ protected $ client ;
3434
3535 /**
3636 * Azure Blob Connection String
@@ -77,9 +77,9 @@ class AzureBlob implements Simulator
7777 */
7878 public function setup (array $ config )
7979 {
80- if (!class_exists ('\\MicrosoftAzure \\Storage \\Blob \\BlobRestProxy ' )) {
81- throw new Exception ('Azure Bob Storage SDK not loaded: use composer to install ' .
82- '"microsoft/ azure-storage-blob " ' );
80+ if (!class_exists ('\\AzureOss \\Storage \\Blob \\BlobServiceClient ' )) {
81+ throw new Exception ('Azure Blob Storage SDK not loaded: use composer to install ' .
82+ '"azure-oss/ storage" ' );
8383 }
8484
8585 // check for mandatory options
@@ -122,7 +122,7 @@ public function sync(Target $target, Result $result)
122122 {
123123 $ this ->client = $ this ->createClient ();
124124
125- if (!$ this ->doesContainerExist ($ this ->client , $ this -> containerName )) {
125+ if (!$ this ->doesContainerExist ($ this ->client )) {
126126 $ result ->debug ('create blob container ' );
127127 $ this ->createContainer ($ this ->client );
128128 }
@@ -139,13 +139,14 @@ public function sync(Target $target, Result $result)
139139 }
140140
141141 /**
142- * Create the Azure Blob client.
142+ * Create the Azure Blob container client.
143143 *
144- * @return \MicrosoftAzure \Storage\Blob\BlobRestProxy
144+ * @return \AzureOss \Storage\Blob\BlobContainerClient
145145 */
146- protected function createClient () : BlobRestProxy
146+ protected function createClient () : BlobContainerClient
147147 {
148- return BlobRestProxy::createBlobService ($ this ->connectionString );
148+ return BlobServiceClient::fromConnectionString ($ this ->connectionString )
149+ ->getContainerClient ($ this ->containerName );
149150 }
150151
151152 /**
@@ -157,7 +158,7 @@ protected function createClient() : BlobRestProxy
157158 protected function createCollector (Target $ target ) : Collector
158159 {
159160 $ path = new Path ($ this ->pathRaw , $ this ->time );
160- return new Collector \AzureBlob ($ target , $ path , $ this ->client , $ this -> containerName );
161+ return new Collector \AzureBlob ($ target , $ path , $ this ->client );
161162 }
162163
163164 /**
@@ -180,47 +181,48 @@ public function simulate(Target $target, Result $result)
180181 /**
181182 * Check if an Azure Blob Storage Container exists
182183 *
183- * @param \MicrosoftAzure\Storage\Blob\BlobRestProxy $blobRestProxy
184- * @param string $containerName
184+ * @param \AzureOss\Storage\Blob\BlobContainerClient $client
185185 * @return bool
186186 */
187- private function doesContainerExist (BlobRestProxy $ blobRestProxy , string $ containerName ): bool
187+ protected function doesContainerExist (BlobContainerClient $ client ): bool
188188 {
189- $ containers = $ blobRestProxy ->listContainers ()->getContainers ();
190- foreach ($ containers as $ container ) {
191- if ($ container ->getName () === $ containerName ) {
192- return true ;
193- }
194- }
195- return false ;
189+ return $ client ->exists ();
196190 }
197191
198192 /**
199193 * Create an Azure Storage Container
200194 *
201- * @param \MicrosoftAzure \Storage\Blob\BlobRestProxy $blobRestProxy
195+ * @param \AzureOss \Storage\Blob\BlobContainerClient $client
202196 */
203- private function createContainer (BlobRestProxy $ blobRestProxy )
197+ protected function createContainer (BlobContainerClient $ client )
204198 {
205- $ blobRestProxy -> createContainer ( $ this -> containerName );
199+ $ client -> create ( );
206200 }
207201
208202 /**
209203 * Upload backup to Azure Blob Storage
210204 *
211- * @param \phpbu\App\Backup\Target $target
212- * @param \MicrosoftAzure \Storage\Blob\BlobRestProxy $blobRestProxy
205+ * @param \phpbu\App\Backup\Target $target
206+ * @param \AzureOss \Storage\Blob\BlobContainerClient $client
213207 * @throws \phpbu\App\Backup\Sync\Exception
214208 * @throws \phpbu\App\Exception
215209 */
216- private function upload (Target $ target , BlobRestProxy $ blobRestProxy )
210+ protected function upload (Target $ target , BlobContainerClient $ client )
217211 {
218212 $ source = $ this ->getFileHandle ($ target ->getPathname (), 'r ' );
219- $ blobRestProxy ->createBlockBlob (
220- $ this ->containerName ,
221- $ this ->getUploadPath ($ target ),
222- $ source
223- );
213+ $ this ->uploadBlob ($ client , $ this ->getUploadPath ($ target ), $ source );
214+ }
215+
216+ /**
217+ * Upload a single blob to the container.
218+ *
219+ * @param \AzureOss\Storage\Blob\BlobContainerClient $client
220+ * @param string $path
221+ * @param resource $source
222+ */
223+ protected function uploadBlob (BlobContainerClient $ client , string $ path , $ source )
224+ {
225+ $ client ->getBlobClient ($ path )->upload ($ source );
224226 }
225227
226228 /**
0 commit comments