文档首页> 对象存储服务 OBS> Go> 其他接口(Go SDK)> 生成带授权信息的表单上传参数(Go SDK)
更新时间:2024-02-01 GMT+08:00

生成带授权信息的表单上传参数(Go SDK)

功能说明

生成用于鉴权的请求参数,以进行基于浏览器的POST表单上传。

使用SDK生成的用于鉴权的请求参数包括两个:

  • Policy,对应表单中policy字段,
  • Signature,对应表单中的x-obs-signature字段。

接口约束

  • 您必须是桶拥有者或拥有上传对象的权限,才能上传对象。建议使用IAM或桶策略进行授权,如果使用IAM则需授予obs:object:PutObject权限,如果使用桶策略则需授予PutObject权限。相关授权方式介绍可参见OBS权限控制概述,配置方式详见使用IAM自定义策略配置对象策略
  • OBS支持的Region与Endpoint的对应关系,详细信息请参见地区与终端节点

方法定义

func (obsClient ObsClient) CreateBrowserBasedSignature(input *CreateBrowserBasedSignatureInput) (output *CreateBrowserBasedSignatureOutput, err error)

请求参数说明

表1 请求参数列表

参数名称

参数类型

是否必选

描述

input

*CreateBrowserBasedSignatureInput

必选

参数解释:

生成带授权信息的表单上传参数接口的请求参数,详情参见CreateBrowserBasedSignatureInput

表2 CreateBrowserBasedSignatureInput

参数名称

参数类型

是否可选

描述

Bucket

string

可选

参数解释

桶名。

约束限制:

  • 桶的名字需全局唯一,不能与已有的任何桶名称重复,包括其他用户创建的桶。
  • 桶命名规则如下:
    • 3~63个字符,数字或字母开头,支持小写字母、数字、“-”、“.”。
    • 禁止使用IP地址。
    • 禁止以“-”或“.”开头及结尾。
    • 禁止两个“.”相邻(如:“my..bucket”)。
    • 禁止“.”和“-”相邻(如:“my-.bucket”和“my.-bucket”)。
  • 同一用户在同一个区域多次创建同名桶不会报错,创建的桶属性以第一次请求为准。

默认取值:

Key

string

可选

参数解释:

对象名。对象名是对象在存储桶中的唯一标识。对象名是对象在桶中的完整路径,路径中不包含桶名。

例如,您对象的访问地址为examplebucket.obs. ap-southeast-1.emyhuaweicloud.com/folder/test.txt 中,对象名为folder/test.txt。

取值范围:

长度大于0且不超过1024的字符串。

默认取值:

Expires

int

可选

参数解释:

表单上传鉴权的过期时间。

取值范围:

0~(263-1),单位:秒。

默认取值:

300

FormParams

map[string]string

可选

参数解释:

除key,policy,signature外,表单上传时的其他参数。

取值范围:

acl、cache-control、content-type、content-disposition、content-encoding、expires

默认取值:

返回结果说明

表3 返回结果

参数名称

参数类型

描述

output

*CreateBrowserBasedSignatureOutput

参数解释:

接口返回信息,详情参见CreateBrowserBasedSignatureOutput

err

error

参数解释:

接口返回错误信息。

表4 CreateBrowserBasedSignatureOutput

参数名称

参数类型

描述

OriginPolicy

string

参数解释:

Policy未经过base64之前的值,仅用于校验。示例如下:

{"expiration":"2023-09-12T12:52:59Z","conditions":[{"content-type":"text/plain"},{"bucket":"examplebucket"},{"key":"example/objectname"},]}"

默认取值:

Policy

string

参数解释:

表单中的policy,已经base64之后的值。示例如下:

eyJleHBpcmF0aW9uIjoiMjAyMy0wOS0xMlQxMjo1Mjo1OVoiLCJjb25kaXRpb25zIjpbeyJjb250ZW50LXR5cGUiOiJ0ZXh0L3BsYWluIn0seyJidWNrZXQiOiJleGFtcGxlYnVja2V0In0seyJrZXkiOiJleGFtcGxlL29iamVjdG5hbWUifSxdfQ==

默认取值:

Signature

string

参数解释:

表单中的signature。示例如下:

g0jQr4v9VWd1Q2FOFDG6LGfV9Cw=

默认取值:

代码样例

本示例用于生成POST上传对象的带授权信息的URL

 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
package main
import (
    "fmt"
    "os"
    obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)
func main() {
    //推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险。
    //您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/intl/zh-cn/usermanual-ca/ca_01_0003.html。
    ak := os.Getenv("AccessKeyID")
    sk := os.Getenv("SecretAccessKey")
    // 【可选】如果使用临时AK/SK和SecurityToken访问OBS,同样建议您尽量避免使用硬编码,以降低信息泄露风险。您可以通过环境变量获取访问密钥AK/SK,也可以使用其他外部引入方式传入。
    // securityToken := os.Getenv("SecurityToken")
    // endpoint填写Bucket对应的Endpoint, 这里以中国-香港为例,其他地区请按实际情况填写。
    endPoint := "https://obs.ap-southeast-1.myhuaweicloud.com"
    // 创建obsClient实例
    // 如果使用临时AKSK和SecurityToken访问OBS,需要在创建实例时通过obs.WithSecurityToken方法指定securityToken值。
    obsClient, err := obs.New(ak, sk, endPoint/*, obs.WithSecurityToken(securityToken)*/)
    if err != nil {
        fmt.Printf("Create obsClient error, errMsg: %s", err.Error())
    }
    // 生成上传对象的带授权信息的URL
    input := &obs.CreateBrowserBasedSignatureInput{}
    input.Bucket = "examplebucket"
    input.Key = "example/objectname"
    input.FormParams = map[string]string{
        "content-type": "text/plain",
        "success_action_redirect": "https://www.example.com",
    }
    output, err := obsClient.CreateBrowserBasedSignature(input)
if err == nil {
        fmt.Printf("Policy:%s\n", output.Policy)
        fmt.Printf("Signature:%s\n", output.Signature)
    } else {
        fmt.Println(err)
        return
    }
    requestBody := &bytes.Buffer{}
    writer := multipart.NewWriter(requestBody)
    writer.WriteField("key", input.Key)
    writer.WriteField("AccessKeyId", ak)
    writer.WriteField("policy", output.Policy)
    writer.WriteField("signature", output.Signature)
    writer.WriteField("success_action_redirect", "https://www.example.com")
    // writer.WriteField("token", obs.WithSecurityToken(securityToken))
    writer.WriteField("Content-Type", "text/plain")
    formFile, _ := writer.CreateFormFile("file", "filename")
    io.Copy(formFile, strings.NewReader("hello OBS!"))
    writer.Close()
    url := "https://" + input.Bucket + "." + strings.Replace(endPoint, "https://", "", 1)
    request, err := http.NewRequest("POST", url, requestBody)
    if err != nil {
        fmt.Println(err)
        return
    }
    request.Header.Set("Content-Type", writer.FormDataContentType())
    client := &http.Client{}
    response, err := client.Do(request)
    if err != nil {
        fmt.Println(err)
        return
    }
    defer response.Body.Close()
    if err == nil {
        fmt.Printf("Use signed-url successful!\n")
        fmt.Printf("Status:%s,Etag:%s\n", response.Status, response.Header.Get("Etag"))
        return
    }
    fmt.Printf("Use signed-url successful!\n")
    fmt.Println("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.")
    fmt.Println(err)
}