全部產品
Search
文件中心

Data Online Migration:代理

更新時間:Dec 11, 2025

本文介紹如何使用SDK調用代理相關的方法。

建立代理

說明

使用代理前務必先部署代理程式,SDK無法自動部署代理,請參考文檔部署代理程式 按照文檔提示操作。僅當使用專線或者VPN的情況下才需要建立代理,公網情境一般無須建立代理。

以下範例程式碼用於建立代理。

import os

from alibabacloud_hcs_mgw20240626.client import Client
from alibabacloud_hcs_mgw20240626.models import CreateAgentRequest, \
    CreateAgentInfo
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"
        # 填寫代理名稱。
        agent_name = "exampleagent"
        # 使用網路,公網請填寫public, 專線或者VPN請填寫vpc。
        agent_endpoint = "public"
        # 部署方式,目前僅支援填寫default。
        deploy_method = "default"
        # 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
            protocol="http"
        )
        client = Client(config)
        response = client.create_agent(userid, CreateAgentRequest(CreateAgentInfo(
            agent_endpoint=agent_endpoint,
            deploy_method=deploy_method,
            name=agent_name,
            tunnel_id=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"
        # 填寫代理名稱。
        agent_name = "exampleagent"
        # 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
            protocol="http"
        )
        client = Client(config)
        response = client.get_agent_status(userid, agent_name)
        print(response.body)
    except Exception as e:
        print(e)

正常返回樣本

{
  "ImportAgentStatus": {
    "Status": "OK",
    "AgentIP": "192.168.0.2",
    "AgentVersion": "1.6.0"
  }
}

擷取代理詳情

以下範例程式碼用於擷取指定代理詳情資訊。

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"
        # 填寫代理名稱。
        agent_name = "exampleagent"
        # 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
            protocol="http"
        )
        client = Client(config)
        response = client.get_agent(userid, agent_name)
        print(response.body)
    except Exception as e:
        print(e)
 

正常返回樣本

{
  "ImportAgent": {
    "Owner": "test_owner",
    "Name": "test_name",
    "CreateTime": "2024-05-01T12:00:00.000Z",
    "ModifyTime": "2024-05-01T12:00:00.000Z",
    "DeployMethod": "default",
    "AgentEndpoint": "vpc",
    "ActivationKey": "6af62558-970d-4f44-8663-4e297170fd6a",
    "Tags": "K1:V1,K2:V2",
    "Version": "test_agent_id",
    "TunnelId": "test_tunnel_id"
  }
}

列舉代理

以下範例程式碼用於列舉帳號下所有代理資訊。

import os

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

if __name__ == "__main__":
    try:
        # 填寫主帳號ID。
        userid = "11470***876***55"
        # 這裡以北京地區為例。
        endpoint = "cn-beijing.mgw.aliyuncs.com"
        # 填寫代理名稱。
        agent_name = "exampleagent"
        # 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
            protocol="http"
        )
        client = Client(config)
        # 根據實際填寫marker, count。
        count = 1
        marker = ""
        response = client.list_agent(userid, ListAgentRequest(
            marker=marker,
            count=count,
        ))
        for import_agent in response.body.import_agent_list.import_agent:
            print(import_agent)
    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"
        # 填寫代理名稱。
        agent_name = "exampleagent"
        # 支援 HTTPS、HTTP, 未指定時預設採用 HTTPs。
        config = Config(
            endpoint=endpoint,
            access_key_id=os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
            access_key_secret=os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
            protocol="http"
        )
        client = Client(config)
        client.delete_agent(userid, agent_name)
    except Exception as e:
        print(e)

後續步驟

代理建立後,您可以選擇繼續建立資料地址,詳情請參見資料地址