Skip to content

Instantly share code, notes, and snippets.

@dvu4
Created January 2, 2023 21:11
Show Gist options
  • Save dvu4/e18ff5272d95454aac2481339fa9ac2e to your computer and use it in GitHub Desktop.
Save dvu4/e18ff5272d95454aac2481339fa9ac2e to your computer and use it in GitHub Desktop.
this script will retrieve the connection string in Azure Storage Account
from azure.identity import DefaultAzureCredential
from azure.mgmt.storage import StorageManagementClient
STORAGE_ACCOUNT_NAME = "storage_account_name"
SUBSCRIPTION_ID = "subscription_id"
RESOURCE_GROUP = "resource_group"
def get_storage_account_connection_string() -> Optional[str]:
"""
Returns : connection string
-------
"""
try:
credential = DefaultAzureCredential(additionally_allowed_tenants=['*'])
# create Storage client
storage_client = StorageManagementClient(credential, SUBSCRIPTION_ID)
# get Primary key for storage account
keys = storage_client.storage_accounts.list_keys(RESOURCE_GROUP, STORAGE_ACCOUNT_NAME)
# get Connection string
connection_string = f"DefaultEndpointsProtocol=https;EndpointSuffix=core.windows.net;" \
f"AccountName={STORAGE_ACCOUNT_NAME};AccountKey={keys.keys[0].value}"
return connection_string
except Exception as err:
print(f"Error : {err}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment