阿里云函数计算http触发器 操作接口实例

先测试触发器

https://api.aliyun.com/api/FC/2023-03-30/GetTrigger?useCommon=true

得到

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{
  "triggerName": "h t t p",
  "description": "openapi控制",
  "triggerId": "XXXXXX",
  "sourceArn": null,
  "triggerType": "http",
  "invocationRole": null,
  "qualifier": "LATEST",
  "triggerConfig": "{\"methods\":[\"GET\",\"POST\",\"PUT\",\"DELETE\"],\"authType\":\"anonymous\",\"disableURLInternet\":false}",
  "createdTime": "2023-11-28T23:16:27Z",
  "lastModifiedTime": "2023-11-28T23:22:11Z",
  "status": null,
  "targetArn": null,
  "httpTrigger": {
    "urlInternet": "https://X",
    "urlIntranet": "https://"
  }
}

主要是 triggerConfig

1
{"methods":["GET","POST","PUT","DELETE"],"authType":"anonymous","disableURLInternet":false}

获得golang

阿里云的这个地方 泛化调用方案(Common模式) 有bug 只能用 特化调用代码

1
2
3
4
go get github.com/alibabacloud-go/fc-20230330/v3/client
go get github.com/alibabacloud-go/darabonba-openapi/v2/client
go get github.com/alibabacloud-go/tea-utils/v2/service
go get github.com/alibabacloud-go/tea/tea

然后修改下面代码中的 TriggerConfig ,比如禁止公网访问 等

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

package main

import (
  "encoding/json"
  "strings"
  "fmt"
  "os"
  fc20230330  "github.com/alibabacloud-go/fc-20230330/v3/client"
  openapi  "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  util  "github.com/alibabacloud-go/tea-utils/v2/service"
  "github.com/alibabacloud-go/tea/tea"
)


/**
 * 使用AK&SK初始化账号Client
 * @param accessKeyId
 * @param accessKeySecret
 * @return Client
 * @throws Exception
 */
func CreateClient (accessKeyId *string, accessKeySecret *string) (_result *fc20230330.Client, _err error) {
  config := &openapi.Config{
    // 必填,您的 AccessKey ID
    AccessKeyId: accessKeyId,
    // 必填,您的 AccessKey Secret
    AccessKeySecret: accessKeySecret,
  }
  // Endpoint 请参考 https://api.aliyun.com/product/FC
  config.Endpoint = tea.String("X X X X X X X.cn-shanghai.fc.aliyuncs.com")
  _result = &fc20230330.Client{}
  _result, _err = fc20230330.NewClient(config)
  return _result, _err
}

func _main (args []*string) (_err error) {
  // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
  // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html
  client, _err := CreateClient(tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")), tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")))
  if _err != nil {
    return _err
  }

  updateTriggerInput := &fc20230330.UpdateTriggerInput{
    TriggerConfig: tea.String("{\"methods\":[\"GET\",\"POST\",\"PUT\",\"DELETE\"],\"authType\":\"anonymous\",\"disableURLInternet\":false}"),
  }
  updateTriggerRequest := &fc20230330.UpdateTriggerRequest{
    Body: updateTriggerInput,
  }
  runtime := &util.RuntimeOptions{}
  headers := make(map[string]*string)
  tryErr := func()(_e error) {
    defer func() {
      if r := tea.Recover(recover()); r != nil {
        _e = r
      }
    }()
    // 复制代码运行请自行打印 API 的返回值
    _, _err = client.UpdateTriggerWithOptions(tea.String("FCNAME"), tea.String("httpX X X X"), updateTriggerRequest, headers, runtime)
    if _err != nil {
      return _err
    }

    return nil
  }()

  if tryErr != nil {
    var error = &tea.SDKError{}
    if _t, ok := tryErr.(*tea.SDKError); ok {
      error = _t
    } else {
      error.Message = tea.String(tryErr.Error())
    }
    // 错误 message
    fmt.Println(tea.StringValue(error.Message))
    // 诊断地址
    var data interface{}
    d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
    d.Decode(&data)
    if m, ok := data.(map[string]interface{}); ok {
      recommend, _ := m["Recommend"]
      fmt.Println(recommend)
    }
    _, _err = util.AssertAsString(error.Message)
    if _err != nil {
      return _err
    }
  }
  return _err
}


func main() {
  err := _main(tea.StringSlice(os.Args[1:]))
  if err != nil {
    panic(err)
  }
}
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计