You need to enable JavaScript to run this app.
导航
Post 表单预签名(Go SDK)
最近更新时间:2024.02.04 18:31:01首次发布时间:2023.01.18 16:55:22

本文介绍 Post 表单预签名的示例代码。

示例代码

package main

import (
   "context"
   "fmt"

   "github.com/volcengine/ve-tos-golang-sdk/v2/tos"
)

func checkErr(err error) {
   if err != nil {
      if serverErr, ok := err.(*tos.TosServerError); ok {
         fmt.Println("Error:", serverErr.Error())
         fmt.Println("Request ID:", serverErr.RequestID)
         fmt.Println("Response Status Code:", serverErr.StatusCode)
         fmt.Println("Response Header:", serverErr.Header)
         fmt.Println("Response Err Code:", serverErr.Code)
         fmt.Println("Response Err Msg:", serverErr.Message)
      } else {
         fmt.Println("Error:", err)
      }
      panic(err)
   }
}

func main() {
   var (
      accessKey = os.Getenv("TOS_ACCESS_KEY")
      secretKey = os.Getenv("TOS_SECRET_KEY")
      // Bucket 对应的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com
      endpoint = "https://tos-cn-beijing.volces.com"
      region   = "cn-beijing"
      // 填写 BucketName
      bucketName = "*** Provide your bucket name ***"

      // 指定的 ObjectKey
      objectKey = "*** Provide your object key ***"
      ctx       = context.Background()
   )
   // 初始化客户端
   client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
   checkErr(err)
   
   // 上传签名
   res, err := client.PreSignedPostSignature(ctx, &tos.PreSignedPostSignatureInput{
      Bucket:  bucketName,
      Key:     objectKey,
      Expires: 3600,
   })
   fmt.Println("Policy:", res.Policy)
   fmt.Println("OriginPolicy:", res.OriginPolicy)
   fmt.Println("Algorithm:", res.Algorithm)
   fmt.Println("Credential:", res.Credential)
   fmt.Println("Date:", res.Date)
   fmt.Println("Signature:", res.Signature)
}

相关文档

关于 POST 表单预签名的详细信息,请参见基于浏览器上传的表单中包含签名