1+ import os
2+
3+ from azure.core.exceptions import ClientAuthenticationError
14from azure.identity import DefaultAzureCredential
25
36
@@ -24,19 +27,38 @@ def get_access_token(audience: str) -> str:
2427
2528def get_onelake_access_token() -> str:
2629 """
27- Retrieves an access token for OneLake storage.
30+ Alias for `get_azure_storage_access_token`
31+ """
32+ return get_azure_storage_access_token()
33+
34+ def get_azure_storage_access_token() -> str:
35+ """
36+ Retrieves an access token for Azure Storage.
2837
2938 This function attempts to obtain an access token for accessing Azure storage.
30- It first checks if the code is running in a Microsoft Fabric notebook environment
31- and attempts to use the `notebookutils` library to get the token. If the library
32- is not available, it falls back to using the `DefaultAzureCredential` from the Azure SDK
33- to fetch the token.
39+ It first checks if the `AZURE_STORAGE_TOKEN` environment variable is set.
40+ Otherwise, it tries to get the token using `notebookutils.credentials.getToken`.
41+ Lastly, it falls back to using the `DefaultAzureCredential`.
3442
3543 Returns:
36- The access token used for authenticating requests to Azure OneLake storage .
44+ The access token used for authenticating requests to Azure Storage .
3745 """
38- audience = "https://storage.azure.com"
39- return get_access_token(audience)
46+
47+ token = os.environ.get("AZURE_STORAGE_TOKEN")
48+ if token:
49+ return token
50+
51+ try:
52+ audience = "https://storage.azure.com"
53+ return get_access_token(audience)
54+ except ClientAuthenticationError as e:
55+ raise ClientAuthenticationError(
56+ f"{str(e)}\n\n"
57+ "Additional troubleshooting steps:\n"
58+ "1. Ensure you can use any of the credentials methods to get an access token\n"
59+ "2. Set the `AZURE_STORAGE_TOKEN` environment variable with a valid access token"
60+ ) from e
61+
4062
4163
4264def get_fabric_bearer_token() -> str:
@@ -71,14 +93,13 @@ def get_azure_devops_access_token() -> str:
7193
7294def get_storage_options() -> dict[str, str]:
7395 """
74- Retrieves storage options including a bearer token for OneLake storage .
96+ Retrieves storage options including a bearer token for Azure Storage .
7597
76- This function calls `get_onelake_access_token` to obtain a bearer token
77- and returns a dictionary containing the token and a flag indicating
78- whether to use the Fabric endpoint.
98+ This function calls `get_azure_storage_access_token` to obtain a bearer token
99+ and returns a dictionary containing the token.
79100
80101 Returns:
81- A dictionary containing the storage options for OneLake .
102+ A dictionary containing the storage options for Azure Storage .
82103
83104 Example:
84105 **Retrieve storage options**
@@ -87,7 +108,7 @@ def get_storage_options() -> dict[str, str]:
87108
88109 options = get_storage_options()
89110 options
90- {' bearer_token': ' your_token_here', 'use_fabric_endpoint': 'true' }
111+ {" bearer_token": " your_token_here" }
91112 ```
92113 """
93- return {"bearer_token": get_onelake_access_token(), "use_fabric_endpoint": "true" }
114+ return {"bearer_token": get_azure_storage_access_token() }
0 commit comments