通道
更新时间:
本文介绍如何使用SDK调用通道相关的方法。
创建通道
说明
仅当使用专线或者VPN的情况下才需要创建通道,公网场景一般无须创建通道。
以下示例代码用于创建通道。
package main
import (
"fmt"
openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
"os"
)
/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** 填写主账号ID。*/
var userId = "11470***876***55";
func main() {
// 这里以北京区域为例。
endpoint := "cn-beijing.mgw.aliyuncs.com"
config := openapipackage.Config{
AccessKeyId: &accessKeyId,
AccessKeySecret: &accessKeySecret,
Endpoint: &endpoint,
}
client, err := mgwpackage.NewClient(&config)
if err != nil{
fmt.Errorf("create client failed, %v", err)
return
}
// MaxBandwidth, MaxQps 默认为0, 表示没有限制, MaxBandWidth的单位是bit,请按照实际需求填写。
maxBandwidth := int64(1073741824)
maxQps := int32(1000)
request := &mgwpackage.CreateTunnelRequest{
ImportTunnel: &mgwpackage.CreateTunnelInfo{
TunnelQos : &mgwpackage.TunnelQos{
MaxBandwidth : &maxBandwidth,
MaxQps : &maxQps,
},
},
}
resp, err := client.CreateTunnel(&userId, request)
if err != nil {
fmt.Errorf("create tunnel failed, %v", err)
return
}
fmt.Println("通道ID:" + *resp.Headers["x-oss-import-tunnel-id"])
}
获取通道详情
以下示例代码用于获取指定通道详情。
package main
import (
"encoding/json"
"fmt"
openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
"os"
)
/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** 填写主账号ID。*/
var userId = "11470***876***55";
func main() {
// 填写通道ID
tunnelId := "ab31d1f9-****-4f62-****-914e4b2f78c7"
// 这里以北京区域为例。
endpoint := "cn-beijing.mgw.aliyuncs.com"
config := openapipackage.Config{
AccessKeyId: &accessKeyId,
AccessKeySecret: &accessKeySecret,
Endpoint: &endpoint,
}
client, err := mgwpackage.NewClient(&config)
if err != nil{
fmt.Errorf("create client failed, %v", err)
return
}
resp, err := client.GetTunnel(&userId, &tunnelId)
if err != nil {
fmt.Errorf("get tunnel failed, %v", err)
return
}
jsonBytes, err := json.Marshal(resp)
if err != nil {
fmt.Println("covert failed:", err)
return
}
fmt.Println(string(jsonBytes))
}
正常返回示例
{
"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 main
import (
"encoding/json"
"fmt"
openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
"os"
)
/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** 填写主账号ID。*/
var userId = "11470***876***55";
func main() {
// 这里以北京区域为例。
endpoint := "cn-beijing.mgw.aliyuncs.com"
config := openapipackage.Config{
AccessKeyId: &accessKeyId,
AccessKeySecret: &accessKeySecret,
Endpoint: &endpoint,
}
client, err := mgwpackage.NewClient(&config)
if err != nil{
fmt.Errorf("create client failed, %v", err)
return
}
// 根据实际填写marker,count。
count := int32(1)
marker := ""
resp, err := client.ListTunnel(&userId, &mgwpackage.ListTunnelRequest{Count: &count, Marker: &marker})
if err != nil {
fmt.Errorf("list tunnel failed, %v", err)
return
}
jsonBytes, err := json.Marshal(resp)
if err != nil {
fmt.Println("covert failed:", err)
return
}
fmt.Println(string(jsonBytes))
}
正常返回示例
{
"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 main
import (
"fmt"
openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
"os"
)
/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** 填写主账号ID。*/
var userId = "11470***876***55";
func main() {
// 填写通道ID。
tunnelId := "ab31d1f9-****-4f62-****-914e4b2f78c7"
// 这里以北京区域为例。
endpoint := "cn-beijing.mgw.aliyuncs.com"
config := openapipackage.Config{
AccessKeyId: &accessKeyId,
AccessKeySecret: &accessKeySecret,
Endpoint: &endpoint,
}
client, err := mgwpackage.NewClient(&config)
if err != nil{
fmt.Errorf("create client failed, %v", err)
return
}
// MaxBandwidth, MaxQps 默认为0, 表示没有限制, MaxBandWidth的单位是bit,请按照实际需求填写
maxBandwidth := int64(1147483648)
maxQps := int32(1000)
_, err = client.UpdateTunnel(&userId, &tunnelId,
&mgwpackage.UpdateTunnelRequest{
ImportTunnel: &mgwpackage.UpdateTunnelInfo{
TunnelQos: &mgwpackage.TunnelQos{
MaxBandwidth: &maxBandwidth,
MaxQps: &maxQps,
},
}})
if err != nil{
fmt.Errorf("update tunnel failed, %v", err)
return
}
}
删除通道
以下示例代码用于删除指定通道。
package main
import (
"fmt"
openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
"os"
)
/** 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。*/
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** 填写主账号ID。*/
var userId = "11470***876***55";
func main() {
// 填写通道ID。
tunnelId := "ab31d1f9-****-4f62-****-914e4b2f78c7"
// 这里以北京区域为例。
endpoint := "cn-beijing.mgw.aliyuncs.com"
config := openapipackage.Config{
AccessKeyId: &accessKeyId,
AccessKeySecret: &accessKeySecret,
Endpoint: &endpoint,
}
client, err := mgwpackage.NewClient(&config)
if err != nil{
fmt.Errorf("create client failed, %v", err)
return
}
_, err = client.DeleteTunnel(&userId, &tunnelId)
if err != nil{
fmt.Errorf("delete tunnel failed, %v", err)
return
}
}
后续步骤
通道创建后,您可以选择继续创建代理,详情请参见代理 。
文档内容是否对您有帮助?