1
1
"""Utilities for working with Azure blob storage"""
2
- import re
3
2
import io
3
+ import re
4
4
5
- from azure .storage .blob import BlobServiceClient
6
5
from azure .identity import EnvironmentCredential
6
+ from azure .storage .blob import BlobServiceClient
7
7
8
8
9
9
class AzureBlobStore :
@@ -20,7 +20,7 @@ class AzureBlobStore:
20
20
21
21
def _blob_service_client (self , account_name , sas_token = None ):
22
22
blob_service_client = BlobServiceClient (
23
- account_url = f" { account_name } .blob.core.windows.net" ,
23
+ account_url = f' { account_name } .blob.core.windows.net' ,
24
24
credential = sas_token or EnvironmentCredential (),
25
25
)
26
26
@@ -32,50 +32,38 @@ def _split_url(self, url):
32
32
see: https://docs.microsoft.com/en-us/azure/storage/common/storage-dotnet-shared-access-signature-part-1 # noqa: E501
33
33
abs://myaccount.blob.core.windows.net/sascontainer/sasblob.txt?sastoken
34
34
"""
35
- match = re .match (
36
- r"abs://(.*)\.blob\.core\.windows\.net\/(.*?)\/([^\?]*)\??(.*)$" , url
37
- )
35
+ match = re .match (r'abs://(.*)\.blob\.core\.windows\.net\/(.*?)\/([^\?]*)\??(.*)$' , url )
38
36
if not match :
39
37
raise Exception (f"Invalid azure blob url '{ url } '" )
40
38
else :
41
39
params = {
42
- " account" : match .group (1 ),
43
- " container" : match .group (2 ),
44
- " blob" : match .group (3 ),
45
- " sas_token" : match .group (4 ),
40
+ ' account' : match .group (1 ),
41
+ ' container' : match .group (2 ),
42
+ ' blob' : match .group (3 ),
43
+ ' sas_token' : match .group (4 ),
46
44
}
47
45
return params
48
46
49
47
def read (self , url ):
50
48
"""Read storage at a given url"""
51
49
params = self ._split_url (url )
52
50
output_stream = io .BytesIO ()
53
- blob_service_client = self ._blob_service_client (
54
- params ["account" ], params ["sas_token" ]
55
- )
56
- blob_client = blob_service_client .get_blob_client (
57
- params ["container" ], params ["blob" ]
58
- )
51
+ blob_service_client = self ._blob_service_client (params ['account' ], params ['sas_token' ])
52
+ blob_client = blob_service_client .get_blob_client (params ['container' ], params ['blob' ])
59
53
blob_client .download_blob ().readinto (output_stream )
60
54
output_stream .seek (0 )
61
- return [line .decode (" utf-8" ) for line in output_stream ]
55
+ return [line .decode (' utf-8' ) for line in output_stream ]
62
56
63
57
def listdir (self , url ):
64
58
"""Returns a list of the files under the specified path"""
65
59
params = self ._split_url (url )
66
- blob_service_client = self ._blob_service_client (
67
- params ["account" ], params ["sas_token" ]
68
- )
69
- container_client = blob_service_client .get_container_client (params ["container" ])
70
- return list (container_client .list_blobs (params ["blob" ]))
60
+ blob_service_client = self ._blob_service_client (params ['account' ], params ['sas_token' ])
61
+ container_client = blob_service_client .get_container_client (params ['container' ])
62
+ return list (container_client .list_blobs (params ['blob' ]))
71
63
72
64
def write (self , buf , url ):
73
65
"""Write buffer to storage at a given url"""
74
66
params = self ._split_url (url )
75
- blob_service_client = self ._blob_service_client (
76
- params ["account" ], params ["sas_token" ]
77
- )
78
- blob_client = blob_service_client .get_blob_client (
79
- params ["container" ], params ["blob" ]
80
- )
67
+ blob_service_client = self ._blob_service_client (params ['account' ], params ['sas_token' ])
68
+ blob_client = blob_service_client .get_blob_client (params ['container' ], params ['blob' ])
81
69
blob_client .upload_blob (data = buf , overwrite = True )
0 commit comments