Updated on 2026-08-14 GMT+08:00

Uploading an Object - Browser-Based (SDK for Go)

Function

This API upload an object smaller than 5 GB to a specified bucket in HTML form. You can perform the following steps to perform a browser-based upload:

  1. Call ObsClient.CreateBrowserBasedSignature to generate request parameters for authentication.
  2. Prepare an HTML form page.
  3. Enter the request parameters in the HTML page.
  4. Select a local file and upload it in browser-based mode.

There are two request parameters generated:

Restrictions

  • The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
  • Values of policy and signature in the HTML form are obtained from the returned result of ObsClient.createPostSignature.

Method

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

Request Parameters

Table 1 List of request parameters

Parameter

Type

Mandatory (Yes/No)

Description

input

*CreateBrowserBasedSignatureInput

Yes

Explanation:

Input parameters for a browser-based upload. For details, see Table 2.

Table 2 CreateBrowserBasedSignatureInput

Parameter

Type

Mandatory (Yes/No)

Description

Bucket

string

No

Explanation:

Bucket name

Restrictions:

  • A bucket name must be unique across all accounts and regions.
  • A bucket name:
    • Must be 3 to 63 characters long and start with a digit or letter. Lowercase letters, digits, hyphens (-), and periods (.) are allowed.
    • Cannot be formatted as an IP address.
    • Cannot start or end with a hyphen (-) or period (.).
    • Cannot contain two consecutive periods (..), for example, my..bucket.
    • Cannot contain a period (.) and a hyphen (-) adjacent to each other, for example, my-.bucket or my.-bucket.
  • If you repeatedly create buckets of the same name in the same region, no error will be reported and the bucket attributes comply with those set in the first creation request.

Default value:

None

Key

string

No

Explanation:

Object name. An object is uniquely identified by an object name in a bucket. An object name is a complete path that does not contain the bucket name.

For example, if the address for accessing the object is examplebucket.obs.ap-southeast-1.myhuaweicloud.com/folder/test.txt, the object name is folder/test.txt.

Value range:

The value must contain 1 to 1,024 characters.

Default value:

None

Expires

int

No

Explanation:

Expiration time of authentication for a browser-based upload

Value range:

A positive integer, in seconds

Default value:

300

FormParams

map[string]string

No

Explanation:

Parameters of a browser-based upload, not including key, policy, and signature

Value range:

acl, cache-control, content-type, content-disposition, content-encoding, and expires

Default value:

None

Responses

Table 3 List of returned results

Parameter

Type

Description

output

*CreateBrowserBasedSignatureOutput

Explanation:

Returned results. For details, see Table 4.

err

error

Explanation:

Error messages returned by the API

Table 4 CreateBrowserBasedSignatureOutput

Parameter

Type

Description

OriginPolicy

string

Explanation:

policy not encoded by Base64. This parameter can only be used for verification.

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

Default value:

None

Policy

string

Explanation:

Base64-encoded value of policy in the form

Example: eyJleHBpcmF0aW9uIjoiMjAyMy0wOS0xMlQxMjo1Mjo1OVoiLCJjb25kaXRpb25zIjpbeyJjb250ZW50LXR5cGUiOiJ0ZXh0L3BsYWluIn0seyJidWNrZXQiOiJleGFtcGxlYnVja2V0In0seyJrZXkiOiJleGFtcGxlL29iamVjdG5hbWUifSxdfQ==

Default value:

None

Signature

string

Explanation:

signature in the form

Example: g0jQr4v9VWd1Q2FOFDG6LGfV9Cw=

Default value:

None

Code Examples

 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
// Import a dependency.
import (
       "fmt"
       "obs"
)


func main() {
    //Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
    //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
    ak := os.Getenv("AccessKeyID")
    sk := os.Getenv("SecretAccessKey")
    // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are not advised to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways.
    // securityToken := os.Getenv("SecurityToken")
    // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
    endPoint := "https://obs.ap-southeast-1.myhuaweicloud.com"
    // Create an obsClient instance.
    // If you use a temporary AK/SK pair and a security token to access OBS, use the obs.WithSecurityToken method to specify a security token when creating an instance.
    obsClient, _:= obs.New(ak, sk, endPoint/*, obs.WithSecurityToken(securityToken)*/)

    input := &obs.CreateBrowserBasedSignatureInput{}
    // Set form parameters.
    formParams := make(map[string]string)
    // Set the object ACL to public read.
    formParams["x-obs-acl"] = string(obs.AclPublicRead)
    // Specify the MIME type for the object.
    formParams["content-type"] = "text/plain"
    input.FormParams = formParams
    // Set the validity period of the browser-based upload request, in seconds.
    input.Expires = 3600
    output, _ := obsClient.CreateBrowserBasedSignature(input)
    // Obtain the request parameters.
    fmt.Printf("Policy:%s\n", output.Policy)
    fmt.Printf("Signature:%s\n", output.Signature)
}

Sample code of an HTML form is as follows:

 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
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <form action="http://bucketname.your-endpoint/" method="post" enctype="multipart/form-data">
            <p>
                Object key
            </p>            
            <!-- Object name -->
            <input type="text" name="key" value="objectname" />
            <p>
                ACL
            </p>
            <!-- Object ACL permission -->
            <input type="text" name="x-obs-acl" value="public-read" />
            <p>
                Content-Type
            </p>
            <!-- MIME type of the object -->
            <input type="text" name="content-type" value="text/plain" />
            <p>            
                <!-- Base64-encoded policy -->
                <input type="hidden" name="policy" value="*** Provide your policy ***" />
                <!-- AK -->
                <input type="hidden" name="AccessKeyId" value="*** Provide your access key ***"/>
                <!-- Signature string -->
                <input type="hidden" name="signature" value="*** Provide your signature ***"/>
                <input name="file" type="file" />
                <input name="submit" value="Upload" type="submit" />
            </p>
        </form>
    </body>
</html>