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):
- 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.
- 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.
How-to Guides
Explore detailed guides for common tasks and specific Azure services.
- Managing Azure Blob Storage Containers and Blobs
- Working with Azure Cosmos DB Documents
- Securely Storing and Retrieving Secrets with Azure Key Vault
- Sending and Receiving Messages with Azure Service Bus
- Processing Events with Azure Event Hubs
- Configuring Applications with Azure App Configuration
- Managing Azure Resource Manager Resources
API Reference
Find detailed information about the classes, methods, and parameters for each Azure SDK for Python package.
Code Examples
Browse practical code examples demonstrating various Azure services and scenarios.
- Blob Storage: Upload, Download, and List Blobs
- Cosmos DB: CRUD Operations on Documents
- Key Vault: Retrieve a Secret
- Event Hubs: Send and Receive Events
- Service Bus: Send and Receive Queued Messages
You can find more comprehensive examples in the Azure Samples GitHub repository.