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

Uploading an Object - Obtaining the Upload Progress (SDK for Go)

Function

This function allows you to monitor and obtain the upload progress of an object in real time. By calling the progress listening API, you can obtain the status information such as the start of the upload task, data transfer in progress, upload completion, and upload failure, as well as the number of uploaded bytes and total number of bytes.

Restrictions

Method

obs.WithProgress(progressListener ProgressListener)

Request Parameters

Table 1 List of request parameters

Parameter

Type

Mandatory (Yes/No)

Description

progressListener

interface

Yes

Explanation:

Progress listener. The following methods need to be implemented:

ProgressChanged(event *Table 2)

Table 2 ProgressEvent

Parameter

Default Value

Description

TransferStartedEvent

1

Event triggered when the upload starts.

TransferDataEvent

2

Event triggered continuously during data transmission.

TransferCompletedEvent

3

Event triggered when the upload is complete.

TransferFailedEvent

4

Event triggered when the upload fails.

Responses

The responses follow the format of the original method's function call. For details, see Sample Code - Monitoring the Progress of a Streaming Upload.

Sample Code - Monitoring the Progress of a Streaming Upload

This example uses streaming to upload example/objectname to bucket examplebucket and monitors the upload progress.

 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
// Import the dependency package.
import (
     "fmt"
     "bytes"
     "encoding/base64"
     "encoding/json"
     "strings"
     obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)

// Define the progress listener.
type ObsProgressListener struct {
}
// Define the function for processing progress change events.
func (listener *ObsProgressListener) ProgressChanged(event *obs.ProgressEvent) {
    switch event.EventType {
    case obs.TransferStartedEvent:
        fmt.Printf("Transfer Started, ConsumedBytes: %d, TotalBytes %d.\n",
            event.ConsumedBytes, event.TotalBytes)
    case obs.TransferDataEvent:
        fmt.Printf("\rTransfer Data, ConsumedBytes: %d, TotalBytes %d, %d%%.\n",
            event.ConsumedBytes, event.TotalBytes, event.ConsumedBytes*100/event.TotalBytes)
    case obs.TransferCompletedEvent:
        fmt.Printf("\nTransfer Completed, ConsumedBytes: %d, TotalBytes %d.\n",
            event.ConsumedBytes, event.TotalBytes)
    case obs.TransferFailedEvent:
        fmt.Printf("\nTransfer Failed, ConsumedBytes: %d, TotalBytes %d.\n",
            event.ConsumedBytes, event.TotalBytes)
    default:
    }
}

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, err := obs.New(ak, sk, endPoint/*, obs.WithSecurityToken(securityToken)*/)

    input := &obs.PutObjectInput{}
    // Specify the bucket name.
    input.Bucket = "bucketname"
    // Specify the object (example/objectname as an example) to upload.
    input.Key = "objectname"
    input.Body = strings.NewReader("Hello OBS")
    output, err := obsClient.PutObject(input, obs.WithProgress(&ObsProgressListener{})
)
    if err == nil {
        // This method must be invoked.
        defer output.CloseCallbackBody()
        fmt.Printf("RequestId:%s\n", output.RequestId)
        fmt.Printf("ETag:%s\n", output.ETag)
        p := make([]byte, 1024)
        var readErr error
        var readCount int
        // Read the callback content.
        for {
            readCount, readErr = output.ReadCallbackBody(p)
            if readCount > 0 {
                fmt.Printf("%s", p[:readCount])
            }
            if readErr != nil {
                break
            }
        }
    } else if obsError, ok := err.(obs.ObsError); ok {
        fmt.Printf("Code:%s\n", obsError.Code)
        fmt.Printf("Message:%s\n", obsError.Message)
    }
}

Sample Code - Monitoring the Progress of a Resumable Upload

This example uploads example/objectname to bucket examplebucket in a resumable upload and monitors the upload progress.

 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
// Import the dependency package.
import (
     "fmt"
     "bytes"
     "encoding/base64"
     "encoding/json"
     "strings"
     obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)

// Define the progress listener.
type ObsProgressListener struct {
}
// Define the function for processing progress change events.
func (listener *ObsProgressListener) ProgressChanged(event *obs.ProgressEvent) {
    switch event.EventType {
    case obs.TransferStartedEvent:
        fmt.Printf("Transfer Started, ConsumedBytes: %d, TotalBytes %d.\n",
            event.ConsumedBytes, event.TotalBytes)
    case obs.TransferDataEvent:
        fmt.Printf("\rTransfer Data, ConsumedBytes: %d, TotalBytes %d, %d%%.\n",
            event.ConsumedBytes, event.TotalBytes, event.ConsumedBytes*100/event.TotalBytes)
    case obs.TransferCompletedEvent:
        fmt.Printf("\nTransfer Completed, ConsumedBytes: %d, TotalBytes %d.\n",
            event.ConsumedBytes, event.TotalBytes)
    case obs.TransferFailedEvent:
        fmt.Printf("\nTransfer Failed, ConsumedBytes: %d, TotalBytes %d.\n",
            event.ConsumedBytes, event.TotalBytes)
    default:
    }
}

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, err := obs.New(ak, sk, endPoint/*, obs.WithSecurityToken(securityToken)*/)

    input := &obs.UploadFileInput{}
    // Specify the bucket name.
    input.Bucket = "bucketname"
    // Specify the object (example/objectname as an example) to upload.
    input.Key = "objectname"
    // Specify your local file (/tmp/objectname as an example) to upload.
    input.UploadFile = "/tmp/objectname"
    // Specify whether to enable resumable upload (true as an example). The default value is false, indicating that resumable upload is disabled.
    input.EnableCheckpoint = true
    // Specify a part size, in bytes. This example sets each part to 9 MB.
    input.PartSize = 9 * 1024 * 1024
    // Specify the maximum number of parts that can be concurrently uploaded. 5 is used as an example.
    input.TaskNum = 5
    output, err := obsClient.UploadFile(input, obs.WithProgress(&ObsProgressListener{})
    if err == nil {
        // This method must be invoked.
        defer output.CloseCallbackBody()
        fmt.Printf("RequestId:%s\n", output.RequestId)
        fmt.Printf("ETag:%s\n", output.ETag)
        p := make([]byte, 1024)
        var readErr error
        var readCount int
        // Read the callback content.
        for {
            readCount, readErr = output.ReadCallbackBody(p)
            if readCount > 0 {
                fmt.Printf("%s", p[:readCount])
            }
            if readErr != nil {
                break
            }
        }
    } else if obsError, ok := err.(obs.ObsError); ok {
        fmt.Printf("Code:%s\n", obsError.Code)
        fmt.Printf("Message:%s\n", obsError.Message)
    }
}