11import re
22import json
33import os .path
4- from datetime import datetime , timedelta
5- from azure .mgmt .storage import StorageManagementClient
6- from azure .storage .filedatalake import DataLakeServiceClient , generate_file_system_sas
4+ from azure .storage .filedatalake import DataLakeServiceClient
75from azure .core .exceptions import ResourceNotFoundError
8- from azure .identity import TokenCachePersistenceOptions , SharedTokenCacheCredential , ChainedTokenCredential , \
9- DeviceCodeCredential
6+ from azure .identity import DeviceCodeCredential , TokenCachePersistenceOptions , ChainedTokenCredential ,\
7+ SharedTokenCacheCredential
108from msgraph .core import GraphClient
119from ddsc .azure .azcopy import create_azcopy , group_by_dirname
1210from ddsc .azure .delivery import DataDelivery
@@ -91,11 +89,8 @@ def ensure_user_exists(self, netid):
9189
9290
9391class Bucket (object ):
94- def __init__ (self , credential , subscription_id , resource_group , storage_account , container_name ):
95- self .resource_group = resource_group
96- self .storage_mgmt_client = StorageManagementClient (credential = credential , subscription_id = subscription_id )
97- self .service = DataLakeServiceClient (f"https://{ storage_account } .dfs.core.windows.net/" , credential = credential ,
98- scopes = DLS_SCOPES )
92+ def __init__ (self , credential , storage_account , container_name ):
93+ self .service = DataLakeServiceClient (f"https://{ storage_account } .dfs.core.windows.net/" , credential = credential )
9994 self .file_system = self .service .get_file_system_client (file_system = container_name )
10095 self .azcopy = create_azcopy ()
10196
@@ -116,28 +111,6 @@ def move_directory(self, source, destination):
116111 def get_file_properties (self , file_path ):
117112 return self .file_system .get_file_client (file_path ).get_file_properties ()
118113
119- def get_storage_account_key1 (self ):
120- return self .storage_mgmt_client .storage_accounts .list_keys (
121- resource_group_name = self .resource_group ,
122- account_name = self .service .account_name ).keys [0 ].value
123-
124- def get_sas_token (self , hours = 6 ):
125- account_key = self .get_storage_account_key1 ()
126- return generate_file_system_sas (
127- account_name = self .service .account_name ,
128- credential = account_key ,
129- file_system_name = self .file_system .file_system_name ,
130- permission = "rwdl" ,
131- protocol = 'https' ,
132- expiry = datetime .utcnow () + timedelta (hours = hours )
133- )
134-
135- def get_sas_url (self , path , hours = 6 ):
136- account_name = self .service .account_name
137- bucket_name = self .file_system .file_system_name
138- token = self .get_sas_token (hours = hours )
139- return f"https://{ account_name } .blob.core.windows.net/{ bucket_name } /{ path } ?{ token } "
140-
141114 def get_url (self , path ):
142115 account_name = self .service .account_name
143116 bucket_name = self .file_system .file_system_name
@@ -254,20 +227,21 @@ def __str__(self):
254227
255228
256229class AzureApi (object ):
257- def __init__ (self , config , credential , subscription_id , resource_group , storage_account , container_name ):
230+ def __init__ (self , config , credential ):
258231 self .config = config
259232 self .users = Users (credential )
260233 self .current_user_netid = self .users .get_current_user_netid ()
261- self .bucket = Bucket (credential , subscription_id , resource_group , storage_account , container_name )
234+ container_name = config .azure_container_name
235+ if not container_name :
236+ container_name = self .current_user_netid
237+ self .bucket = Bucket (credential , config .azure_storage_account , container_name )
262238
263239 def list_projects (self ):
264- path_dicts = self .bucket .get_paths (path = self . current_user_netid , recursive = False )
240+ path_dicts = self .bucket .get_paths (path = "" , recursive = False )
265241 return [AzureProject (self , path_dict ) for path_dict in path_dicts if path_dict ["is_directory" ] is True ]
266242
267243 def get_project_by_name (self , name ):
268244 path = name
269- if "/" not in path :
270- path = f"{ self .current_user_netid } /{ name } "
271245 try :
272246 return AzureProject (self , self .bucket .get_directory_properties (path ))
273247 except ResourceNotFoundError :
@@ -301,9 +275,6 @@ def get_file_paths(self, path):
301275 def get_file_properties (self , file_path ):
302276 return self .bucket .get_file_properties (file_path )
303277
304- def get_sas_url (self ):
305- return self .bucket .get_sas_url (path = self .current_user_netid )
306-
307278 def add_user_to_project (self , project , netid , auth_role ):
308279 user_id , user_name = self .users .get_id_and_name (netid )
309280 role = self .get_auth_role_by_id (auth_role )
@@ -317,15 +288,11 @@ def remove_user_from_project(self, project, netid):
317288
318289 def upload_paths (self , project_name , paths , dry_run ):
319290 project_path = project_name
320- if "/" not in project_path :
321- project_path = f"{ self .current_user_netid } /{ project_name } /"
322291 self .bucket .upload_paths (project_path , paths , dry_run )
323292 print ("\n Upload complete.\n See azcopy log file for details about transferred files.\n \n " )
324293
325294 def download_paths (self , project_name , include_paths , exclude_paths , destination , dry_run ):
326295 project_path = project_name
327- if "/" not in project_path :
328- project_path = f"{ self .current_user_netid } /{ project_name } /"
329296 self .bucket .download_paths (project_path , include_paths , exclude_paths , destination , dry_run = dry_run )
330297 print ("\n Download complete.\n See azcopy log file for details about transferred files.\n \n " )
331298
@@ -352,15 +319,10 @@ def deliver(self, project, netid, resend, user_message, share_usernames):
352319
353320
354321def create_azure_api (config ):
355- # Setup to use token cache or prompt user to login with a URL (DeviceCodeCredential)
356322 cache_persistence_options = TokenCachePersistenceOptions (allow_unencrypted_storage = True )
357323 credential = ChainedTokenCredential (
358324 SharedTokenCacheCredential (),
359325 DeviceCodeCredential (cache_persistence_options = cache_persistence_options ))
360326 return AzureApi (
361327 config = config ,
362- credential = credential ,
363- subscription_id = config .azure_subscription_id ,
364- resource_group = config .azure_resource_group ,
365- storage_account = config .azure_storage_account ,
366- container_name = config .azure_container_name )
328+ credential = credential )
0 commit comments