Getting Started with Azure Storage Management in Python

Build Status

This sample shows how to manage your storage account using the Azure Storage Management package for Python. The Storage Resource Provider is a client library for working with the storage accounts in your Azure subscription. Using the client library, you can create a new storage account, read its properties, list all storage accounts in a given subscription or resource group, read and regenerate the storage account keys, and delete a storage account.

On this page

Run this sample

  1. If you don't already have it, install Python.

  2. General recommendation for Python development is to use a Virtual Environment. For more information, see

    Install and initialize the virtual environment with the "venv" module on Python 3 (you must install virtualenv for Python 2.7):

    python -m venv mytestenv # Might be "python3" or "py -3.6" depending on your Python installation
    cd mytestenv
    source bin/activate      # Linux shell (Bash, ZSH, etc.) only
    ./scripts/activate       # PowerShell only
    ./scripts/activate.bat   # Windows CMD only
  3. Clone the repository.

    git clone
  4. Install the dependencies using pip.

    cd storage-python-manage
    pip install -r requirements.txt
  5. Create an Azure service principal, using Azure CLI, PowerShell or Azure Portal.

  6. Export these environment variables into your current shell.

    export AZURE_TENANT_ID={your tenant id}
    export AZURE_CLIENT_ID={your client id}
    export AZURE_CLIENT_SECRET={your client secret}
    export AZURE_SUBSCRIPTION_ID={your subscription id}
  7. Run the sample.


What is doing?

The sample walks you through several Storage Resource Provider operations. It starts by setting up a ResourceManagementClient and StorageManagementClient objects using your subscription and credentials.

import os
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from import StorageManagementClient
from import StorageAccountCreateParameters

subscription_id = os.environ.get(
    '11111111-1111-1111-1111-111111111111') # your Azure Subscription Id
credentials = ServicePrincipalCredentials(
resource_client = ResourceManagementClient(credentials, subscription_id)
storage_client = StorageManagementClient(credentials, subscription_id)

It also sets up a ResourceGroup object (resource_group_params) to be used as a parameter in some of the API calls.

resource_group_params = {'location':'westus'}

There are a couple of supporting functions (print_item and print_properties) that print an Azure object and its properties.

Check storage account name availability

Check the availability and/or the validity of a given string as a storage account.

bad_account_name = 'invalid-or-used-name'
availability = storage_client.storage_accounts.check_name_availability(bad_account_name)
print('The account {} is available: {}'.format(bad_account_name, availability.name_available))
print('Reason: {}'.format(availability.reason))
print('Detailed message: {}'.format(availability.message))

Create a new storage account

storage_async_operation = storage_client.storage_accounts.create(
storage_account = storage_async_operation.result()

Get the properties of a storage account

storage_account = storage_client.storage_accounts.get_properties(

List storage accounts

for item in storage_client.storage_accounts.list():

List storage accounts by resource group

for item in storage_client.storage_accounts.list_by_resource_group(GROUP_NAME):

Get the storage account keys

storage_keys = storage_client.storage_accounts.list_keys(GROUP_NAME, STORAGE_ACCOUNT_NAME)
storage_keys = {v.key_name: v.value for v in storage_keys.keys}
print('\tKey 1: {}'.format(storage_keys['key1']))
print('\tKey 2: {}'.format(storage_keys['key2']))

Regenerate a storage account key

storage_keys = storage_client.storage_accounts.regenerate_key(
storage_keys = {v.key_name: v.value for v in storage_keys.keys}
print('\tNew key 1: {}'.format(storage_keys['key1']))

Modify the storage account SKU

The storage account SKU specifies what type of replication applies to the storage account. You can update the storage account SKU to change how the storage account is replicated, as shown in the sample:

storage_account = storage_client.storage_accounts.update(

Note that modifying the SKU for a production storage account may have associated costs. For example, if you convert a locally redundant storage account to a geo-redundant storage account, you will be charged for replicating your data to the secondary region. Before you modify the SKU for a production account, be sure to consider any cost implications. See Azure Storage replication for additional information about storage replication.

Delete a storage account

storage_client.storage_accounts.delete(GROUP_NAME, STORAGE_ACCOUNT_NAME)

List usages

for usage in storage_client.usages.list_by_location("westus"):

More information