Microsoft Azure Client Libraries for Python (Meta-package)

5.0.0 · deprecated · verified Sat Apr 11

The `azure` package is a meta-package that historically bundled a wide range of Microsoft Azure client and management libraries for Python. As of version 5.0.0, this meta-package is officially deprecated. Users are strongly advised to install individual service-specific packages (e.g., `azure-storage-blob`, `azure-identity`, `azure-mgmt-compute`) based on their needs, rather than the monolithic `azure` package. The individual Azure SDK for Python client libraries follow a continuous release cadence, with updates often monthly for various services, while adhering to common Azure SDK design guidelines for consistency.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to Azure Blob Storage using the recommended `DefaultAzureCredential` for authentication and perform basic operations like creating a container, uploading a blob, and downloading its content. It requires the `azure-storage-blob` and `azure-identity` packages. Ensure you have an Azure Storage Account and have configured `DefaultAzureCredential` (e.g., by logging in via Azure CLI: `az login` or setting environment variables like `AZURE_STORAGE_ACCOUNT_NAME`).

import os
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient

# Replace with your storage account name or retrieve from environment variables
STORAGE_ACCOUNT_NAME = os.environ.get('AZURE_STORAGE_ACCOUNT_NAME', 'yourstorageaccountname')
CONTAINER_NAME = 'my-container'
BLOB_NAME = 'hello_world.txt'

try:
    # Authenticate using DefaultAzureCredential (recommended passwordless approach)
    credential = DefaultAzureCredential()
    
    # Construct the blob service client
    blob_service_client = BlobServiceClient(account_url=f"https://{STORAGE_ACCOUNT_NAME}.blob.core.windows.net/", credential=credential)

    # Get a client to interact with a specific container
    container_client = blob_service_client.get_container_client(CONTAINER_NAME)
    
    # Create the container if it doesn't exist
    try:
        container_client.create_container()
        print(f"Container '{CONTAINER_NAME}' created.")
    except Exception as e:
        if "ContainerAlreadyExists" in str(e): # Specific exception for idempotent create
            print(f"Container '{CONTAINER_NAME}' already exists.")
        else:
            raise

    # Upload data to a blob
    data = "Hello, Azure Blob Storage! This is a test blob."
    blob_client = container_client.get_blob_client(BLOB_NAME)
    blob_client.upload_blob(data, overwrite=True)
    print(f"Blob '{BLOB_NAME}' uploaded to container '{CONTAINER_NAME}'.")

    # Download the blob data
    downloaded_blob = blob_client.download_blob().readall()
    print(f"Downloaded blob content: {downloaded_blob.decode('utf-8')}")

    # Clean up (optional: delete the blob and container)
    # blob_client.delete_blob()
    # print(f"Blob '{BLOB_NAME}' deleted.")
    # container_client.delete_container()
    # print(f"Container '{CONTAINER_NAME}' deleted.")

except Exception as ex:
    print(f"An error occurred: {ex}")

view raw JSON →