page_type | languages | products | description | urlFragment | ||
---|---|---|---|---|---|---|
sample |
|
|
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. |
storage-python-manage |
Getting Started with Azure Storage Management in Python
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
-
If you don't already have it, install Python.
-
General recommendation for Python development is to use a Virtual Environment. For more information, see https://docs.python.org/3/tutorial/venv.html
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
-
Clone the repository.
git clone https://github.com/Azure-Samples/storage-python-manage.git
-
Install the dependencies using pip.
cd storage-python-manage pip install -r requirements.txt
-
Create an Azure service principal, using Azure CLI, PowerShell or Azure Portal.
-
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}
-
Run the sample.
python example.py
What is example.py 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 azure.mgmt.storage import StorageManagementClient
from azure.mgmt.storage.models import (
StorageAccountCreateParameters,
StorageAccountUpdateParameters,
Sku,
SkuName,
Kind
)
subscription_id = os.environ.get(
'AZURE_SUBSCRIPTION_ID',
'11111111-1111-1111-1111-111111111111') # your Azure Subscription Id
credentials = ServicePrincipalCredentials(
client_id=os.environ['AZURE_CLIENT_ID'],
secret=os.environ['AZURE_CLIENT_SECRET'],
tenant=os.environ['AZURE_TENANT_ID']
)
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(
GROUP_NAME,
STORAGE_ACCOUNT_NAME,
StorageAccountCreateParameters(
sku=Sku(name=SkuName.standard_ragrs),
kind=Kind.storage,
location='westus',
enable_https_traffic_only=True
)
)
storage_account = storage_async_operation.result()
Get the properties of a storage account
storage_account = storage_client.storage_accounts.get_properties(
GROUP_NAME, STORAGE_ACCOUNT_NAME)
List storage accounts
for item in storage_client.storage_accounts.list():
print_item(item)
List storage accounts by resource group
for item in storage_client.storage_accounts.list_by_resource_group(GROUP_NAME):
print_item(item)
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(
GROUP_NAME,
STORAGE_ACCOUNT_NAME,
'key1')
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(
GROUP_NAME, STORAGE_ACCOUNT_NAME,
StorageAccountUpdateParameters(
sku=Sku(name=SkuName.standard_grs)
)
)
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"):
print('\t{}'.format(usage.name.value))