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

Uploading an Object - Asynchronous Resumable Upload

Function

The resumable upload is an encapsulated and enhanced version of the multipart upload used for dealing with possible upload failures of large files when the network connection is unstable or a program crashes. This API splits the file into multiple parts and uploads them individually. The upload result of each part is recorded in a checkpoint file in real time. A success message is returned only when all parts are uploaded. If any parts fail, an error message is returned telling you to call the API again to upload the failed parts. Since the checkpoint file contains the progress of each part, it saves you uploading all parts again in the event of an error.

This API is used for asynchronous resumable upload. Compared with synchronous resumable upload (see Uploading an Object - Resumable (SDK for Go)), this API allows you to pause, resume, or cancel the upload.

If you need to pause, resume, or cancel the upload, you are advised to use this API. Otherwise, you are advised to use the synchronous resumable upload API, which is easier to use.

Restrictions

  • To upload an object, you must be the bucket owner or have the required permission (obs:object:PutObject in IAM or PutObject in a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Configuring an Object Policy.
  • The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
  • The file uploaded by the resumable upload API must be larger than 100 KB in size.
  • When the resumable upload API is used, this option should be enabled so that the progress of the last upload can be read for entering the process again.

Method

func (obsClient ObsClient) UploadFileAsync(input *UploadFileInput, extensions ...extensionOptions) (task *TransferTask, err error)

Request Parameters

Table 1 List of request parameters

Parameter

Type

Mandatory (Yes/No)

Description

input

*UploadFileInput

Yes

Explanation:

Input parameters for a resumable upload. For details, see Table 2.

Table 2 UploadFileInput

Parameter

Type

Mandatory (Yes/No)

Description

Bucket

string

Yes

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

Yes

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

UploadFile

string

Yes

Explanation:

Local file path of the object to be uploaded

Default value:

None

PartSize

int64

Yes

Explanation:

Part size

Value range:

The value ranges from 100 KB to 5 GB, in bytes.

Default value:

102400

TaskNum

int

No

Explanation:

Maximum number of parts that can be uploaded concurrently

Value range:

An integer from 1 to 10000

Default value:

1, indicating concurrent uploads are not used

EnableCheckpoint

bool

No

Explanation:

Whether to enable the resumable upload

Value range:

true: The resumable upload mode is enabled.

false: The resumable upload mode is disabled.

Default value:

false

CheckpointFile

string

No

Explanation:

File used to record the upload progress. This parameter is valid only in the resumable upload mode.

Default value:

If this parameter is left blank, the progress file will be in the same directory as the local file to be uploaded.

ACL

AclType

No

Explanation:

ACL that can be pre-defined during object creation. For details about the ACL, see ACLs.

Value range:

For details about the ACL options, see AclType.

Default value:

None

WebsiteRedirectLocation

string

No

Explanation:

If the bucket is configured with website hosting, the request for obtaining the object can be redirected to another object in the bucket or an external URL.

The request is redirected to object anotherPage.html in the same bucket:

WebsiteRedirectLocation:/anotherPage.html

The request is redirected to an external URL http://www.example.com/:

WebsiteRedirectLocation:http://www.example.com/

OBS obtains the specified value from the header and stores it in the object metadata WebsiteRedirectLocation.

Restrictions:

  • The value must start with a slash (/), http://, or https:// and cannot exceed 2 KB.
  • OBS only supports redirection for objects in the root directory of a bucket.

Default value:

None

ContentType

string

No

Explanation:

MIME type of the file to be uploaded. MIME type is a standard way of describing a data type and is used by the browser to decide how to display data.

Value range:

See What Is Content-Type (MIME)?

Default value:

If you do not specify this parameter when uploading an object, the SDK determines the object type based on the suffix of the specified object name and automatically assigns a value to this parameter.

SseHeader

SseCHeader or SseKmsHeader

No

Explanation:

Server-side encryption header information. If SSE-C is used, see Table 4. If SSE-KMS is used, see Table 5.

StorageClass

StorageClassType

No

Explanation:

Object storage class

Value range:

See Table 6.

Default value:

If this parameter is not specified, the object inherits the storage class of the bucket.

Metadata

map[string]string

No

Explanation:

Custom metadata of the object to be uploaded. You can add a header starting with x-obs-meta- in the request to define metadata. The custom metadata will be returned in the response when you retrieve the object or query the object metadata.

Restrictions:

  • The custom metadata cannot exceed 8 KB. To measure the custom metadata, sum the number of bytes in the UTF-8 encoding of each key and value.
  • The custom metadata keys are case insensitive, but are stored in lowercase in OBS. The key values are case sensitive.
  • Both custom metadata keys and their values must conform to US-ASCII standards. If non-ASCII or unrecognizable characters are necessary, they must be encoded and decoded in URL or Base64 on the client, because the server side does not perform any decoding.

Default value:

None

Expires

int64

No

Explanation:

Expiration time of the object (calculated from the latest modification time of the object). Expired objects are automatically deleted.

Value range:

1 to (263 - 1), in days

Default value:

None

GrantReadId

string

No

Explanation:

ID (domain_id) of an account the READ permission is granted to. The account with the READ permission can read the current object and obtain its metadata.

Value range:

To obtain the account ID, see How Do I Get My Account ID and User ID?

Default value:

None

GrantReadAcpId

string

No

Explanation:

ID (domain_id) of an account the READ_ACP permission is granted to. The account with the READ_ACP permission can read the ACL of the current object.

Value range:

To obtain the account ID, see How Do I Get My Account ID and User ID?

Default value:

None

GrantWriteAcpId

string

No

Explanation:

ID (domain_id) of an account the WRITE_ACP permission is granted to. The account with the WRITE_ACP permission can write the ACL of the current object.

Value range:

To obtain the account ID, see How Do I Get My Account ID and User ID?

Default value:

None

GrantFullControlId

string

No

Explanation:

ID (domain_id) of an account the FULL_CONTROL permission is granted to. The account with the FULL_CONTROL permission can read the current object, obtain its metadata, and obtain and write its ACL.

Value range:

To obtain the account ID, see How Do I Get My Account ID and User ID?

Default value:

None

EncodingType

string

No

Explanation:

Encoding type for Key in the response. If Key in the response contains control characters that are not supported by the XML 1.0 standard, you can specify this parameter to encode Key.

Value range:

url

Default value:

None. If you leave this parameter blank, encoding is not applied to Key.

Table 3 AclType

Constant

Default Value

Description

AclPrivate

private

Private read/write

A bucket or object can only be accessed by its owner.

AclPublicRead

public-read

Public read and private write

If this permission is granted on a bucket, anyone can read the object list, multipart tasks, metadata, and object versions in the bucket.

If it is granted on an object, anyone can read the content and metadata of the object.

AclPublicReadWrite

public-read-write

Public read/write

If this permission is granted on a bucket, anyone can read the object list, multipart tasks, metadata, and object versions in the bucket, and can upload or delete objects, initiate multipart upload tasks, upload parts, assemble parts, copy parts, and abort multipart upload tasks.

If it is granted on an object, anyone can read the content and metadata of the object.

AclPublicReadDelivered

public-read-delivered

Public read on a bucket as well as objects in the bucket

If this permission is granted on a bucket, anyone can read the object list, multipart tasks, metadata, and object versions, and read the content and metadata of objects in the bucket.

NOTE:

AclPublicReadDelivered does not apply to objects.

AclPublicReadWriteDelivered

public-read-write-delivered

Public read/write on a bucket as well as objects in the bucket

If this permission is granted on a bucket, anyone can read the object list, multipart uploads, metadata, and object versions in the bucket, and can upload or delete objects, initiate multipart upload tasks, upload parts, assemble parts, copy parts, and abort multipart uploads. They can also read the content and metadata of objects in the bucket.

NOTE:

AclPublicReadWriteDelivered does not apply to objects.

AclBucketOwnerFullControl

bucket-owner-full-control

If this permission is granted on an object, only the bucket and object owners have the full control over the object.

By default, if you upload an object to a bucket of any other user, the bucket owner does not have the permissions on your object. After you grant this permission to the bucket owner, the bucket owner can have full control over your object.

Table 4 SseCHeader

Parameter

Type

Mandatory (Yes/No)

Description

Encryption

string

Yes if used as a request parameter

Explanation:

SSE-C used for encrypting objects

Restrictions:

None

Value range:

AES256, indicating objects are encrypted using SSE-C

Default value:

None

Key

string

Yes if used as a request parameter

Explanation:

Key for encrypting the object when SSE-C is used

Restrictions:

The value is a Base64-encoded 256-bit key, for example, K7QkYpBkM5+hca27fsNkUnNVaobncnLht/rCB2o/9Cw=.

Value range:

None

Default value:

None

KeyMD5

string

No if used as a request parameter

Explanation:

MD5 value of the key for encrypting objects when SSE-C is used. This value is used to check whether any error occurs during the transmission of the key.

Restrictions:

The value is encrypted by MD5 and then encoded by Base64, for example, 4XvB3tbNTN+tIEVa0/fGaQ==.

Value range:

None

Default value:

None

Table 5 SseKmsHeader

Parameter

Type

Mandatory (Yes/No)

Description

Encryption

string

Yes if used as a request parameter

Explanation:

SSE-KMS is used for encrypting objects on the server side.

Value range:

kms

Default value:

None

Key

string

No if used as a request parameter

Explanation:

ID of the KMS master key when SSE-KMS is used

Value range:

Valid value formats are as follows:

  1. regionID:domainID:key/key_id
  2. key_id

In the preceding formats:

Default value:

  • If this parameter is not specified, the default master key will be used.
  • If there is no such a default master key, OBS will create one and use it by default.

ProjectID

string

No if used as a request parameter

Explanation:

ID of the project (not an enterprise project) where the KMS CMK belongs when SSE-KMS is used

Restrictions:

None

Value range:

Project ID (not enterprise project ID) that matches the key ID specified by Key.

Default value:

None

Table 6 StorageClassType

Constant

Default Value

Description

StorageClassStandard

STANDARD

OBS Standard

Features low access latency and high throughput and is used for storing massive, frequently accessed (multiple times a month) or small objects (< 1 MB) requiring quick response.

StorageClassWarm

WARM

OBS Infrequent Access

Used for storing data that is semi-frequently accessed (fewer than 12 times a year) but is instantly available when needed.

StorageClassCold

COLD

OBS Archive

Used for storing rarely accessed (once a year) data.

Responses

Table 7 TransferTask parameters

Method

Description

Status() TransferStatusType

Obtains the status of the current task.

Pause() error

Pauses the task.

Resume() error

Resumes the task.

Cancel() error

Cancels the task.

Done() <-chan struct{}

Obtains the completion signal channel.

GetResult() (interface{}, error)

Obtains the upload result.

After the upload is successful, use GetResult () to obtain the result. CompleteMultipartUploadOutput is returned.

Table 8 CompleteMultipartUploadOutput parameters

Field

Type

Description

VersionId

string

Object version ID (available in a versioning-enabled bucket).

ETag

string

ETag of the object.

Location

string

Bucket location.

Bucket

string

Bucket name.

Key

string

Object key.

Sample Code

Example 1: This example uses the asynchronous resumable method to upload local file example to bucket examplebucket as an object named new-example.

 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
package main
import (
    "fmt"
    "os"

    obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)
func main() {
    // Obtain an AK/SK pair using environment variables or import an 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, err := obs.New(ak, sk, endPoint/*, obs.WithSecurityToken(securityToken)*/)
    if err != nil {
        fmt.Printf("Create obsClient error, errMsg: %s", err.Error())
    }
// Prepare the input for upload.
    input := &obs.UploadFileInput{
        Bucket:     "examplebucket",
        Key:        "new-example",
        UploadFile: "/path/to/local/example",
        PartSize:   10 * 1024 * 1024, // 10 MB
        TaskNum:    3,                // Three concurrent tasks
    }

    // Start asynchronous upload.
    task, err := client.UploadFileAsync(input)
    if err != nil {
        log.Fatal(err)
    }

    // Wait until the upload is complete.
    <-task.Done()

    // Obtain the upload result.
    result, err := task.GetResult()
    if err != nil {
        fmt.Printf("Upload failed: %v\n", err)
        return
    }

    output := result.(*obs.CompleteMultipartUploadOutput)
    fmt.Printf("Upload succeeded.\nETag: %s\nVersionId: %s\n", output.ETag, output.VersionId)
}

Example 2: This example uses the asynchronous resumable method to upload local file example to bucket examplebucket as an object named new-example and obtains the upload progress through callback.

 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
package main
import (
    "fmt"
    "os"

    obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)

// Progress listener
type ProgressListener struct{}

func (l *ProgressListener) ProgressChanged(event *obs.ProgressEvent) {
    switch event.EventType {
    case obs.TransferStartedEvent:
        fmt.Printf("Upload started. Total size: %d bytes\n", event.TotalBytes)
    case obs.TransferDataEvent:
        percent := float64(event.ConsumedBytes) / float64(event.TotalBytes) * 100
        fmt.Printf("Uploading: %.2f%% (%d/%d bytes)\n", percent, event.ConsumedBytes, event.TotalBytes)
    case obs.TransferCompletedEvent:
        fmt.Println("Upload completed.")
    case obs.TransferFailedEvent:
        fmt.Printf("Upload failed: %d/%d bytes transferred.\n", event.ConsumedBytes, event.TotalBytes)
    }
}
func main() {
    // Obtain an AK/SK pair using environment variables or import an 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, err := obs.New(ak, sk, endPoint/*, obs.WithSecurityToken(securityToken)*/)
    if err != nil {
        fmt.Printf("Create obsClient error, errMsg: %s", err.Error())
    }
input := &obs.UploadFileInput{
        Bucket:     "examplebucket",
        Key:        "new-example",
        UploadFile: "/path/to/local/example",
        PartSize:   10 * 1024 * 1024,
        TaskNum:    3,
    }

    // Add a progress listener.
    task, err := client.UploadFileAsync(input, obs.WithProgressListener(&ProgressListener{}))
    if err != nil {
        log.Fatal(err)
    }

    // Wait until the task is complete.
    <-task.Done()

    result, err := task.GetResult()
    if err != nil {
        fmt.Printf("Upload failed: %v\n", err)
        return
    }

    output := result.(*obs.CompleteMultipartUploadOutput)
    fmt.Printf("Upload succeeded. ETag: %s\n", output.ETag)
}

Example 3: This example suspends and resumes an asynchronous resumable upload.

 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
package main
import (
    "fmt"
    "os"

    obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)
func main() {
    // Obtain an AK/SK pair using environment variables or import an 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, err := obs.New(ak, sk, endPoint/*, obs.WithSecurityToken(securityToken)*/)
    if err != nil {
        fmt.Printf("Create obsClient error, errMsg: %s", err.Error())
    }
    input := &obs.UploadFileInput{
        Bucket:     "examplebucket",
        Key:        "new-example",
        UploadFile: "/path/to/local/example",
        PartSize:   10 * 1024 * 1024,
        TaskNum:    3,
    }

    task, err := client.UploadFileAsync(input)
    if err != nil {
        log.Fatal(err)
    }

    // Pause the upload task after 5 seconds of execution.
    go func() {
        time.Sleep(5 * time.Second)
        fmt.Println("Pausing the upload...")
        err := task.Pause()
        if err != nil {
            fmt.Printf("Pause failed: %v\n", err)
        } else {
            fmt.Println("Upload paused.")
        }
    }()

    // Wait until the task is paused.
    for task.Status() != obs.TransferStatusPaused {
        time.Sleep(100 * time.Millisecond)
    }

    fmt.Println("Current status: paused")

    // Resume the task after 3 seconds of pause.
    time.Sleep(3 * time.Second)
    fmt.Println("Resuming upload...")
    err = task.Resume()
    if err != nil {
        fmt.Printf("Resumption failed: %v\n", err)
    } else {
        fmt.Println("Upload resumed.")
    }

    // Wait until the task is complete.
    <-task.Done()

    result, err := task.GetResult()
    if err != nil {
        fmt.Printf("Upload failed: %v\n", err)
        return
    }

    output := result.(*obs.CompleteMultipartUploadOutput)
    fmt.Printf("Upload succeeded. ETag: %s\n", output.ETag)
}

Example 4: This example cancels an asynchronous resumable upload.

 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
package main
import (
    "fmt"
    "os"

    obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)
func main() {
    // Obtain an AK/SK pair using environment variables or import an 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, err := obs.New(ak, sk, endPoint/*, obs.WithSecurityToken(securityToken)*/)
    if err != nil {
        fmt.Printf("Create obsClient error, errMsg: %s", err.Error())
    }
    input := &obs.UploadFileInput{
        Bucket:     "examplebucket",
        Key:        "new-example",
        UploadFile: "/path/to/local/example",
        PartSize:   10 * 1024 * 1024,
        TaskNum:    3,
    }

    task, err := client.UploadFileAsync(input)
    if err != nil {
        log.Fatal(err)
    }

    // Cancel the upload task after 5 seconds of execution.
    go func() {
        time.Sleep(5 * time.Second)
        fmt.Println("Canceling the upload...")
        err := task.Cancel()
        if err != nil {
            fmt.Printf("Cancellation failed: %v\n", err)
        } else {
            fmt.Println("Upload canceled.")
        }
    }()

    // Wait until the task is complete or canceled.
    <-task.Done()

    // Check the result.
    result, err := task.GetResult()
    if err != nil {
        if err.Error() == "upload task cancelled" {
            fmt.Println("Upload canceled by the user.")
        } else {
            fmt.Printf("Upload failed: %v\n", err)
        }
        return
    }

    output := result.(*obs.CompleteMultipartUploadOutput)
    fmt.Printf("Upload succeeded. ETag: %s\n", output.ETag)

}