Get started with Azure Blob Storage and Python
This article shows you how to connect to Azure Blob Storage by using the Azure Blob Storage client library for Python. Once connected, your code can operate on containers, blobs, and features of the Blob Storage service.
API reference | Package (PyPi) | Library source code | Samples | Give feedback
Prerequisites
- Azure subscription - create one for free
- Azure storage account - create a storage account
- Python 3.7+
Set up your project
This section walks you through preparing a project to work with the Azure Blob Storage client library for Python.
From your project directory, install packages for the Azure Blob Storage and Azure Identity client libraries using the pip install
command. The azure-identity package is needed for passwordless connections to Azure services.
pip install azure-storage-blob azure-identity
Then open your code file and add the necessary import statements. In this example, we add the following to our .py file:
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
Blob client library information:
- azure.storage.blob: Contains the primary classes (client objects) that you can use to operate on the service, containers, and blobs.
Authorize access and connect to Blob Storage
To connect an application to Blob Storage, create an instance of the BlobServiceClient class. This object is your starting point to interact with data resources at the storage account level. You can use it to operate on the storage account and its containers. You can also use the service client to create container clients or blob clients, depending on the resource you need to work with.
To learn more about creating and managing client objects, see Create and manage client objects that interact with data resources.
You can authorize a BlobServiceClient
object by using an Azure Active Directory (Azure AD) authorization token, an account access key, or a shared access signature (SAS).
To authorize with Azure AD, you'll need to use a security principal. Which type of security principal you need depends on where your application runs. Use the following table as a guide:
Where the application runs | Security principal | Guidance |
---|---|---|
Local machine (developing and testing) | Service principal | To learn how to register the app, set up an Azure AD group, assign roles, and configure environment variables, see Authorize access using developer service principals |
Local machine (developing and testing) | User identity | To learn how to set up an Azure AD group, assign roles, and sign in to Azure, see Authorize access using developer credentials |
Hosted in Azure | Managed identity | To learn how to enable managed identity and assign roles, see Authorize access from Azure-hosted apps using a managed identity |
Hosted outside of Azure (for example, on-premises apps) | Service principal | To learn how to register the app, assign roles, and configure environment variables, see Authorize access from on-premises apps using an application service principal |
Authorize access using DefaultAzureCredential
An easy and secure way to authorize access and connect to Blob Storage is to obtain an OAuth token by creating a DefaultAzureCredential instance. You can then use that credential to create a BlobServiceClient object.
The following example creates a BlobServiceClient
object using DefaultAzureCredential
:
def get_blob_service_client_token_credential(self):
# TODO: Replace <storage-account-name> with your actual storage account name
account_url = "https://<storage-account-name>.blob.core.windows.net"
credential = DefaultAzureCredential()
# Create the BlobServiceClient object
blob_service_client = BlobServiceClient(account_url, credential=credential)
return blob_service_client
Build your application
As you build applications to work with data resources in Azure Blob Storage, your code primarily interacts with three resource types: storage accounts, containers, and blobs. To learn more about these resource types, how they relate to one another, and how apps interact with resources, see Understand how apps interact with Blob Storage data resources.
The following guides show you how to work with data resources and perform specific actions using the Azure Storage client library for Python:
Guide | Description |
---|---|
Create a container | Create containers. |
Delete and restore containers | Delete containers, and if soft-delete is enabled, restore deleted containers. |
List containers | List containers in an account and the various options available to customize a listing. |
Manage properties and metadata (containers) | Get and set properties and metadata for containers. |
Create and manage container leases | Establish and manage a lock on a container. |
Create and manage blob leases | Establish and manage a lock on a blob. |
Upload blobs | Learn how to upload blobs by using strings, streams, file paths, and other methods. |
Download blobs | Download blobs by using strings, streams, and file paths. |
Copy blobs | Copy a blob from one location to another. |
List blobs | List blobs in different ways. |
Delete and restore | Delete blobs, and if soft-delete is enabled, restore deleted blobs. |
Find blobs using tags | Set and retrieve tags as well as use tags to find blobs. |
Manage properties and metadata (blobs) | Get and set properties and metadata for blobs. |
Set or change a blob's access tier | Set or change the access tier for a block blob. |
Feedback
Submit and view feedback for