Initialize a Data Online Migration SDK for Java client with access credentials before you call API operations. Alibaba Cloud services use the access credentials to verify your identity and access permissions.
Prerequisites
Data Online Migration SDK for Java is installed. For more information, see Install the SDK.
Access credentials
An Alibaba Cloud account has full permissions on all resources within the account. An AccessKey pair leak poses critical threats to the system. Use the AccessKey pair of a RAM user that is granted the minimum required permissions.
The following types of access credentials are available:
-
Temporary access credentials (recommended): Best for high-security scenarios. Temporary access credentials are valid only within a specific period of time, which helps prevent credential leaks. They also support fine-grained access control to minimize the risk of excessive permissions.
-
Long-term access credentials: Suitable for convenience-oriented scenarios that do not require frequent credential refreshes. For security, rotate long-term access credentials every three months. If long-term access credentials are leaked or no longer used, delete or disable them immediately to reduce security risks.
Use temporary access credentials to initialize a client
-
Configure environment variables for temporary access credentials.
Mac OS X/Linux/Unix
export ALIBABA_CLOUD_ACCESS_KEY_ID=<ALIBABA_CLOUD_ACCESS_KEY_ID>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<ALIBABA_CLOUD_ACCESS_KEY_SECRET>
export ALIBABA_CLOUD_SECURITY_TOKEN=<ALIBABA_CLOUD_SECURITY_TOKEN>
Windows
set ALIBABA_CLOUD_ACCESS_KEY_ID=<ALIBABA_CLOUD_ACCESS_KEY_ID>
set ALIBABA_CLOUD_ACCESS_KEY_SECRET=<ALIBABA_CLOUD_ACCESS_KEY_SECRET>
set ALIBABA_CLOUD_SECURITY_TOKEN=<ALIBABA_CLOUD_SECURITY_TOKEN>
-
Initialize the client.
package sample;
import com.aliyun.hcs_mgw20240626.Client;
public class StsTest {
/** Do not hard-code the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, compromising the security of all resources within your account. */
static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
static String securityToken = System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN");
public static void main(String[] args) throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
// The following example uses the China (Beijing) region.
config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
config.setAccessKeyId(accessKeyId);
config.setAccessKeySecret(accessKeySecret);
config.setSecurityToken(securityToken);
// Both HTTPS and HTTP are supported. HTTPS is used by default.
config.setProtocol("http");
Client client = new com.aliyun.hcs_mgw20240626.Client(config);
// Perform subsequent operations by using the initialized client.
}
}
Use long-term access credentials to initialize a client
-
Configure environment variables for long-term access credentials.
Mac OS X/Linux/Unix
export ALIBABA_CLOUD_ACCESS_KEY_ID=<ALIBABA_CLOUD_ACCESS_KEY_ID>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<ALIBABA_CLOUD_ACCESS_KEY_SECRET>
Windows
setx ALIBABA_CLOUD_ACCESS_KEY_ID "ALIBABA_CLOUD_ACCESS_KEY_ID"
setx ALIBABA_CLOUD_ACCESS_KEY_SECRET "ALIBABA_CLOUD_ACCESS_KEY_SECRET"
-
Initialize the client.
package sample;
import com.aliyun.hcs_mgw20240626.Client;
public class SampleTest {
/** Do not hard-code the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, compromising the security of all resources within your account. */
static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
public static void main(String[] args) throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
// The following example uses the China (Beijing) region.
config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
config.setAccessKeyId(accessKeyId);
config.setAccessKeySecret(accessKeySecret);
// Both HTTPS and HTTP are supported. HTTPS is used by default.
config.setProtocol("http");
Client client = new com.aliyun.hcs_mgw20240626.Client(config);
// Perform subsequent operations by using the initialized client.
}
}
What to do next
After the client is initialized, perform operations on channels, proxies, data addresses, and tasks. Channels and proxies are used only in leased line or VPN scenarios.