Getting Started with Azure SDK for Python

Welcome to the Azure SDK for Python! This guide will help you set up your environment and make your first call to an Azure service.

1. Installation

You can install the core SDK libraries and specific service libraries using pip. The core libraries provide common authentication and configuration mechanisms.

pip install azure-identity azure-storage-blob

For a full list of available packages, visit the Azure SDK PyPI index.

2. Authentication

The Azure SDK for Python supports multiple authentication methods, including environment variables, managed identities, and service principals. The azure-identity library simplifies this process.

Using Environment Variables (Recommended for local development):

  1. Set the following environment variables:
    • AZURE_CLIENT_ID: Your Azure AD application client ID.
    • AZURE_TENANT_ID: Your Azure AD tenant ID.
    • AZURE_CLIENT_SECRET: Your Azure AD application key.
  2. In your code, use DefaultAzureCredential:
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient

credential = DefaultAzureCredential()
# Use the credential object to authenticate BlobServiceClient, KeyClient, etc.

3. Making Your First Call (Blob Storage Example)

Let's upload a file to Azure Blob Storage.

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

# Replace with your storage account name
account_url = "https://.blob.core.windows.net"
blob_name = "my-blob.txt"
local_file_name = "my-local-file.txt"
data = "Hello, Azure Blob Storage!"

# Create a local file for upload
with open(local_file_name, "w") as f:
    f.write(data)

# Authenticate and create a client
credential = DefaultAzureCredential()
blob_service_client = BlobServiceClient(account_url, credential=credential)

# Upload the file
try:
    with open(local_file_name, "rb") as data:
        blob_client = blob_service_client.get_blob_client(container="my-container", blob=blob_name)
        blob_client.upload_blob(data)
    print(f"Successfully uploaded {local_file_name} to {blob_name}")
except Exception as ex:
    print(f"An error occurred: {ex}")
finally:
    # Clean up the local file
    import os
    if os.path.exists(local_file_name):
        os.remove(local_file_name)

Ensure you have a container named 'my-container' in your storage account.

API Reference

Find detailed information about the classes, methods, and parameters for each Azure SDK for Python package.

Azure Core

Common utilities, authentication, and pipeline components.

View Reference

Azure Storage Blob

Interact with Azure Blob Storage for object storage.

View Reference

Azure Cosmos DB

Manage and query NoSQL data in Azure Cosmos DB.

View Reference

Azure Key Vault Secrets

Securely manage secrets, keys, and certificates.

View Reference

Azure Identity

Authentication methods for Azure services.

View Reference

Azure Event Hubs

Ingest and process large volumes of event data.

View Reference

Code Examples

Browse practical code examples demonstrating various Azure services and scenarios.

You can find more comprehensive examples in the Azure Samples GitHub repository.