Azure Media Services Management
The azure-mgmt-media library is the Microsoft Azure Media Services Client Library for Python. It provides classes and methods for programmatically managing Azure Media Services resources, such as Media Services accounts, assets, transforms, and jobs. The current version is 10.2.1. Like other Azure SDKs, it follows a regular release cadence with updates aligning with Azure service API versions and general SDK improvements.
Warnings
- gotcha Azure Management SDKs, including `azure-mgmt-media`, require proper authentication and authorization. Common issues include insufficient permissions for the authenticated identity (e.g., Service Principal, Managed Identity) or incorrect Azure AD application configuration. Ensure your identity has 'Contributor' or 'Media Services Contributor' role on the relevant resource group or subscription.
- gotcha When creating an Azure Media Services account, it *must* be linked to an existing Azure Storage account. If the specified storage account does not exist, is in a different region, or is not accessible by the identity creating the Media Services account, the creation will fail. Ensure the storage account name is globally unique and the full resource ID is correct.
- breaking The Azure SDKs for Python undergo significant changes between major versions, often aligning with new Azure REST API versions. While specific breaking changes from v9 to v10 for `azure-mgmt-media` might vary, general patterns include changes in class names, method signatures (e.g., parameters), and the structure of returned objects. Always consult the official migration guide for the specific service.
Install
-
pip install azure-mgmt-media
Imports
- MediaServicesClient
from azure.mgmt.media import MediaServicesClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.media import MediaServicesClient
# Set your Azure subscription ID and resource group name in environment variables
# AZURE_SUBSCRIPTION_ID, AZURE_RESOURCE_GROUP
# Also ensure AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID are set for DefaultAzureCredential
subscription_id = os.environ.get('AZURE_SUBSCRIPTION_ID', 'YOUR_SUBSCRIPTION_ID')
resource_group_name = os.environ.get('AZURE_RESOURCE_GROUP', 'YOUR_RESOURCE_GROUP_NAME')
media_account_name = os.environ.get('AZURE_MEDIA_ACCOUNT_NAME', 'mytestmediaaccount')
location = os.environ.get('AZURE_LOCATION', 'westus2') # Or your preferred Azure region
def main():
# Authenticate to Azure using DefaultAzureCredential
# This credential will attempt to authenticate via various mechanisms:
# environment variables, managed identity, VS Code, Azure CLI, etc.
credential = DefaultAzureCredential()
# Create a Media Services client
media_client = MediaServicesClient(credential, subscription_id)
print(f"Created MediaServicesClient for subscription: {subscription_id}")
# Check if Media Services account exists
try:
account = media_client.mediaservices.get(resource_group_name, media_account_name)
print(f"Media Services account '{media_account_name}' already exists.")
except Exception as e:
print(f"Media Services account '{media_account_name}' does not exist or error: {e}. Creating...")
# Create a new Media Services account (requires a storage account to be linked)
# For a real scenario, you would first create a storage account and link it.
# This example assumes a default storage account is handled or will fail if not configured.
# In a real application, you'd define primary_storage_account: StorageAccount
# and related properties.
media_account_properties = {
"location": location,
"storage_accounts": [
{
"id": f"/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Storage/storageAccounts/youruniquestorageaccountname",
"type": "Primary"
}
]
}
# NOTE: 'youruniquestorageaccountname' must be replaced with an actual, globally unique storage account name
# that exists in the same resource group and subscription.
print(f"Creating Media Services account '{media_account_name}' in '{resource_group_name}'...")
media_client.mediaservices.begin_create_or_update(
resource_group_name,
media_account_name,
media_account_properties
).result()
print(f"Media Services account '{media_account_name}' created successfully.")
# List all Media Services accounts in the resource group
print(f"Listing Media Services accounts in resource group '{resource_group_name}':")
for media_account in media_client.mediaservices.list_by_resource_group(resource_group_name):
print(f"- {media_account.name} (Location: {media_account.location})")
if __name__ == '__main__':
# Replace 'youruniquestorageaccountname' with an actual, globally unique storage account name
# that exists in your subscription and resource group.
# This quickstart is illustrative and requires a pre-existing storage account.
# For a fully runnable example, ensure the storage account is created first.
# os.environ['AZURE_STORAGE_ACCOUNT_ID'] = "..."
main()