All Products
Search
Document Center

Data Online Migration:Initialize a client

Last Updated:Nov 01, 2024

This topic describes how to initialize a client for using Data Online Migration SDK for Java. Before you use Data Online Migration SDK for Go to initiate a request, you must initialize a client with access credentials. Alibaba Cloud services use the access credentials to verify your identity information and access permissions.

Prerequisites

Data Online Migration SDK for Java is installed. For more information, see Install the SDK.

Access credentials

Warning

An Alibaba Cloud account has full permissions on resources within the account. AccessKey pair leaks of an Alibaba Cloud account pose critical threats to the system. Therefore, we recommend that you use the AccessKey pair of a Resource Access Management (RAM) user that is granted permissions based on the principle of least privilege.

The following types of access credentials are supported:

  • Temporary access credentials: For scenarios that require high security, we recommend that you use temporary access credentials. Temporary access credentials are valid only within a specific period of time, which helps prevent credential leaks. Temporary access credentials support fine-grained access control. This helps effectively prevent security risks caused by excessive permissions.

  • Long-term access credentials: To ensure data security, we recommend that you do not use long-term access credentials. For scenarios that require convenience, long-term access credentials eliminate the need for multiple refreshes within a long period of time. We recommend that you change your long-term access credentials every three months to ensure the security of your Alibaba Cloud account. If long-term access credentials are leaked or no longer used, you must delete or disable the long-term access credentials at the earliest opportunity to reduce security risks.

Use temporary access credentials for initialization

  1. Use temporary access credentials to specify environment variables.

Mac OS X/Linux/Unix

export OSS_ACCESS_KEY_ID=<ALIBABA_CLOUD_ACCESS_KEY_ID>
export OSS_ACCESS_KEY_SECRET=<ALIBABA_CLOUD_ACCESS_KEY_SECRET>
export OSS_SESSION_TOKEN=<ALIBABA_CLOUD_SECURITY_TOKEN>

Windows

set OSS_ACCESS_KEY_ID=<ALIBABA_CLOUD_ACCESS_KEY_ID>
set OSS_ACCESS_KEY_SECRET=<ALIBABA_CLOUD_ACCESS_KEY_SECRET>
set OSS_SESSION_TOKEN=<ALIBABA_CLOUD_SECURITY_TOKEN>
  1. Initialize a client.

package sample;

import com.aliyun.hcs_mgw20240626.Client;

public class StsTest {
	./** We recommend that you do not hard-code the AccessKey ID or AccessKey secret into your project code. Otherwise, the AccessKey pair may be leaked and the security of all the resources within your account is compromised. */
	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("OSS_SESSION_TOKEN");

	public static void main(String[] args) throws Exception {
		com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
		// In this example, the endpoint in the China (Beijing) region is used. 
		config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
		config.setRegionId(accessKeyId);
		config.setAccessKeyId(accessKeyId);
		config.setAccessKeySecret(accessKeySecret);
		config.setSecurityToken(securityToken);
		Client client = new com.aliyun.hcs_mgw20240626.Client(config);
		// Use the initialized client for subsequent operations.
	}
}

Use long-term access credentials for initialization

  1. Use long-term access credentials to specify environment variables.

Mac OS X/Linux/Unix

export OSS_ACCESS_KEY_ID=<ALIBABA_CLOUD_ACCESS_KEY_ID>
export OSS_ACCESS_KEY_SECRET=<ALIBABA_CLOUD_ACCESS_KEY_SECRET>

Windows

set OSS_ACCESS_KEY_ID=<ALIBABA_CLOUD_ACCESS_KEY_ID>
set OSS_ACCESS_KEY_SECRET=<ALIBABA_CLOUD_ACCESS_KEY_SECRET>
  1. Initialize a client.

package sample;

import com.aliyun.hcs_mgw20240626.Client;

public class SampleTest {
	./** We recommend that you do not hard-code the AccessKey ID or AccessKey secret into your project code. Otherwise, the AccessKey pair may be leaked and the security of all the resources within your account is compromised. */
	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();
		// In this example, the endpoint in the China (Beijing) region is used. 
		config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
		config.setRegionId(accessKeyId);
		config.setAccessKeyId(accessKeyId);
		config.setAccessKeySecret(accessKeySecret);
		Client client = new com.aliyun.hcs_mgw20240626.Client(config);
		// Use the initialized client for subsequent operations.
	}
}

What to do next

After a client is initialized, you can use the SDK to manage tunnels, agents, data addresses, and tasks. For more information, see Manage tunnels, Manage agents, Manage data addresses, and Manage tasks. Tunnels and agents apply only to scenarios in which you migrate data over an Express Connect circuit or a VPN gateway.