Azure Media Services Management

10.2.1 · active · verified Wed Apr 08

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

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with Azure using `DefaultAzureCredential` and interact with Azure Media Services to list existing accounts or create a new one. It assumes you have Azure credentials configured (e.g., via environment variables, Azure CLI, or Visual Studio Code) and a resource group ready. Note that creating a Media Services account requires a linked storage account, which must be created separately and referenced by its full resource ID.

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()

view raw JSON →