in python/dpu_utils/utils/richpath.py [0:0]
def create(path: str, azure_info_path: Optional[str]=None) -> 'RichPath':
"""This creates a RichPath object based on the input path.
To create a remote path, just prefix it appropriately and pass
in the path to the .json configuration.
"""
if path.startswith(AZURE_PATH_PREFIX):
# Strip off the AZURE_PATH_PREFIX:
path = path[len(AZURE_PATH_PREFIX):]
account_name, container_name, path = path.split('/', 2)
if azure_info_path is not None:
with open(azure_info_path, 'r') as azure_info_file:
azure_info = json.load(azure_info_file)
azure_info = azure_info.get(account_name)
if azure_info is None:
raise Exception("Could not find access information for account '%s'!" % (account_name,))
account_endpoint = azure_info.get('endpoint', "https://%s.blob.core.windows.net/" % account_name)
cache_location = azure_info.get('cache_location')
connection_string = azure_info.get('connection_string')
sas_token = azure_info.get('sas_token')
account_key = azure_info.get('account_key')
if connection_string is not None:
container_client = ContainerClient.from_connection_string(connection_string, container_name)
elif sas_token is not None:
query_string: str = sas_token
if not query_string.startswith('?'):
query_string = '?' + query_string
container_client = ContainerClient.from_container_url(f"{account_endpoint}/{container_name}{query_string}")
elif account_key is not None:
connection_string = f"AccountName={account_name};AccountKey={account_key};BlobEndpoint={account_endpoint};"
container_client = ContainerClient.from_connection_string(connection_string, container_name)
else:
raise Exception("Access to Azure storage account '%s' with azure_info_path requires either account_key or sas_token!" % (
account_name,
))
else:
token_credential = DefaultAzureCredential()
# This is the correct URI for all non-sovereign clouds or emulators.
account_endpoint = "https://%s.blob.core.windows.net/" % account_name
cache_location = None
container_client = ContainerClient(
account_url=account_endpoint,
container_name=container_name,
credential=token_credential)
# Replace environment variables in the cache location
if cache_location is not None:
def replace_by_env_var(m) -> str:
env_var_name = m.group(1)
env_var_value = os.environ.get(env_var_name)
if env_var_value is not None:
return env_var_value
else:
return env_var_name
cache_location = re.sub('\\${([^}]+)}', replace_by_env_var, cache_location)
return AzurePath(path,
azure_container_client=container_client,
cache_location=cache_location)
else:
return LocalPath(path)