本文介紹如何使用SDK調用通道相關的方法。
建立通道
說明
僅當使用專線或者VPN的情況下才需要建立通道,公網情境一般無須建立通道。
以下範例程式碼用於建立通道。
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 {
/** 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。*/
static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
/** 填寫主帳號ID。*/
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();
// 這裡以北京地區為例
config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
config.setAccessKeyId(accessKeyId);
config.setAccessKeySecret(accessKeySecret);
// 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
config.setProtocol("http");
Client client = new Client(config);
CreateTunnelRequest request = new CreateTunnelRequest();
CreateTunnelInfo info = new CreateTunnelInfo();
TunnelQos qos = new TunnelQos();
// MaxBandwidth, MaxQps 預設為0, 表示沒有限制, MaxBandWidth的單位是bit,請按照實際需求填寫。
qos.setMaxBandwidth(1073741824L);
qos.setMaxQps(1000);
info.setTunnelQos(qos);
request.setImportTunnel(info);
CreateTunnelResponse resp = client.createTunnel(userId, request);
System.out.println("通道ID:" + resp.headers.get("x-oss-import-tunnel-id"));
} catch (Exception e) {
e.printStackTrace();
}
}
}擷取通道詳情
以下範例程式碼用於擷取指定通道詳情。
public class Demo {
/** 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。*/
static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
/** 填寫主帳號ID。*/
static String userId = "11470***876***55";
public static void main(String[] args) {
// 填寫通道ID。
String tunnelId = "ab31d1f9-****-4f62-****-914e4b2f78c7";
try {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
// 這裡以北京地區為例。
config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
config.setAccessKeyId(accessKeyId);
config.setAccessKeySecret(accessKeySecret);
// 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
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();
}
}
}正常返回樣本
{
"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
}
}
}列舉通道
以下範例程式碼用於列舉當前帳號下的所有通道資訊。
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 {
/** 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。*/
static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
/** 填寫主帳號ID。*/
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();
// 這裡以北京地區為例。
config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
config.setAccessKeyId(accessKeyId);
config.setAccessKeySecret(accessKeySecret);
// 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
config.setProtocol("http");
Client client = new Client(config);
ListTunnelRequest request = new ListTunnelRequest();
// 根據實際填寫marker,count。
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();
}
}
}
正常返回樣本
{
"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
}
}
]
}
}更新通道
以下範例程式碼用於更新通道Qos等資訊。
package sample;
import com.aliyun.hcs_mgw20240626.Client;
import com.aliyun.hcs_mgw20240626.models.*;
public class Demo {
/** 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。*/
static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
/** 填寫主帳號ID。*/
static String userId = "11470***876***55";
public static void main(String[] args) {
// 填寫通道ID。
String tunnelId = "ab31d1f9-****-4f62-****-914e4b2f78c7";
try {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
// 這裡以北京地區為例。
config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
config.setAccessKeyId(accessKeyId);
config.setAccessKeySecret(accessKeySecret);
// 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
config.setProtocol("http");
Client client = new Client(config);
UpdateTunnelInfo info = new UpdateTunnelInfo();
TunnelQos qos = new TunnelQos();
// MaxBandwidth, MaxQps 預設為0, 表示沒有限制, MaxBandWidth的單位是bit,請按照實際需求填寫。
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();
}
}
}刪除通道
以下範例程式碼用於刪除指定通道。
package sample;
import com.aliyun.hcs_mgw20240626.Client;
public class Demo {
/** 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。*/
static String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
static String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
/** 填寫主帳號ID。*/
static String userId = "11470***876***55";
public static void main(String[] args) {
// 填寫通道ID。
String tunnelId = "ab31d1f9-****-4f62-****-914e4b2f78c7";
try {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
// 這裡以北京地區為例。
config.setEndpoint("cn-beijing.mgw.aliyuncs.com");
config.setAccessKeyId(accessKeyId);
config.setAccessKeySecret(accessKeySecret);
// 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
config.setProtocol("http");
Client client = new Client(config);
client.deleteTunnel(userId, tunnelId);
} catch (Exception e) {
e.printStackTrace();
}
}
}後續步驟
通道建立完成後,您可以選擇繼續進行代理的建立操作,具體資訊請參見代理。