All Products
Search
Document Center

ApsaraVideo VOD:Initialization

Last Updated:Aug 22, 2023

This topic describes how to initialize the server operation SDK for Python by using an AccessKey pair or a Security Token Service (STS) token. You can use either method based on your business requirements.

Background information

The server SDK of ApsaraVideo VOD can be initialized by using two different methods. You can initialize the SDK by using the AccessKey pair of an Alibaba Cloud account or a RAM user that is granted required permissions based on authorization policies. The AccessKey pair remains valid after the Alibaba Cloud account or RAM user is created. We recommend that you use this method on the server. Alternatively, you can initialize the SDK by using an STS token that is granted required permissions based on authorization policies. You can specify the validity period of the STS token.

Prerequisites

  • The server operation SDK for Python is installed. For more information, see Installation.

  • A region is specified for using ApsaraVideo VOD. For example, if you use ApsaraVideo VOD in the China (Shanghai) region, the region ID is cn-shanghai. For more information, see VOD centers and endpoints.

Initialize the SDK by using an AccessKey pair

Obtain an AccessKey pair to complete identity verification so that you can call server API operations. For more information about how to obtain an AccessKey pair, see Obtain an AccessKey pair.

Use the AccessKey pair to initialize the SDK. Sample code:

# -*- coding: UTF-8 -*-
import json
import traceback
from aliyunsdkcore.client import AcsClient

# Obtain the AccessKey information.
def init_vod_client():
    regionId = 'cn-shanghai'   # The region in which ApsaraVideo VOD is activated.
    connectTimeout = 3         # The timeout period. Unit: seconds.
    // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. 
    // We recommend that you do not save your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked. As a result, the security of all resources within your account is compromised. 
    // In this example, ApsaraVideo VOD reads the AccessKey pair from the environment variables to implement identity verification for API access. Before you run the sample code, configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. 
    credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
    return AcsClient(region_id=regionId, credential=credentials)

Initialize the SDK by using an STS token

An STS token is obtained for initializing the SDK. For more information about how to obtain an STS token, see the "Use STS to authorize access" section of the Create a role and grant temporary access permissions to the role by using STS topic.

Use an STS token to initialize the SDK. Sample code:

# -*- coding: UTF-8 -*-
import json
import traceback
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.auth.credentials import StsTokenCredential

# Obtain the STS information.
def init_vod_client(accessKeyId, accessKeySecret, securityToken):
    regionId = 'cn-shanghai'   # Specify the region in which ApsaraVideo VOD is activated.
    connectTimeout = 3         # The timeout period. Unit: seconds.
    credential = StsTokenCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'], os.environ['ALIBABA_CLOUD_SECURITY_TOKEN'])
    return AcsClient(region_id=regionId, auto_retry=True, max_retry_time=3, timeout=connectTimeout, credential=credential)