Last active
September 19, 2021 12:21
-
-
Save dongfg/b948b4ff31ea15b0125e22740c75a4be to your computer and use it in GitHub Desktop.
腾讯云 CloudBase Open API 参考 - 函数调用 + 数据库操作
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"crypto/hmac" | |
"crypto/sha256" | |
"encoding/hex" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"net/http/httputil" | |
"strconv" | |
"time" | |
) | |
var ( | |
SecretId = "change it" | |
SecretKey = "change it" | |
EnvId = "change it" | |
// 测试云函数 | |
FunctionName = "ping" | |
DocumentName = "ping" | |
) | |
var ( | |
HTTPRequestMethod = "POST" | |
CanonicalURI = "//api.tcloudbase.com/" | |
CanonicalQueryString = "" | |
CanonicalHeaders = "content-type:application/json; charset=utf-8\nhost:api.tcloudbase.com\n" | |
SignedHeaders = "content-type;host" | |
Algorithm = "TC3-HMAC-SHA256" | |
) | |
func main() { | |
now := time.Now() | |
canonicalRequest := | |
HTTPRequestMethod + "\n" + | |
CanonicalURI + "\n" + | |
CanonicalQueryString + "\n" + | |
CanonicalHeaders + "\n" + | |
SignedHeaders + "\n" + | |
sha256hex(``) | |
stringToSign := | |
Algorithm + "\n" + | |
timestamp(now) + "\n" + | |
credentialScope(now) + "\n" + | |
sha256hex(canonicalRequest) | |
fmt.Println(stringToSign) | |
signature := sign(date(now), stringToSign) | |
authorization := fmt.Sprintf("1.0 %s Credential=%s/%s, SignedHeaders=%s, Signature=%s", | |
Algorithm, | |
SecretId, | |
credentialScope(now), | |
SignedHeaders, | |
signature) | |
invokeCloudFunction(authorization, timestamp(now)) | |
insertDocument(authorization, timestamp(now)) | |
} | |
func invokeCloudFunction(authorization string, stamp string) { | |
req, _ := http.NewRequest("POST", fmt.Sprintf("https://tcb-api.tencentcloudapi.com/api/v2/envs/%s/functions/%s:invoke", EnvId, FunctionName), nil) | |
req.Header.Set("X-CloudBase-Authorization", authorization) | |
req.Header.Set("X-CloudBase-TimeStamp", stamp) | |
debug(httputil.DumpRequestOut(req, true)) | |
client := &http.Client{} | |
resp, err := client.Do(req) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer resp.Body.Close() | |
body, _ := ioutil.ReadAll(resp.Body) | |
fmt.Println("response Body:", string(body)) | |
} | |
func insertDocument(authorization string, stamp string) { | |
fakeJson := []byte(`{"data": "{\"name\":\"luke\",\"sex\":\"male\"}"}`) | |
req, _ := http.NewRequest("POST", fmt.Sprintf("https://tcb-api.tencentcloudapi.com/api/v2/envs/%s/databases/%s/documents/fakeId", EnvId, DocumentName), bytes.NewBuffer(fakeJson)) | |
req.Header.Set("X-CloudBase-Authorization", authorization) | |
req.Header.Set("X-CloudBase-TimeStamp", stamp) | |
req.Header.Set("Content-Type", "application/json; charset=utf-8") | |
debug(httputil.DumpRequestOut(req, true)) | |
client := &http.Client{} | |
resp, err := client.Do(req) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer resp.Body.Close() | |
body, _ := ioutil.ReadAll(resp.Body) | |
fmt.Println("response Body:", string(body)) | |
} | |
func sign(date string, stringToSign string) string { | |
secretDate := hmacSha256("TC3"+SecretKey, date) | |
secretService := hmacSha256(secretDate, "tcb") | |
secretSigning := hmacSha256(secretService, "tc3_request") | |
signed := hmacSha256(secretSigning, stringToSign) | |
return hex.EncodeToString([]byte(signed)) | |
} | |
func date(t time.Time) string { | |
return t.UTC().Format("2006-01-02") | |
} | |
func timestamp(t time.Time) string { | |
return strconv.FormatInt(t.Unix(), 10) | |
} | |
func credentialScope(t time.Time) string { | |
return fmt.Sprintf("%s/tcb/tc3_request", date(t)) | |
} | |
func sha256hex(data string) string { | |
sum := sha256.Sum256([]byte(data)) | |
return fmt.Sprintf("%x", sum) | |
} | |
func hmacSha256(key string, data string) string { | |
h := hmac.New(sha256.New, []byte(key)) | |
h.Write([]byte(data)) | |
return string(h.Sum(nil)) | |
} | |
func debug(data []byte, err error) { | |
if err == nil { | |
fmt.Printf("%s\n\n", data) | |
} else { | |
log.Fatalf("%s\n\n", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment