|
7 | 7 | from azure.storage.blob.aio import BlobServiceClient
|
8 | 8 | from azure.identity import DefaultAzureCredential
|
9 | 9 | import urllib
|
| 10 | +from environment import IdentityType, get_identity_type |
10 | 11 |
|
11 | 12 |
|
12 | 13 | class StorageAccountHelper:
|
13 | 14 | """Helper class for interacting with Azure Blob Storage."""
|
14 | 15 |
|
15 | 16 | def __init__(self) -> None:
|
16 | 17 | """Initialize the StorageAccountHelper class."""
|
17 |
| - self._client_id = os.environ["FunctionApp__ClientId"] |
18 |
| - |
19 | 18 | self._endpoint = os.environ["StorageAccount__ConnectionString"]
|
20 | 19 |
|
21 | 20 | async def get_client(self):
|
22 | 21 | """Get the BlobServiceClient object."""
|
23 |
| - credential = DefaultAzureCredential(managed_identity_client_id=self._client_id) |
24 |
| - |
25 |
| - return BlobServiceClient(account_url=self._endpoint, credential=credential) |
| 22 | + if get_identity_type() == IdentityType.SYSTEM_ASSIGNED: |
| 23 | + credential = DefaultAzureCredential() |
| 24 | + return BlobServiceClient(account_url=self._endpoint, credential=credential) |
| 25 | + elif get_identity_type() == IdentityType.USER_ASSIGNED: |
| 26 | + credential = DefaultAzureCredential( |
| 27 | + managed_identity_client_id=os.environ["FunctionApp__ClientId"] |
| 28 | + ) |
| 29 | + return BlobServiceClient(account_url=self._endpoint, credential=credential) |
| 30 | + else: |
| 31 | + return BlobServiceClient(account_url=self._endpoint) |
26 | 32 |
|
27 | 33 | async def add_metadata_to_blob(
|
28 | 34 | self, source: str, container: str, metadata: dict
|
|
0 commit comments