This topic describes how to initialize the server operation SDK for Java by using an AccessKey pair or a Security Token Service (STS) token. You can use one of the methods as needed.

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 Java 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. The following sample code provides an example:
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;

// Specify the AccessKey pair.
public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
    String regionId = "cn-shanghai";  // Specify the region from which you want to access ApsaraVideo VOD.
    DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
    DefaultAcsClient client = new DefaultAcsClient(profile);
    return client;
}

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. The following sample code provides an example:
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;

// Specify the STS token.
public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret, String securityToken) throws ClientException {
    String regionId = "cn-shanghai";  // Specify the region from which you want to access ApsaraVideo VOD.
    DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret, securityToken);
    DefaultAcsClient client = new DefaultAcsClient(profile);
    return client;
}