Deze pagina is nog niet beschikbaar in uw eigen taal. We werken er hard aan om meer taalversies toe te voegen. Bedankt voor uw steun.

On this page

Show all

Help Center/ Object Storage Service/ SDK Reference/ Python/ Object-Related APIs (SDK for Python)/ Downloading an Object - Obtaining the Download Progress (SDK for Python)

Downloading an Object - Obtaining the Download Progress (SDK for Python)

Updated on 2024-11-26 GMT+08:00

You can obtain the download progress when downloading an object in binary, streaming, file-based, or resumable mode.

This example returns the object download 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
from obs import ObsClient
import os
import traceback

# Obtain an AK and SK pair using environment variables or import the AK and SK pair 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/eu/usermanual-ca/ca_01_0003.html.
ak = os.getenv("AccessKeyID")
sk = os.getenv("SecretAccessKey")
# (Optional) If you use a temporary AK and 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. EU-Dublin is used here as an example. Replace it with the one in use.
server = "https://obs.eu-west-101.myhuaweicloud.eu" 

# Create an obsClient instance.
# If you use a temporary AK and SK pair and a security token to access OBS, you must specify security_token when creating an instance.
obsClient = ObsClient(access_key_id=ak, secret_access_key=sk, server=server)

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

try:
    bucketName="examplebucket"
    objectKey="objectname"
    # Download an object.
    resp = obsClient.getObject(bucketName=bucketName,objectKey=objectKey, progressCallback=callback)
    # If status code 2xx is returned, the API is called successfully. Otherwise, the API call fails.
    if resp.status < 300:
        print('Get Object Succeeded')
        print('requestId:', resp.requestId)
        # Read the object content.
        while True:
            chunk = resp.body.response.read(65536)
            if not chunk:
                break
            print(chunk)
        resp.body.response.close()
    else:
        print('Get Object Failed')
        print('requestId:', resp.requestId)
        print('errorCode:', resp.errorCode)
        print('errorMessage:', resp.errorMessage)
except:
    print('Get Object Failed')
    print(traceback.format_exc())
Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback