This topic describes how to initialize ApsaraVideo VOD server operation SDK for Java 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 ApsaraVideo VOD SDK for Java is installed. For more information, see Installation.
A Resource Access Management (RAM) user is created, and the required permissions are granted to the RAM user. For more information, see Create and grant permissions to a RAM user.
Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables. For more information, see Configure environment variables in Linux, macOS, and Windows.
ImportantThe AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M.
We recommend that you do not hard-code the AccessKey ID and AccessKey secret into your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account is compromised.
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:
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
// Obtain the AccessKey information.
public static DefaultAcsClient initVodClient() throws ClientException {
String regionId = "cn-shanghai"; // Specify the region in which ApsaraVideo VOD is activated.
// 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 not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources within your account may be 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.
DefaultProfile profile = DefaultProfile.getProfile(regionId, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
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. Sample code:
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
// Obtain the STS information.
public static DefaultAcsClient initVodClient() throws ClientException {
String regionId = "cn-shanghai"; // Specify the region in which ApsaraVideo VOD is activated.
DefaultProfile profile = DefaultProfile.getProfile(
regionId, // The region ID
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), // The AccessKey ID of the RAM account
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), // The AccessKey Secret of the RAM account
System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")); // STS Token
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}