代理

更新时间:

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

创建代理

说明

使用代理前务必先部署代理,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()  {
   // 填写通道ID。
   tunnelId := "ab31d1f9-****-4f62-****-914e4b2f78c7"
   // 这里以北京区域为例。
   endpoint := "cn-beijing.mgw.aliyuncs.com"
   // 填写代理名称。
   agentName := "exampleagent"
   // 使用网络,公网请填写public, 专线或者VPN请填写vpc。
   agentEndpoint := "public"
   // 部署方式,目前仅支持填写default。
   deployMethod := "default"
   
   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
   }
   info := mgwpackage.CreateAgentInfo{
      AgentEndpoint : &agentEndpoint,
      DeployMethod : &deployMethod,
      Name : &agentName,
      TunnelId : &tunnelId,
   }
   _, err = client.CreateAgent(&userId, &mgwpackage.CreateAgentRequest{ImportAgent: &info})
   if err != nil {
      fmt.Errorf("create agent failed, %v", err)
      return
   }
}

查询代理状态

以下示例代码用于查询指定代理状态。

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"
   // 填写代理名称。
   agentName := "exampleagent"
   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.GetAgentStatus(&userId, &agentName)
   if err != nil{
      fmt.Errorf("get client status failed, %v", err)
      return
   }
   jsonBytes, err := json.Marshal(resp)
   if err != nil {
      fmt.Println("covert failed,", err)
      return
   }
   // 如果status字段显示的是OK,说明代理状态正常,其它任何值都表示代理异常。
   fmt.Println(string(jsonBytes))
}

正常返回示例

{
  "ImportAgentStatus": {
    "Status": "OK"
  }
}

获取代理详情

以下示例代码用于获取指定代理详情信息。

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"
   // 填写代理名称。
   agentName := "exampleagent"
   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.GetAgent(&userId, &agentName)
   if err != nil{
      fmt.Errorf("get agent failed, %v", err)
      return
   }
   jsonBytes, err := json.Marshal(resp)
   if err != nil {
      fmt.Println("covert failed,", err)
      return
   }
   fmt.Println(string(jsonBytes))
}

正常返回示例

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

列举代理

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

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.ListAgent(&userId, &mgwpackage.ListAgentRequest{
      &count, &marker,
   })
   if err != nil{
      fmt.Errorf("list agent failed, %v", err)
      return
   }
   jsonBytes, err := json.Marshal(resp)
   if err != nil {
      fmt.Println("covert failed,", err)
      return
   }
   fmt.Println(string(jsonBytes))
}

正常返回示例

{
  "ImportAgentList": {
    "Truncated": true,
    "NextMarker": "test_next_marker",
    "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"
      }
    ]
  }
}

删除代理

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

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"
   // 填写代理名称。
   agentName := "exampleagent"
   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.DeleteAgent(&userId, &agentName)
   if err != nil{
      fmt.Errorf("delete agent failed, %v", err)
      return
   }
}

后续步骤

代理创建后,您可以选择继续创建数据地址,详情请参见数据地址