60
60
from hubblestack .utils .signing import find_wrapf
61
61
62
62
try :
63
- import azure .storage .common
64
- import azure .storage .blob
63
+ from azure .storage .blob import BlobServiceClient
65
64
HAS_AZURE = True
66
65
except ImportError :
67
66
HAS_AZURE = False
@@ -193,7 +192,7 @@ def update():
193
192
blob_service = _get_container_service (container )
194
193
name = container ['container_name' ]
195
194
try :
196
- blob_list = blob_service .list_blobs (name )
195
+ blob_list = blob_service .list_blobs ()
197
196
except Exception as exc :
198
197
log .exception ('Error occurred fetching blob list for azurefs' )
199
198
@@ -224,8 +223,14 @@ def update():
224
223
log .exception ('Problem occurred trying to invalidate cache for container "{0}"' .format (name ))
225
224
continue
226
225
226
+ blobs_data = []
227
+ for blob in blob_list :
228
+ # list_blobs returns an iterator
229
+ # and we iterate over it more than once
230
+ blobs_data .append (blob )
231
+
227
232
# Walk the cache directory searching for deletions
228
- blob_names = [blob .name for blob in blob_list ]
233
+ blob_names = [blob .name for blob in blobs_data ]
229
234
blob_set = set (blob_names )
230
235
for root , dirs , files in os .walk (path ):
231
236
for f in files :
@@ -240,12 +245,12 @@ def update():
240
245
if not dirs and not files :
241
246
shutil .rmtree (root )
242
247
243
- for blob in blob_list :
248
+ for blob in blobs_data :
244
249
fname = os .path .join (path , blob .name )
245
250
update = False
246
251
if os .path .exists (fname ):
247
252
# File exists, check the hashes
248
- source_md5 = blob .properties . content_settings .content_md5
253
+ source_md5 = blob .content_settings .content_md5
249
254
local_md5_hex = hubblestack .utils .hashutils .get_hash (fname , 'md5' )
250
255
local_md5 = base64 .b64encode (bytes .fromhex (local_md5_hex ))
251
256
if local_md5 != source_md5 :
@@ -263,7 +268,10 @@ def update():
263
268
fp_ .write ('' )
264
269
265
270
try :
266
- blob_service .get_blob_to_path (name , blob .name , fname )
271
+ blob_client = blob_service .get_blob_client (blob )
272
+
273
+ with open (fname , "wb" ) as download_file :
274
+ download_file .write (blob_client .download_blob ().readall ())
267
275
except Exception as exc :
268
276
log .exception ('Error occurred fetching blob from azurefs' )
269
277
@@ -400,21 +408,27 @@ def _get_container_service(container):
400
408
401
409
Try account_key, sas_token, and no auth in that order
402
410
"""
403
- if 'account_key' in container :
404
- account = azure .storage .common .CloudStorageAccount (container ['account_name' ], account_key = container ['account_key' ])
405
- elif 'sas_token' in container :
406
- account = azure .storage .common .CloudStorageAccount (container ['account_name' ], sas_token = container ['sas_token' ])
407
- else :
408
- account = azure .storage .common .CloudStorageAccount (container ['account_name' ])
409
- blob_service = account .create_block_blob_service ()
410
- if 'proxy' in container and len (container ['proxy' ].split (':' )) == 2 :
411
- blob_service .set_proxy (container ['proxy' ].split (':' )[0 ], container ['proxy' ].split (':' )[1 ])
411
+ account_url = f'https://{ container ["account_name" ]} .blob.core.windows.net'
412
+
413
+ proxies = None
414
+ if 'proxy' in container :
415
+ proxies = {'http' : container ['proxy' ]}
412
416
# If 'proxy' isn't specified in container block, check if 'https_proxy' is set.
413
- elif 'https_proxy' in __opts__ and len (__opts__ ['https_proxy' ].split (':' )) == 2 :
414
- blob_service .set_proxy (__opts__ ['https_proxy' ].split (':' )[0 ], __opts__ ['https_proxy' ].split (':' )[1 ])
415
- return blob_service
416
-
417
+ elif 'https_proxy' in __opts__ :
418
+ proxies = {'https' : __opts__ ['https_proxy' ]}
419
+
420
+ # instantiate based upon credential
421
+ if "account_key" in container :
422
+ blob_service = BlobServiceClient (
423
+ account_url = account_url , credential = container ["account_key" ], proxies = proxies )
424
+ elif "sas_token" in container :
425
+ blob_service = BlobServiceClient (
426
+ account_url = account_url , credential = container ["sas_token" ], proxies = proxies )
427
+ else :
428
+ blob_service = BlobServiceClient (account_url = account_url , proxies = proxies )
417
429
430
+ return blob_service .get_container_client (container ['container_name' ])
431
+
418
432
def _validate_config ():
419
433
"""
420
434
Validate azurefs config, return False if it doesn't validate
0 commit comments