This topic describes how to initialize the client of ApsaraVideo Media Processing (MPS) SDK for Python.
Obtain an AccessKey pair from environment variables
You can define the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET to configure the default credentials. When you call an API operation, the system reads the AccessKey pair from the default credentials and uses the AccessKey pair to complete authentication. For more information, see Configure environment variables in Linux, macOS, and Windows.
Initialize the client
Initialize the alibabacloud_tea_openapi.Config object.
import os from alibabacloud_tea_openapi import models as open_api_models config = open_api_models.Config( // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured in the code runtime environment. access_key_id=os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured in the code runtime environment. access_key_secret=os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'] ) # Specify the endpoint. config.endpoint = 'mts.cn-hangzhou.aliyuncs.com'Instantiate a client object from the alibabacloud_mts20140618.Client class.
from alibabacloud_mts20140618.client import Client as Client from alibabacloud_mts20140618 import models as models client = Client(config)
Sample code:
import os
import sys
from typing import List
from alibabacloud_mts20140618.client import Client as Mts20140618Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_mts20140618 import models as mts_20140618_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient
class Sample:
def __init__(self):
pass
@staticmethod
def create_client() -> Mts20140618Client:
"""
Use your AccessKey ID and AccessKey secret to initialize the client.
@return: Client
@throws Exception
"""
config = open_api_models.Config(
# Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured. ,
access_key_id=os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
# Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured. ,
access_key_secret=os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
)
config.endpoint = f'mts.cn-hangzhou.aliyuncs.com'
return Mts20140618Client(config)
@staticmethod
def main(
args: List[str],
) -> None:
client = Sample.create_client()
activate_media_workflow_request = mts_20140618_models.ActivateMediaWorkflowRequest(
media_workflow_id='your_value'
)
try:
# If you copy and run the sample code, write your own code to display the response of the API operation.
client.activate_media_workflow_with_options(activate_media_workflow_request, util_models.RuntimeOptions())
except Exception as error:
# Handle exceptions with caution based on your actual business scenario and do not ignore exceptions in your project. The error messages displayed in this example are for reference only.
# Display error messages.
print(error.message)
# Provide the URL for troubleshooting.
print(error.data.get("Recommend"))
UtilClient.assert_as_string(error.message)
@staticmethod
async def main_async(
args: List[str],
) -> None:
client = Sample.create_client()
activate_media_workflow_request = mts_20140618_models.ActivateMediaWorkflowRequest(
media_workflow_id='your_value'
)
try:
# If you copy and run the sample code, write your own code to display the response of the API operation.
await client.activate_media_workflow_with_options_async(activate_media_workflow_request, util_models.RuntimeOptions())
except Exception as error:
# Handle exceptions with caution based on your actual business scenario and do not ignore exceptions in your project. The error messages displayed in this example are for reference only.
# Display error messages.
print(error.message)
# Provide the URL for troubleshooting.
print(error.data.get("Recommend"))
UtilClient.assert_as_string(error.message)
if __name__ == '__main__':
Sample.main(sys.argv[1:])