Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[azure] do not require both Account(Name|Key) in connection_string #1312

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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