Downloading Objects - Streaming

API Description

You can use this API to download a specified file in streaming mode.

Method Definition

ObsClient.getObject(bucketName, objectKey, downloadPath, getObjectRequest, headers, loadStreamInMemory, progressCallback)

Request Parameters

Field

Type

Optional or Mandatory

Description

bucketName

str

Mandatory

Bucket name

objectKey

str

Mandatory

Name of the object to be downloaded

downloadPath

str

Optional

The target path to which the object is downloaded, including the file name, for example, aa/bb.txt.

getObjectRequest

GetObjectRequest

Optional

Additional parameter of the request for downloading an object

headers

GetObjectHeader

Optional

Additional header of the request for downloading objects

loadStreamInMemory

bool

Optional

Whether to load the data stream of the object to the memory. The default value is False. If the value is True, the downloadPath parameter will be ineffective and the obtained data stream will be directly loaded to the memory.

progressCallback

callable

Optional

Callback function for obtaining the download progress

NOTE:

This function contains the following parameters in sequence: number of downloaded bytes, total number of bytes, and used time (unit: second). For details about the sample code, see Downloading an Object – Obtaining the Download Progress.

Returned Results

Type

Description

GetResult

SDK common result object

GetResult.body Type

Description

ObjectStream

Response result of the request for downloading an object

Sample Code

try:
    resp = obsClient.getObject('bucketname', 'objectname', loadStreamInMemory=False) 
          
    if resp.status < 300: 
        print('requestId:', resp.requestId)
        # Obtain the object content.
        while True:
            chunk = resp.body.response.read(65536)
            if not chunk:
                break
            print(chunk)
        resp.body.response.close()
    else: 
        print('errorCode:', resp.errorCode) 
        print('errorMessage:', resp.errorMessage)
except:
    import traceback
    print(traceback.format_exc())