通道

更新时间:

本文介绍如何使用SDK调用通道相关的方法。

创建通道

说明

仅在使用专线或VPN的情况下,才需要创建通道;在公网环境下,通常无需创建通道。

以下示例代码用于创建通道。

import os

from alibabacloud_hcs_mgw20240626.client import Client
from alibabacloud_hcs_mgw20240626.models import CreateTunnelRequest, TunnelQos, CreateTunnelInfo
from alibabacloud_tea_openapi.models import Config

if __name__ == "__main__":
    try:
        # 填写主账号ID。
        userid = "11470***876***55"
        config = Config(
            endpoint="cn-beijing.mgw.aliyuncs.com",
            access_key_id=os.environ.get("OSS_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("OSS_ACCESS_KEY_SECRET"),
            security_token=os.environ.get("OSS_SESSION_TOKEN")
        )
        client = Client(config)
        resp = client.create_tunnel(userid, CreateTunnelRequest(
            CreateTunnelInfo(tunnel_qos=TunnelQos(max_bandwidth=2147483648, max_qps=2000))))
        tunnel_id = resp.headers["x-oss-import-tunnel-id"]
        print(tunnel_id)
    except Exception as e:
        print(e)
 

获取通道详情

以下示例代码用于获取特定通道的详细信息。

import os

from alibabacloud_hcs_mgw20240626.client import Client
from alibabacloud_tea_openapi.models import Config

if __name__ == "__main__":
    try:
        # 填写主账号ID。
        userid = "11470***876***55"
        # 这里以北京区域为例。
        endpoint = "cn-beijing.mgw.aliyuncs.com"
        # 填写通道ID。
        tunnel_id = "ab31d1f9-****-4f62-****-914e4b2f78c7"

        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("OSS_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("OSS_ACCESS_KEY_SECRET"),
        )
        client = Client(config)
        response = client.get_tunnel(userid, tunnel_id)
        print(response.body.import_tunnel)
    except Exception as e:
        print(e)
        

正常返回示例

{
  "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
    }
  }
}

列举通道

以下示例代码用于列举账号下的所有通道信息。

import os

from alibabacloud_hcs_mgw20240626.client import Client
from alibabacloud_hcs_mgw20240626.models import ListTunnelRequest
from alibabacloud_tea_openapi.models import Config

if __name__ == "__main__":
    try:
        # 填写主账号ID。
        userid = "11470***876***55"
        # 这里以北京区域为例。
        endpoint = "cn-beijing.mgw.aliyuncs.com"

        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("OSS_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("OSS_ACCESS_KEY_SECRET"),
        )
        client = Client(config)

        # 根据实际填写marker, count。
        count = 1
        marker = ""
        response = client.list_tunnel(userid, ListTunnelRequest(
            marker=marker,
            count=count,
        ))
        for import_tunnel in response.body.import_tunnel_list.import_tunnel:
            print(import_tunnel)
    except Exception as e:
        print(e)

更新通道

以下示例代码用于更新通道等信息。

import os

from alibabacloud_hcs_mgw20240626.client import Client
from alibabacloud_hcs_mgw20240626.models import UpdateTunnelRequest, UpdateTunnelInfo, TunnelQos
from alibabacloud_tea_openapi.models import Config

if __name__ == "__main__":
    try:
        # 填写主账号ID。
        userid = "11470***876***55"
        # 这里以北京区域为例。
        endpoint = "cn-beijing.mgw.aliyuncs.com"
        # 填写通道ID。
        tunnel_id = "ab31d1f9-****-4f62-****-914e4b2f78c7"

        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("OSS_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("OSS_ACCESS_KEY_SECRET"),
        )
        client = Client(config)
        
        # MaxBandwidth, MaxQps 默认为0, 表示没有限制, MaxBandWidth的单位是bit,请按照实际需求填写。
        max_bandwidth = 1147483648
        max_qps = 1000
        client.update_tunnel(userid, tunnel_id, UpdateTunnelRequest(UpdateTunnelInfo(
            tunnel_qos=TunnelQos(
                max_bandwidth=max_bandwidth,
                max_qps=max_qps
            ),
        )))
    except Exception as e:
        print(e)

删除通道

以下示例代码用于删除指定通道。

import os

from alibabacloud_hcs_mgw20240626.client import Client
from alibabacloud_hcs_mgw20240626.models import UpdateTunnelRequest, UpdateTunnelInfo, TunnelQos
from alibabacloud_tea_openapi.models import Config

if __name__ == "__main__":
    try:
        # 填写主账号ID。
        userid = "11470***876***55"
        # 这里以北京区域为例。
        endpoint = "cn-beijing.mgw.aliyuncs.com"
        # 填写通道ID。
        tunnel_id = "ab31d1f9-****-4f62-****-914e4b2f78c7"

        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("OSS_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("OSS_ACCESS_KEY_SECRET"),
        )
        client = Client(config)
        client.delete_tunnel(userid, tunnel_id)
    except Exception as e:
        print(e)
        

后续步骤

通道创建后,您可以选择继续创建代理,详情请参见代理