Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Get started with Azure Blob Storage and Python

Feedback

In this article

This article shows you how to connect to Azure Blob Storage by using the Azure Blob Storage client library for Python. Once connected, use thedeveloper guides to learn how your code can operate on containers, blobs, and features of the Blob Storage service.

If you're looking to start with a complete example, seeQuickstart: Azure Blob Storage client library for Python.

API reference |Package (PyPi) |Library source code |Samples |Give feedback

Prerequisites

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 thepip install command. Theazure-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 DefaultAzureCredentialfrom 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.

Asynchronous programming

The Azure Blob Storage client library for Python supports both synchronous and asynchronous APIs. The asynchronous APIs are based on Python'sasyncio library.

Follow these steps to use the asynchronous APIs in your project:

  • Install an async transport, such asaiohttp. You can installaiohttp along withazure-storage-blob by using an optional dependency install command. In this example, we use the followingpip install command:

    pip install azure-storage-blob[aio]
  • Open your code file and add the necessary import statements. In this example, we add the following to our.py file:

    import asynciofrom azure.identity.aio import DefaultAzureCredentialfrom azure.storage.blob.aio import BlobServiceClient, BlobClient, ContainerClient

    Theimport asyncio statement is only required if you're using the library in your code. It's added here for clarity, as the examples in thedeveloper guide articles use theasyncio library.

  • Create a client object usingasync with to begin working with data resources. Only the top level client needs to useasync with, as other clients created from it share the same connection pool. In this example, we create aBlobServiceClient object usingasync with, and then create aContainerClient object:

    async with BlobServiceClient(account_url, credential=credential) as blob_service_client:    container_client = blob_service_client.get_container_client(container="sample-container")

    To learn more, see the async examples inAuthorize access and connect to Blob Storage.

Blob async client library information:

  • azure.storage.blob.aio: Contains the primary classes that you can use to operate on the service, containers, and blobs asynchronously.

Authorize access and connect to Blob Storage

To connect an app to Blob Storage, create an instance of theBlobServiceClient 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, including best practices, seeCreate and manage client objects that interact with data resources.

You can authorize aBlobServiceClient object by using a Microsoft Entra authorization token, an account access key, or a shared access signature (SAS). For optimal security, Microsoft recommends using Microsoft Entra ID with managed identities to authorize requests against blob data. For more information, seeAuthorize access to blobs using Microsoft Entra ID.

To authorize with Microsoft Entra ID, you need to use asecurity principal. Which type of security principal you need depends on where your app runs. Use the following table as a guide:

Where the app runsSecurity principalGuidance
Local machine (developing and testing)Service principalTo learn how to register the app, set up a Microsoft Entra group, assign roles, and configure environment variables, seeAuthorize access using developer service principals
Local machine (developing and testing)User identityTo learn how to set up a Microsoft Entra group, assign roles, and sign in to Azure, seeAuthorize access using developer credentials
Hosted in AzureManaged identityTo learn how to enable managed identity and assign roles, seeAuthorize access from Azure-hosted apps using a managed identity
Hosted outside of Azure (for example, on-premises apps)Service principalTo learn how to register the app, assign roles, and configure environment variables, seeAuthorize 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 aDefaultAzureCredential instance. You can then use that credential to create aBlobServiceClient object.

The following example creates aBlobServiceClient object usingDefaultAzureCredential:

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

If your project uses asynchronous APIs, instantiateBlobServiceClient usingasync with:

# TODO: Replace <storage-account-name> with your actual storage account nameaccount_url = "https://<storage-account-name>.blob.core.windows.net"credential = DefaultAzureCredential()async with BlobServiceClient(account_url, credential=credential) as blob_service_client:    # Work with data resources in the storage account

Build your app

As you build apps 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, seeUnderstand how apps interact with Blob Storage data resources.

The following guides show you how to access data and perform specific actions using the Azure Storage client library for Python:

GuideDescription
Configure a retry policyImplement retry policies for client operations.
Copy blobsCopy a blob from one location to another.
Create a containerCreate blob containers.
Create a user delegation SASCreate a user delegation SAS for a container or blob.
Create and manage blob leasesEstablish and manage a lock on a blob.
Create and manage container leasesEstablish and manage a lock on a container.
Delete and restore blobsDelete blobs and restore soft-deleted blobs.
Delete and restore containersDelete containers and restore soft-deleted containers.
Download blobsDownload blobs by using strings, streams, and file paths.
Find blobs using tagsSet and retrieve tags, and use tags to find blobs.
List blobsList blobs in different ways.
List containersList containers in an account and the various options available to customize a listing.
Manage properties and metadata (blobs)Get and set properties and metadata for blobs.
Manage properties and metadata (containers)Get and set properties and metadata for containers.
Performance tuning for data transfersOptimize performance for data transfer operations.
Set or change a blob's access tierSet or change the access tier for a block blob.
Upload blobsLearn how to upload blobs by using strings, streams, file paths, and other methods.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?