Connect to Media Services v3 API - Python

Media Services logo v3


Warning

Azure Media Services will be retired June 30th, 2024. For more information, see the AMS Retirement Guide.

This article shows you how to connect to the Azure Media Services v3 Python SDK using the service principal sign in method.

Prerequisites

  • Download Python from python.org
  • Make sure to set the PATH environment variable
  • Create a Media Services account. Be sure to remember the resource group name and the Media Services account name.
  • Follow the steps in the Access APIs topic, selecting the Service principal authentication method. Record the subscription ID, application client ID, the authentication key, and the tenant ID that you need in the later steps.

Important

Review naming conventions.

Install the modules

To work with Azure Media Services using Python, you need to install these modules.

Open a command-line tool and use the following commands to install the modules.

pip3 install azure-identity
pip3 install azure-mgmt-media

Connect to the Python client

  1. Create a file with a .py extension

  2. Open the file in your favorite editor

  3. Add the the following code to the file. The code imports the required modules and creates the Active Directory credentials object you need to connect to Media Services.

    Set the variables' values to the values you got from Access APIs. Update the ACCOUNT_NAME and RESOURCE_GROUP_NAME variables to the Media Services account name and Resource Group names used when creating those resources.

    from azure.identity import ClientSecretCredential 
    from azure.mgmt.media import AzureMediaServices
    
    # Tenant ID for your Azure Subscription
    TENANT_ID = "(update-this-value)"
    
    # Your Application Client ID of your Service Principal
    CLIENT_ID = "(update-this-value)"
    
    # Your Service Principal secret key
    CLIENT_SECRET = "(update-this-value)"
    
    # Your Azure Subscription ID
    SUBSCRIPTION_ID = "(update-this-value)"
    
    # Your Resource Group name
    RESOURCE_GROUP_NAME = "(update-this-value)"
    
    # Your Azure Media Service account name
    ACCOUNT_NAME = "(update-this-value)"
    
    credentials = ClientSecretCredential(TENANT_ID, CLIENT_ID, CLIENT_SECRET, authority=AzureAuthorityHosts.AZURE_CHINA)
    
    # The Azure Media Services Client
      client = AzureMediaServices(credentials, SUBSCRIPTION_ID, base_url="https://management.chinacloudapi.cn")
    
    # Now that you are authenticated, you can manipulate the entities.
    # For example, list assets in your Media Services account
    assets = client.assets.list(RESOURCE_GROUP_NAME, ACCOUNT_NAME)
    
    for i, r in enumerate(assets):
        print(r)
    
  4. Run the file

Additional samples

Additional samples are available in GitHub in the Azure Media Services v3 Python Samples repo.