Skip to content

Commit

Permalink
[azure] do not require both Account(Name|Key) in connection_string
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneier committed Sep 29, 2023
1 parent 7d6ceeb commit 1654d38
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
13 changes: 8 additions & 5 deletions storages/backends/azure_storage.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import mimetypes
import re
from datetime import datetime
from datetime import timedelta
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
Expand Down Expand Up @@ -124,10 +124,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 {
Expand Down
7 changes: 7 additions & 0 deletions tests/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,13 @@ 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
Expand Down

0 comments on commit 1654d38

Please sign in to comment.