AzureBlobClient is a wrapper around the Azure BlobClient class. Parameters ---------- account : str The Azure account name. container : str The Azure container name.
| 10 | |
| 11 | |
| 12 | class AzureBlobClient: |
| 13 | """ |
| 14 | AzureBlobClient is a wrapper around the Azure BlobClient class. |
| 15 | |
| 16 | |
| 17 | Parameters |
| 18 | ---------- |
| 19 | account : str |
| 20 | The Azure account name. |
| 21 | container : str |
| 22 | The Azure container name. |
| 23 | """ |
| 24 | |
| 25 | account: str |
| 26 | container: str |
| 27 | sas_token: SecretStr = None |
| 28 | account_key: SecretStr = None |
| 29 | clients: dict[str, BlobClient] = {} |
| 30 | |
| 31 | def __init__(self, account, container): |
| 32 | self.account = account |
| 33 | self.container = container |
| 34 | |
| 35 | def set_sas_token(self, sas_token): |
| 36 | self.sas_token = sas_token |
| 37 | |
| 38 | def set_account_key(self, account_key): |
| 39 | self.account_key = account_key |
| 40 | |
| 41 | def sas_token_has_write_permission(self): |
| 42 | if not self.sas_token: |
| 43 | return False |
| 44 | if not self.sas_token.get_secret_value(): |
| 45 | return False |
| 46 | if self.sas_token.get_secret_value() == "": |
| 47 | return False |
| 48 | # make sure that sp contains w and c and they need to be in this |
| 49 | # part of the query string not in the & that follows |
| 50 | content = self.sas_token.get_secret_value().split("&")[0] |
| 51 | if "w" in content and "c" in content: |
| 52 | return True |
| 53 | return False |
| 54 | |
| 55 | def can_write(self): |
| 56 | return self.account_key or self.sas_token_has_write_permission() |
| 57 | |
| 58 | def get_blob_client(self, filepath): |
| 59 | if filepath in self.clients: |
| 60 | return self.clients[filepath] |
| 61 | if self.account_key: |
| 62 | sas_token = self.generate_sas_token( |
| 63 | filepath, self.account_key.get_secret_value(), True |
| 64 | ) |
| 65 | blob_client = BlobClient.from_blob_url( |
| 66 | f"https://{self.account}.blob.core.windows.net/" |
| 67 | f"{self.container}/{filepath}", |
| 68 | credential=sas_token, |
| 69 | ) |
no outgoing calls
no test coverage detected