Skip to content

Latest commit

 

History

History
108 lines (65 loc) · 5.5 KB

azure-sdk-example-storage.md

File metadata and controls

108 lines (65 loc) · 5.5 KB
title description ms.date ms.topic ms.custom
Provision Azure Storage with the Azure libraries for Python
Use the Azure SDK for Python libraries to provision a blob container in an Azure Storage account and then upload a file to that container.
06/24/2021
conceptual
devx-track-python, devx-track-azurecli

Example: Provision Azure Storage using the Azure libraries for Python

In this article, you learn how to use the Azure management libraries in a Python script to provision a resource group that contains and Azure Storage account and a Blob storage container. (Equivalent Azure CLI commands are given later in this article. If you prefer to use the Azure portal, see Create an Azure storage account and Create a blob container.)

After provisioning the resources, see Example: Use Azure Storage to use the Azure client libraries in Python application code to upload a file to the Blob storage container.

All the commands in this article work the same in Linux/macOS bash and Windows command shells unless noted.

1: Set up your local development environment

If you haven't already, follow all the instructions on Configure your local Python dev environment for Azure.

Be sure to create a service principal for local development, and create and activate a virtual environment for this project.

2: Install the needed Azure library packages

  1. Create a requirements.txt file that lists the management libraries used in this example:

    :::code language="txt" source="~/../python-sdk-docs-examples/storage/requirements_provision.txt":::

  2. In your terminal with the virtual environment activated, install the requirements:

    pip install -r requirements.txt

3: Write code to provision storage resources

This section describes how to provision storage resources from Python code. If you prefer, you can also provision resources through the Azure portal or through the equivalent Azure CLI commands.

Create a Python file named provision_blob.py with the following code. The comments explain the details:

:::code language="python" source="~/../python-sdk-docs-examples/storage/provision_blob.py":::

[!INCLUDE cli-auth-note]

Reference links for classes used in the code

4. Run the script

python provision_blob.py

The script will take a minute or two to complete.

5: Verify the resources

  1. Open the Azure portal to verify that the resource group and storage account were provisioned as expected. You may need to wait a minute and also select Show hidden types in the resource group to see a storage account provisioned from a Python script:

    Azure portal page for the new resource group, showing the storage account

  2. Select the storage account, then select Data storage > Containers in the left-hand menu to verify that the "blob-container-01" appears:

    Azure portal page for the storage account showing the blob container

  3. If you want to try using these provisioned resources from application code, continue with Example: Use Azure Storage.

For an additional example of using the Azure Storage management library, see the Manage Python Storage sample.

For reference: equivalent Azure CLI commands

The following Azure CLI commands complete the same provisioning steps as the Python script:

:::code language="azurecli" source="~/../python-sdk-docs-examples/storage/provision.cmd":::

:::code language="azurecli" source="~/../python-sdk-docs-examples/storage/provision.sh":::


6: Clean up resources

Leave the resources in place if you want to follow the article Example: Use Azure Storage to use these resources in app code.

Otherwise, run the following command to avoid ongoing charges in your subscription.

az group delete -n PythonAzureExample-Storage-rg  --no-wait

[!INCLUDE resource_group_begin_delete]

See also