Quickstart: Azure Blob storage client library v12 for Python

Get started with the Azure Blob storage client library v12 for Python. Azure Blob storage is Microsoft's object storage solution for the cloud. Follow steps to install the package and try out example code for basic tasks. Blob storage is optimized for storing massive amounts of unstructured data.

Note

To get started with the previous SDK version, see Quickstart: Azure Blob storage client library for Python.

Use the Azure Blob storage client library to:

  • Create a container
  • Upload a blob to Azure Storage
  • List all of the blobs in a container
  • Download the blob to your local computer
  • Delete a container

API reference documentation | Library source code | Package (Python Package Index) | Samples

Note

The features described in this article are now available to accounts that have a hierarchical namespace. To review limitations, see the Known issues with Azure Data Lake Storage Gen2 article.

Prerequisites

Setting up

This section walks you through preparing a project to work with the Azure Blob storage client library v12 for Python.

Create the project

Create a Python application named blob-quickstart-v12.

  1. In a console window (such as cmd, PowerShell, or Bash), create a new directory for the project.

    mkdir blob-quickstart-v12
    
  2. Switch to the newly created blob-quickstart-v12 directory.

    cd blob-quickstart-v12
    
  3. In side the blob-quickstart-v12 directory, create another directory called data. This is where the blob data files will be created and stored.

    mkdir data
    

Install the package

While still in the application directory, install the Azure Blob storage client library for Python package by using the pip install command.

pip install azure-storage-blob

This command installs the Azure Blob storage client library for Python package and all the libraries on which it depends. In this case, that is just the Azure core library for Python.

Set up the app framework

From the project directory:

  1. Open a new text file in your code editor

  2. Add import statements

  3. Create the structure for the program, including very basic exception handling

    Here's the code:

    import os, uuid
    from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
    
    try:
        print("Azure Blob storage v12 - Python quickstart sample")
        # Quick start code goes here
    except Exception as ex:
        print('Exception:')
        print(ex)
    
  4. Save the new file as blob-quickstart-v12.py in the blob-quickstart-v12 directory.

Copy your credentials from the Azure portal

When the sample application makes a request to Azure Storage, it must be authorized. To authorize a request, add your storage account credentials to the application as a connection string. View your storage account credentials by following these steps:

  1. Sign in to the Azure portal.

  2. Locate your storage account.

  3. In the Settings section of the storage account overview, select Access keys. Here, you can view your account access keys and the complete connection string for each key.

  4. Find the Connection string value under key1, and select the Copy button to copy the connection string. You will add the connection string value to an environment variable in the next step.

    Screenshot showing how to copy a connection string from the Azure portal

Configure your storage connection string

After you have copied your connection string, write it to a new environment variable on the local machine running the application. To set the environment variable, open a console window, and follow the instructions for your operating system. Replace <yourconnectionstring> with your actual connection string.

Windows

setx CONNECT_STR "<yourconnectionstring>"

After you add the environment variable in Windows, you must start a new instance of the command window.

Linux

export CONNECT_STR="<yourconnectionstring>"

macOS

export CONNECT_STR="<yourconnectionstring>"

Restart programs

After you add the environment variable, restart any running programs that will need to read the environment variable. For example, restart your development environment or editor before continuing.

Object model

Azure Blob storage is optimized for storing massive amounts of unstructured data. Unstructured data is data that does not adhere to a particular data model or definition, such as text or binary data. Blob storage offers three types of resources:

  • The storage account
  • A container in the storage account
  • A blob in the container

The following diagram shows the relationship between these resources.

Diagram of Blob storage architecture

Use the following Python classes to interact with these resources:

  • BlobServiceClient: The BlobServiceClient class allows you to manipulate Azure Storage resources and blob containers.
  • ContainerClient: The ContainerClient class allows you to manipulate Azure Storage containers and their blobs.
  • BlobClient: The BlobClient class allows you to manipulate Azure Storage blobs.

Code examples

These example code snippets show you how to perform the following with the Azure Blob storage client library for Python:

Get the connection string

The code below retrieves the connection string for the storage account from the environment variable created in the Configure your storage connection string section.

Add this code inside the try block:

# Retrieve the connection string for use with the application. The storage
# connection string is stored in an environment variable on the machine
# running the application called CONNECT_STR. If the environment variable is
# created after the application is launched in a console or with Visual Studio,
# the shell or application needs to be closed and reloaded to take the
# environment variable into account.
connect_str = os.getenv('CONNECT_STR')

Create a container

Decide on a name for the new container. The code below appends a UUID value to the container name to ensure that it is unique.

Important

Container names must be lowercase. For more information about naming containers and blobs, see Naming and Referencing Containers, Blobs, and Metadata.

Create an instance of the BlobServiceClient class by calling the from_connection_string method. Then, call the create_container method to actually create the container in your storage account.

Add this code to the end of the try block:

# Create the BlobServiceClient object which will be used to create a container client
blob_service_client = BlobServiceClient.from_connection_string(connect_str)

# Create a unique name for the container
container_name = "quickstart" + str(uuid.uuid4())

# Create the container
container_client = blob_service_client.create_container(container_name)

Upload blobs to a container

The following code snippet:

  1. Creates a text file in the local directory.
  2. Gets a reference to a BlobClient object by calling the get_blob_client method on the BlobServiceClient from the Create a container section.
  3. Uploads the local text file to the blob by calling the upload_blob method.

Add this code to the end of the try block:

# Create a file in local Documents directory to upload and download
local_path = "./data"
local_file_name = "quickstart" + str(uuid.uuid4()) + ".txt"
upload_file_path = os.path.join(local_path, local_file_name)

# Write text to the file
file = open(upload_file_path, 'w')
file.write("Hello, World!")
file.close()

# Create a blob client using the local file name as the name for the blob
blob_client = blob_service_client.get_blob_client(container=container_name, blob=local_file_name)

print("\nUploading to Azure Storage as blob:\n\t" + local_file_name)

# Upload the created file
with open(upload_file_path, "rb") as data:
    blob_client.upload_blob(data)

List the blobs in a container

List the blobs in the container by calling the list_blobs method. In this case, only one blob has been added to the container, so the listing operation returns just that one blob.

Add this code to the end of the try block:

print("\nListing blobs...")

# List the blobs in the container
blob_list = container_client.list_blobs()
for blob in blob_list:
    print("\t" + blob.name)

Download blobs

Download the previously created blob by calling the download_blob method. The example code adds a suffix of "DOWNLOAD" to the file name so that you can see both files in local file system.

Add this code to the end of the try block:

# Download the blob to a local file
# Add 'DOWNLOAD' before the .txt extension so you can see both files in Documents
download_file_path = os.path.join(local_path, str.replace(local_file_name ,'.txt', 'DOWNLOAD.txt'))
print("\nDownloading blob to \n\t" + download_file_path)

with open(download_file_path, "wb") as download_file:
    download_file.write(blob_client.download_blob().readall())

Delete a container

The following code cleans up the resources the app created by removing the entire container using the ​delete_container method. You can also delete the local files, if you like.

The app pauses for user input by calling input() before it deletes the blob, container, and local files. This is a good chance to verify that the resources were actually created correctly, before they are deleted.

Add this code to the end of the try block:

# Clean up
print("\nPress the Enter key to begin clean up")
input()

print("Deleting blob container...")
container_client.delete_container()

print("Deleting the local source and downloaded files...")
os.remove(upload_file_path)
os.remove(download_file_path)

print("Done")

Run the code

This app creates a test file in your local folder and uploads it to Blob storage. The example then lists the blobs in the container and downloads the file with a new name so that you can compare the old and new files.

Navigate to the directory containing the blob-quickstart-v12.py file, then execute the following python command to run the app.

python blob-quickstart-v12.py

The output of the app is similar to the following example:

Azure Blob storage v12 - Python quickstart sample

Uploading to Azure Storage as blob:
        quickstartcf275796-2188-4057-b6fb-038352e35038.txt

Listing blobs...
        quickstartcf275796-2188-4057-b6fb-038352e35038.txt

Downloading blob to
        ./data/quickstartcf275796-2188-4057-b6fb-038352e35038DOWNLOAD.txt

Press the Enter key to begin clean up

Deleting blob container...
Deleting the local source and downloaded files...
Done

Before you begin the clean up process, check your Documents folder for the two files. You can open them and observe that they are identical.

After you've verified the files, press the Enter key to delete the test files and finish the demo.

Next steps

In this quickstart, you learned how to upload, download, and list blobs using Python.

To see Blob storage sample apps, continue to: