How to create Azure Storage Account using Python

This post describes how to create Azure Storage Account using Python. Azure Storage Management package can be used to manage storage accounts using Python.

Prerequisite: Below are the prerequisites for following this post.

1. Azure CLI. Refer below articles to install Azure CLI.

[Windows]
Install Azure CLI on windows
[Ubuntu]
Install Azure CLI on Ubuntu
2. Python 3.6 or later

Create Azure Storage Account using Python

  • Login with Azure CLI
  •     
    
    az login
    
    
    
  • Install required Python Libraries
  •     
    
    pip install azure-identity
    pip install azure-mgmt-storage
    
    
    
  • Import required modules
  •     
    
    from azure.mgmt.storage import StorageManagementClient
    from azure.identity import AzureCliCredential
    from azure.mgmt.storage.models import StorageAccountCreateParameters, Sku
    
    
    
  • Define SubscriptionID, ResourceGroupName and StorageAccountName
  •     
    
    subscription_id = "REPLACE_WITH_SUBSCRIPTION_ID"    
    RESOURCE_GROUP_NAME = "REPLACE_WITH_RESOURCE_GROUP_NAME"
    storage_account_name = "REPLACE_WITH_GLOBALLY_UNIQUE_STORAGE_ACCOUNT_NAME"
    
    
    
  • Get credentials and Obtain the management object for storage
  •     
    
    credential = AzureCliCredential()
    storage_client = StorageManagementClient(credential, subscription_id)
    
    
    
  • Create Azure Storage Account
  •     
    
    response = storage_client.storage_accounts.begin_create(
    resource_group_name,
    storage_account_name,
    StorageAccountCreateParameters(
        sku=Sku(name="Standard_LRS"),
        kind="Storage",
        location="westus",
        enable_https_traffic_only=True,
    ),
    )
    
    storage_account = response.result()
    
    print(
    f"Storage account {storage_account.name} provision state is {storage_account.provisioning_state}"
    )
            
    
    
  • Complete Code Snippet
  •     
    
    from azure.mgmt.storage import StorageManagementClient
    from azure.identity import AzureCliCredential
    from azure.mgmt.storage.models import StorageAccountCreateParameters, Sku
    
    subscription_id = "REPLACE_WITH_SUBSCRIPTION_ID"    
    RESOURCE_GROUP_NAME = "REPLACE_WITH_RESOURCE_GROUP_NAME"
    storage_account_name = "REPLACE_WITH_GLOBALLY_UNIQUE_STORAGE_ACCOUNT_NAME"
    
    credential = AzureCliCredential()
    storage_client = StorageManagementClient(credential, subscription_id)
    
    
    response = storage_client.storage_accounts.begin_create(
    resource_group_name,
    storage_account_name,
    StorageAccountCreateParameters(
        sku=Sku(name="Standard_LRS"),
        kind="Storage",
        location="westus",
        enable_https_traffic_only=True,
    ),
    )
    storage_account = response.result()
    
    print(
    f"Storage account {storage_account.name} provision state is {storage_account.provisioning_state}"
    )       
    
    
    

    Categories: Azure