diff --git a/storages/backends/azure_storage.py b/storages/backends/azure_storage.py index 77e45c96..838c82b1 100644 --- a/storages/backends/azure_storage.py +++ b/storages/backends/azure_storage.py @@ -5,6 +5,7 @@ from tempfile import SpooledTemporaryFile from azure.core.exceptions import ResourceNotFoundError +from azure.core.utils import parse_connection_string from azure.storage.blob import BlobClient from azure.storage.blob import BlobSasPermissions from azure.storage.blob import BlobServiceClient @@ -124,10 +125,13 @@ def __init__(self, **settings): self._user_delegation_key = None self._user_delegation_key_expiry = datetime.utcnow() if self.connection_string and (not self.account_name or not self.account_key): - self.account_name = re.search( - r"AccountName=(\w+);", self.connection_string - )[1] - self.account_key = re.search(r"AccountKey=(.*);", self.connection_string)[1] + parsed = parse_connection_string( + self.connection_string, case_sensitive_keys=True + ) + if not self.account_name and "AccountName" in parsed: + self.account_name = parsed["AccountName"] + if not self.account_key and "AccountKey" in parsed: + self.account_key = parsed["AccountKey"] def get_default_settings(self): return { diff --git a/tests/test_azure.py b/tests/test_azure.py index b454fc2f..d5deed2a 100644 --- a/tests/test_azure.py +++ b/tests/test_azure.py @@ -292,6 +292,11 @@ def test_container_client_params_token_credential(self): "https://test.blob.core.windows.net", credential="foo_cred" ) + def test_connection_string_can_have_missing(self): + storage = azure_storage.AzureStorage(connection_string="AccountKey=abc;Foobar=xyz;") + self.assertEqual(storage.account_key, "abc") + self.assertIsNone(storage.account_name) + def test_container_client_params_connection_string(self): storage = azure_storage.AzureStorage() storage.account_name = self.account_name