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

Downloading an Object - Image Processing (SDK for Python)

Code Example 1: Creating a Signed URL for Download

This example creates a signed URL for image processing.

 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
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) 
try: 
    # Name of the bucket that stores the object
    bucket_name = 'originBucketName' 
    # Object name
    object_key = 'test.png' 
    query_params = {"x-image-process": "image/resize, w_100"} 
    # Create a signed URL for image processing.
    res = obs_client.createSignedUrl(method='GET', bucketName=bucket_name, objectKey=object_key, queryParams=query_params, 
                                    expires=3600) 
    print('signedUrl:', res.signedUrl) 
except Exception: 
    print(traceback.format_exc())

Code Example 2: Performing a Resumable Download

This example processes an image during resumable download.

 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
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) 
try: 
    bucket_name = "examplebucket" 
    # Object name
    object_key = "example.png" 
    # Specify the full path to which the object is downloaded. The full path contains the local file name.
    download_file = './example.png' 
    # Specify the number of parts that can be concurrently downloaded.
    task_num = 3 
    # Specify the part size.
    part_size = 5 * 1024 * 1024 
    # Enable the resumable download by setting enableCheckpoint to True.
    enable_checkpoint = True 
    # Specify the image processing parameter.
    image_process = "image/resize, w_100" 
    # Download the object using resumable download.
    resp = obs_client.downloadFile(bucket_name, object_key, download_file, part_size, task_num, imageProcess=image_process) 

    # If status code 2xx is returned, the API call succeeds. Otherwise, the API call fails.
    if resp.status < 300: 
        print('Download File Succeeded') 
        print('requestId:', resp.requestId) 
    else: 
        print('Download File Failed') 
        print('requestId:', resp.requestId) 
        print('errorCode:', resp.errorCode) 
        print('errorMessage:', resp.errorMessage) 
except Exception: 
    print('Download File Failed') 
    print(traceback.format_exc())