working with azure blob storage python api

Author

Reads 188

Computer server in data center room
Credit: pexels.com, Computer server in data center room

Working with Azure Blob Storage Python API is a game-changer for developers who need to handle large amounts of unstructured data.

You can use the Azure Blob Storage Python API to store and retrieve binary data, such as images, videos, and documents, from a cloud-based repository.

With Azure Blob Storage, you can store up to 5 TB of data per container, which is perfect for large-scale applications.

Azure Blob Storage Python API allows you to upload files of any size, making it ideal for applications that require high-performance data transfer.

Setting Up

To set up your Azure Blob Storage Python API, start by creating a Python application named blob-quickstart.

You can do this on Windows by following the instructions in the setup process.

First, create a project directory and navigate to it in your terminal or command prompt.

Next, install packages for the Azure Blob Storage and Azure Identity client libraries using the pip install command.

The azure-identity package is necessary for passwordless connections to Azure services.

Open your code file and add the necessary import statements, such as importing the Azure Blob Storage and Azure Identity client libraries.

Broaden your view: Azure Identity Python

Azure Blob Storage Basics

Credit: youtube.com, 02. Azure using Python SDK: Access and upload files in Azure Blob Storage

The Azure Blob Service has three main components: the storage account itself, a container within the storage account, and a blob within a container.

To interact with these components, you'll need to use a dedicated client object. The Azure Storage Blobs client library for Python provides this functionality.

You can list blobs in your container using the BlobServiceClient class. This is a great way to get started with Azure Blob Storage.

The storage account is the top-level resource in the Azure Blob Service. It contains one or more containers, which in turn contain blobs.

You can manipulate Azure Storage resources and blob containers using the BlobServiceClient class. This includes listing blobs in your container, which can be done asynchronously.

Here's a quick rundown of the main components:

The ContainerClient class allows you to manipulate Azure Storage containers and their blobs. This is useful for managing your blob storage needs.

Authenticate and authorize in Azure

Authenticating to Azure Blob Storage is a crucial step in accessing and manipulating data. You can use the DefaultAzureCredential class provided by the Azure Identity client library to implement passwordless connections to Azure services.

Credit: youtube.com, 02. Azure using Python SDK: Access and upload files in Azure Blob Storage

The DefaultAzureCredential class supports multiple authentication methods and determines which method to use at runtime, enabling your app to use different authentication methods in different environments without implementing environment-specific code.

To authorize access to Azure Blob Storage, you can use a Microsoft Entra authorization token, an account access key, or a shared access signature (SAS). Azure recommends using Microsoft Entra ID with managed identities to authorize requests against blob data.

In most cases, it will take a minute or two for the role assignment to propagate in Azure, but in rare cases, it may take up to eight minutes. If you receive authentication errors when you first run your code, wait a few moments and try again.

To assign a role at the resource level using the Azure CLI, you first must retrieve the resource id using the az storage account show command. You can filter the output properties using the --query parameter.

Here are the different types of credentials you can use to authenticate and authorize in Azure:

You can also use the account access key to authorize requests to Azure Blob Storage, but this approach should be used with caution, as anyone who has the access key is able to authorize requests against the storage account and effectively has access to all the data.

Related reading: Azure Openai Api Key

Storage Connection

Credit: youtube.com, Getting Started With Azure Storage API In Python For Beginners

To connect to Azure Blob Storage using the Python API, you'll need to create a storage connection. You can do this by signing in to the Azure portal and locating your storage account. From there, navigate to the "Access Keys" section to view the account access keys and the complete connection string for each key.

The connection string includes the storage account access key and uses it to authorize requests. Always be careful to never expose the keys in an unsecure location.

To authorize data access with the storage account access key, you'll need permissions for the Azure RBAC action: Microsoft.Storage/storageAccounts/listkeys/action. The least privileged built-in role with permissions for this action is Reader and Data Access.

You can find the connection string for your storage account using the az storage account show-connection-string command or by using PowerShell with the Get-AzStorageAccount and Get-AzStorageAccountKey commands.

Here are the steps to create an environment variable for the connection string:

  1. Sign in to the Azure portal.
  2. Locate your storage account.
  3. In the storage account menu pane, under Security + networking, select Access keys.
  4. In the Access keys pane, select Show keys.
  5. Locate the Connection string value and copy it to the clipboard.

After creating the environment variable, you can use the connection string to construct a service client object.

The account access key should be used with caution, as anyone who has the access key can authorize requests against the storage account and effectively have access to all the data.

Suggestion: Azure Api Key

Containers and Blobs

Credit: youtube.com, A Beginners Guide to Azure Blob Storage

Creating a container is a straightforward process in Azure Blob Storage. You can create a new container by calling the create_container method on the blob_service_client object, appending a GUID value to the container name to ensure it's unique.

Container names must be in lowercase, as specified in the documentation. This is an important consideration when naming your containers and blobs.

You can then upload blobs to the container using the upload_blob method, which allows you to store text and binary data. This method is especially useful for creating text files in the local data directory to upload to the container.

Here are the different types of blobs you can choose from:

  • Block blobs: store text and binary data, up to approximately 4.75 TiB.
  • Append blobs: are made up of blocks like block blobs, but are optimized for append operations.
  • Page blobs: store random access files up to 8 TiB in size.

To list the blobs in a container, you can call the list_blobs method, which returns a listing of the blobs in the container. This method is especially useful when you need to list the blobs asynchronously.

For another approach, see: Azure Container Storage

Blob Types

Blob Types are an essential part of working with Azure storage. You can choose from three main types: Block blobs, Append blobs, and Page blobs.

Explore further: Azure Storage Type

Credit: youtube.com, Blob Storage

Block blobs store text and binary data up to approximately 4.75 TiB. This makes them suitable for a wide range of use cases.

Append blobs are optimized for append operations, making them ideal for scenarios like logging data from virtual machines. They're also made up of blocks like Block blobs.

Page blobs store random access files up to 8 TiB in size. This makes them particularly useful for storing virtual hard drive (VHD) files and serving as disks for Azure virtual machines.

Here's a quick rundown of the different blob types:

Create A Container

To create a container, you need to call the create_container method on the blob_service_client object.

This method allows you to create a new container in your storage account, and it's a good idea to append a GUID value to the container name to ensure it's unique.

Container names must be lowercase, so make sure to follow this naming convention.

You can learn more about creating a container and explore more code samples by checking out the "Create a blob container with Python" page.

List container blobs

Credit: youtube.com, List Containers and BLOBs using BlobServiceClient, ContainerClient #data #dataengineering

Listing container blobs is a straightforward process. You can call the list_blobs method to retrieve the list of blobs in a container.

To list the blobs, you can use the list_blobs method, which returns the list of blobs in the container. In fact, if you've only added one blob to the container, the listing operation will return just that one blob.

Listing blobs can be done synchronously by calling the list_blobs method. However, you can also list blobs asynchronously, which can be useful if you need to perform other tasks while waiting for the listing operation to complete.

You can explore more code samples on listing blobs with Python to get a better understanding of the process.

Download Blobs

Download blobs by calling the download_blob method. This method allows you to access the previously created blob.

You can add a suffix to the file name to distinguish it from the original blob. For example, adding "DOWNLOAD" to the file name makes it easy to see both files in your local file system.

To learn more about downloading blobs, check out the "Download a blob with Python" guide for additional code samples.

For more insights, see: Azure Blob Storage Download

Prerequisites and Setup

Credit: youtube.com, Getting Started With Azure Storage API In Python For Beginners

To get started with the Azure Blob Storage Python API, you'll need to meet some prerequisites.

You'll need Python 3.8 or later to use this package. For more details, please read our page on Azure SDK for Python version support policy.

You'll also need an Azure subscription and an Azure storage account to use this package.

Here are the specific requirements in a concise list:

  • Python 3.8 or later
  • Azure subscription
  • Azure storage account

Once you have these prerequisites covered, you can move on to setting up your project.

To set up your project, create a Python application named blob-quickstart. If you're on Windows, you'll need to follow the specific instructions for your operating system.

From your project directory, you'll need to 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.

You'll also need to add the necessary import statements to your code file. In this example, you'll need to add the following to your .py file:

Client Configuration

Credit: youtube.com, python api azure

Client configuration is crucial when working with the Azure Blob Storage Python API. The Azure Storage Blobs client library for Python allows you to interact with three types of resources: the storage account itself, blob storage containers, and blobs.

To create a client object, you will need the storage account's blob service account URL and a credential that allows you to access the storage account. This can be done by using the DefaultAzureCredential class, which provides enhanced security features and benefits.

Four different clients are provided to interact with the various components of the Blob Service: BlobServiceClient - this client represents interaction with the Azure storage account itself, and allows you to acquire preconfigured client instances to access the containers and blobs within.ContainerClient - this client represents interaction with a specific container (which need not exist yet), and allows you to acquire preconfigured client instances to access the blobs within.BlobClient - this client represents interaction with a specific blob (which need not exist yet). It provides operations to upload, download, delete, and create snapshots of a blob, as well as specific operations per blob type.BlobLeaseClient - this client represents lease interactions with a ContainerClient or BlobClient. It provides operations to acquire, renew, release, change, and break a lease on a specified resource.

Related reading: Create Blob Storage Azure

More Sample Code

Computer server in data center room
Credit: pexels.com, Computer server in data center room

If you're looking for more sample code to help you get started with Azure Blob Storage, you're in luck. There are several Storage Blobs Python SDK samples available in the SDK's GitHub repository.

These samples provide example code for additional scenarios commonly encountered while working with Storage Blobs. For instance, you can find code examples for setting Access policies, common Storage Blob tasks, authenticating and creating the client, interacting with the blob service, and more.

Some of the samples include async versions, such as blob_samples_container_access_policy.py, blob_samples_hello_world.py, blob_samples_authentication.py, blob_samples_service.py, blob_samples_containers.py, blob_samples_common.py, and blob_samples_async.py.

Here are some of the specific samples you can find in the repository:

  • blob_samples_container_access_policy.py (async version) - Examples to set Access policies:
  • blob_samples_hello_world.py (async version) - Examples for common Storage Blob tasks:
  • blob_samples_authentication.py (async version) - Examples for authenticating and creating the client:
  • blob_samples_service.py (async version) - Examples for interacting with the blob service:
  • blob_samples_containers.py (async version) - Examples for interacting with containers:
  • blob_samples_common.py (async version) - Examples common to all types of blobs:
  • blob_samples_directory_interface.py - Examples for interfacing with Blob storage as if it were a directory on a filesystem:

Configure Client/Operation

You can configure the client or per-operation with various keyword arguments. The client keyword arguments include connection_timeout and read_timeout, which specify the number of seconds the client will wait to establish a connection to the server and wait for a response from the server, respectively.

Connection timeout defaults to 20 seconds, while read timeout defaults to 60 seconds. You can also specify a custom transport to send the HTTP request. Per-operation keyword arguments include raw_response_hook and raw_request_hook, which allow you to use the response returned from the service and the request before being sent to the service, respectively.

You can also specify a client_request_id, user_agent, logging_enable, and logging_body, which enable logging at the DEBUG level, append a custom value to the user-agent header, and log the request and response body, respectively. Additionally, you can pass in custom headers as key, value pairs using the headers argument.

Asynchronous Programming and Logging

Credit: youtube.com, Azure Functions using Python: Exploring HTTP Streaming and Azure Blob Storage

Asynchronous programming is a key feature of the Azure Blob Storage client library for Python. You can install an async transport like aiohttp along with azure-storage-blob using an optional dependency install command.

To use asynchronous APIs in your project, you'll need to import the necessary modules, including asyncio, DefaultAzureCredential, and the BlobServiceClient, BlobClient, and ContainerClient classes. The import asyncio statement is only required if you're using the library in your code.

The asynchronous APIs are based on Python's asyncio library, and you can create a client object using async with to begin working with data resources. Only the top-level client needs to use async with, as other clients created from it share the same connection pool.

Here are the logging levels you can use to make debugging easier, along with their recommended uses:

  • DEBUG: log strings to sign
  • INFO: log outgoing requests and responses, as well as retry attempts
  • WARNING: not used
  • ERROR: log calls that still failed after all the retries

Asynchronous Programming

Asynchronous programming is a powerful technique that allows your code to run multiple tasks concurrently, improving performance and responsiveness.

Credit: youtube.com, What Are The Best Practices For Logging Asynchronous Code? - Next LVL Programming

To use asynchronous APIs in your project, you'll need to install an async transport like aiohttp. You can do this with an optional dependency install command using pip.

Here's the pip install command you can use: pip install azure-storage-blob[aio]. This will install aiohttp along with the Azure Blob Storage client library for Python.

The Azure Blob Storage client library for Python supports both synchronous and asynchronous APIs, with the latter based on Python's asyncio library.

You can import the necessary classes from the library using the following code: import asyncio from azure.identity.aio import DefaultAzureCredential from azure.storage.blob.aio import BlobServiceClient, BlobClient, ContainerClient.

To work with data resources, you'll need to create a client object using async with. This only needs to be done for the top-level client, as other clients created from it share the same connection pool.

Here are the steps to create a client object: async with BlobServiceClient(account_url, credential=credential) as blob_service_client: container_client = blob_service_client.get_container_client(container="sample-container").

The azure.storage.blob.aio module contains the primary classes you can use to operate on the service, containers, and blobs asynchronously.

On a similar theme: Object Storage Google

Logging

Credit: youtube.com, How To Log Asynchronous Code Effectively For Debugging? - Next LVL Programming

Logging is an essential tool for debugging and understanding what's happening in your asynchronous code.

Using the right logging level can make a big difference. It's recommended to use INFO logging for the 'azure.storage' logger. This will help you see outgoing requests and responses, as well as retry attempts, which can be really helpful for debugging.

Here are the different logging levels and what they do:

  • DEBUG: log strings to sign
  • INFO: log outgoing requests and responses, as well as retry attempts
  • WARNING: not used
  • ERROR: log calls that still failed after all the retries

By using INFO logging, you'll get a clear picture of what's happening in your code, making it easier to identify and fix issues.

Authorization and Access

To authorize access to Azure Blob Storage, you must use the DefaultAzureCredential class provided by the Azure Identity client library. This class supports multiple authentication methods and determines which method should be used at runtime.

Using the DefaultAzureCredential class enables your app to use different authentication methods in different environments without implementing environment-specific code.

The order and locations in which DefaultAzureCredential looks for credentials can be found in the Azure Identity library overview.

Credit: youtube.com, Stored Access Policy Vs Shared Access Signature (SAS) - Azure Blob Storage Access Permissions

You can also authorize access to Azure Blob Storage by using the account access key, but this approach should be used with caution as it exposes the key in an unsecure location.

In most cases, it will take a minute or two for the role assignment to propagate in Azure, but in rare cases it may take up to eight minutes.

To assign a role at the resource level using the Azure CLI, you first must retrieve the resource id using the az storage account show command.

To authorize access and connect 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 authorize a BlobServiceClient object by using a Microsoft Entra authorization token, an account access key, or a shared access signature (SAS). For optimal security, Azure recommends using Microsoft Entra ID with managed identities to authorize requests against blob data.

The following table summarizes the types of security principals you need depending on where your app runs:

General and Advanced Topics

Credit: youtube.com, A beginners guide to azure blob storage

Azure Blob Storage Python API offers robust support for general and advanced topics.

You can use the Azure Blob Storage Python API to upload large files, up to 4.8 TB in size, using the block blob API.

With Azure Blob Storage, you can store and serve large amounts of unstructured data, such as images, videos, and audio files, making it a great option for media storage.

The Azure Blob Storage Python API also supports advanced features like data encryption, which can be enabled using the `encryption` parameter when creating a blob client.

See what others are reading: Gdrive Large Size Movies

Retry Policy Configuration

When configuring the retry policy, it's essential to understand the various options available.

The total number of retries is determined by the `retry_total` argument, which defaults to 10 if not specified.

You can also configure the number of retries for connection-related errors with the `retry_connect` argument, which defaults to 3.

For read errors, you can use the `retry_read` argument, which also defaults to 3.

Credit: youtube.com, Mastering Polly Library: Ensuring Effective Retry Logic with WaitAndRetryAsync

Similarly, the `retry_status` argument determines the number of retries for bad status codes, with a default value of 3.

If you're using RA-GRS accounts and potentially stale data can be handled, you can enable the `retry_to_secondary` argument, which defaults to False.

Here's a summary of the retry policy configuration options:

Encryption Configuration

Encryption Configuration is crucial for ensuring the security of your data. You can configure encryption when instantiating a client using specific keyword arguments.

To enforce encryption, set the `require_encryption` argument to `True`. This will ensure that objects are encrypted and decrypted as needed.

The `encryption_version` argument specifies the version of encryption to use. The current options are '2.0' or '1.0', with '2.0' being the recommended version due to its deprecation.

You can specify the `key_encryption_key` object, which must implement specific methods for key encryption.

A `key_resolver_function` is also required, which uses the kid string to return a key-encryption-key implementing the interface defined above.

Here are the specific keyword arguments you can use to configure encryption:

  • require_encryption (bool): Enforces encryption for objects.
  • encryption_version (str): Specifies the encryption version, with '2.0' being the recommended choice.
  • key_encryption_key (object): Provides a user-provided key-encryption-key.
  • key_resolver_function (callable): Uses the kid string to return a key-encryption-key implementing the interface.

General

Modern data center corridor with server racks and computer equipment. Ideal for technology and IT concepts.
Credit: pexels.com, Modern data center corridor with server racks and computer equipment. Ideal for technology and IT concepts.

Storage Blob clients raise exceptions defined in Azure Core, which can be used for reference to catch thrown exceptions.

You can use the error_code attribute to get the specific error code of the exception, for example, exception.error_code.

Some exceptions are raised by Storage Blob clients, and you can reference the list of exceptions to handle them properly.

By catching and handling exceptions, you can ensure that your application remains stable and provides a good user experience, even in the face of errors.

Margarita Champlin

Writer

Margarita Champlin is a seasoned writer with a passion for crafting informative and engaging content. With a keen eye for detail and a knack for simplifying complex topics, she has established herself as a go-to expert in the field of technology. Her writing has been featured in various publications, covering a range of topics, including Azure Monitoring.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.