Updated on 2026-07-31 GMT+08:00

Obtaining the Upload Progress (SDK for Python)

You can query the upload progress when uploading an object in streaming, file-based, multipart, appendable, or resumable mode.

This example configures a callback function to obtain the object upload progress.

Sample code 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import traceback

from obs import ObsClient

# Obtain an AK/SK pair using environment variables (recommended) or import it in other ways. Using hard coding may result in leakage.
# Obtain an AK and 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")
# If you use a temporary AK/SK pair and a security token to access OBS, obtain them from environment variables.
# security_token = os.getenv("SecurityToken")
# Set server to the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
server = "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, you must specify security_token when creating an instance.
obs_client = ObsClient(access_key_id=ak, secret_access_key=sk, server=server) 

# Obtain the upload progress.
def callback(transferredAmount, totalAmount, totalSeconds): 
    # Obtain the average upload rate (KB/s).
    print(transferredAmount * 1.0 / totalSeconds / 1024) 
    # Obtain the upload progress in percentage.
    print(transferredAmount * 100.0 / totalAmount) 

try: 
    bucket_name = "examplebucket" 
    # Specify an object name (the name displayed after the file is uploaded to the bucket).
    object_key = "objectname" 
    # Specify the full path of the file to be uploaded, for example, aa/bb.txt.
    file_path = 'localfile' 
    # Perform the file-based upload.
    resp = obs_client.putFile(bucket_name, object_key, file_path, progressCallback=callback) 
    # If status code 2xx is returned, the API is called successfully. Otherwise, the API call fails.
    if resp.status < 300: 
        print('Put File Succeeded') 
        print('requestId:', resp.requestId) 
        print('etag:', resp.body.etag) 
        print('versionId:', resp.body.versionId) 
        print('storageClass:', resp.body.storageClass) 
    else: 
        print('Put File Failed') 
        print('requestId:', resp.requestId) 
        print('errorCode:', resp.errorCode) 
        print('errorMessage:', resp.errorMessage) 
except Exception: 
    print('Put File Failed') 
    print(traceback.format_exc())