All Products
Search
Document Center

Data Online Migration:Manage tunnels

Last Updated:Dec 11, 2025

This topic describes how to use Data Online Migration SDK for Java to manage tunnels.

Create a tunnel

Note

You need to create a tunnel only in scenarios in which an Express Connect circuit or VPN is used. You do not need to create a tunnel in Internet access scenarios.

The following sample code shows how to create a tunnel:

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.CreateTunnelInfo;
import com.aliyun.hcs_mgw20240626.models.CreateTunnelRequest;
import com.aliyun.hcs_mgw20240626.models.TunnelQos;

public class Demo {
	/** Do not store the AccessKey pair in your code. A leak can expose all resources in your Alibaba Cloud account to security risks. */
	static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
	static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
	/** Enter the ID of your Alibaba Cloud account. */
	static String userId = "11470***876***55";

	public static void main(String[] args) {
		try {
			com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
			// This 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. If you do not specify a protocol, HTTPS is used by default.
		        config.setProtocol("http");
			Client client = new Client(config);
			CreateTunnelRequest request = new CreateTunnelRequest();
			CreateTunnelInfo info = new CreateTunnelInfo();
			TunnelQos qos = new TunnelQos();
			// The default value for MaxBandwidth and MaxQps is 0, which means no limit. The unit for MaxBandwidth is bits. Set these parameters as needed.
			qos.setMaxBandwidth(1073741824L);
			qos.setMaxQps(1000);
			info.setTunnelQos(qos);
			request.setImportTunnel(info);
			CreateTunnelResponse resp = client.createTunnel(userId, request);
			System.out.println("Tunnel ID: " + resp.headers.get("x-oss-import-tunnel-id"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Query the details of a tunnel

The following sample code shows how to query the details of a tunnel:

public class Demo {
   /** Do not store the AccessKey pair in your code. A leak can expose all resources in your Alibaba Cloud account to security risks. */
   static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
   static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
   /** Enter the ID of your Alibaba Cloud account. */
   static String userId = "11470***876***55";

   public static void main(String[] args) {
      // Enter the tunnel ID.
      String tunnelId = "ab31d1f9-****-4f62-****-914e4b2f78c7";
      try {
         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
         // This 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. If you do not specify a protocol, HTTPS is used by default.
         config.setProtocol("http");
         Client client = new Client(config);
         GetTunnelResponse resp = client.getTunnel(userId, tunnelId);
         System.out.println(new Gson().toJson(resp.getBody().getImportTunnel()));
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Sample success response

{
  "ImportTunnel": {
    "Owner": "test_owner",
    "TunnelId": "test_tunnel_id",
    "CreateTime": "2024-05-01T12:00:00.000Z",
    "ModifyTime": "2024-05-01T12:00:00.000Z",
    "Tags": "K1:V1,K2:V2",
    "TunnelQos": {
      "MaxQps": 100,
      "MaxBandwidth": 1073741824
    }
  }
}

List channels

The following sample code shows how to query the information about all tunnels within an Alibaba Cloud account:

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.GetTunnelResponse;
import com.aliyun.hcs_mgw20240626.models.ListTunnelRequest;
import com.aliyun.hcs_mgw20240626.models.ListTunnelResponse;
import com.google.gson.Gson;

public class Demo {
	/** Do not store the AccessKey pair in your code. A leak can expose all resources in your Alibaba Cloud account to security risks. */
	static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
	static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
	/** Enter the ID of your Alibaba Cloud account. */
	static String userId = "11470***876***55";

	public static void main(String[] args) {
		try {
			com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
			// This 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. If you do not specify a protocol, HTTPS is used by default.
                        config.setProtocol("http");
			Client client = new Client(config);
			ListTunnelRequest request = new ListTunnelRequest();
			// Set the marker and count parameters as needed.
			String marker = "";
			int count = 1;
			request.setMarker(marker);
			request.setCount(count);
			ListTunnelResponse resp = client.listTunnel(userId, request);
			System.out.println(new Gson().toJson(resp.getBody().getImportTunnelList()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Sample success response

{
  "ImportTunnelList": {
    "Truncated": true,
    "NextMarker": "test_marker",
    "ImportTunnel": [
      {
        "Owner": "test_owner",
        "TunnelId": "test_tunnel_id",
        "CreateTime": "2024-05-01T12:00:00.000Z",
        "ModifyTime": "2024-05-01T12:00:00.000Z",
        "Tags": "K1:V1,K2:V2",
        "TunnelQos": {
          "MaxQps": 100,
          "MaxBandwidth": 1073741824
        }
      }
    ]
  }
}

Update a tunnel

The following sample code shows how to update a tunnel, such as updating quality of service (QoS) of the tunnel:

package sample;

import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.*;

public class Demo {
	/** Do not store the AccessKey pair in your code. A leak can expose all resources in your Alibaba Cloud account to security risks. */
	static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
	static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
	/** Enter the ID of your Alibaba Cloud account. */
	static String userId = "11470***876***55";

	public static void main(String[] args) {
		// Enter the tunnel ID.
		String tunnelId = "ab31d1f9-****-4f62-****-914e4b2f78c7";
		try {
			com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
			// This 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. If you do not specify a protocol, HTTPS is used by default.
                        config.setProtocol("http");
			Client client = new Client(config);
			UpdateTunnelInfo info = new UpdateTunnelInfo();
			TunnelQos qos = new TunnelQos();
			// The default value for MaxBandwidth and MaxQps is 0, which means no limit. The unit for MaxBandwidth is bits. Set these parameters as needed.
			qos.setMaxBandwidth(1273741824L);
			qos.setMaxQps(1000);
			info.setTunnelQos(qos);
			UpdateTunnelRequest request = new UpdateTunnelRequest();
			request.setImportTunnel(info);
			client.updateTunnel(userId, tunnelId, request);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Delete a tunnel

The following sample code shows how to delete a tunnel:

package sample;

import com.aliyun.hcs_mgw20240626.Client;

public class Demo {
	/** Do not store the AccessKey pair in your code. A leak can expose all resources in your Alibaba Cloud account to security risks. */
	static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
	static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
	/** Enter the ID of your Alibaba Cloud account. */
	static String userId = "11470***876***55";

	public static void main(String[] args) {
		// Enter the tunnel ID.
		String tunnelId = "ab31d1f9-****-4f62-****-914e4b2f78c7";
		try {
			com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
			// This 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. If you do not specify a protocol, HTTPS is used by default.
                        config.setProtocol("http");
			Client client = new Client(config);
			client.deleteTunnel(userId, tunnelId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

What to do next

After you create a tunnel, you can continue to create an agent. For more information, see Manage agents.