Downloading Objects - Partially

API Description

You can use this API to download data falling within a specific range when only partial data of an object is required. If the specified range is 0 to 1000, data at the 0th to the 1000th bytes, 1001 bytes in total, will be returned. If the specified range is invalid, data of the whole object will be returned.

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:
    from obs import GetObjectHeader
    headers = GetObjectHeader()
    # Set the start point and end point.
    headers.range = '0-1000'
    resp = obsClient.getObject('bucketname', 'objectname', loadStreamInMemory=True, headers=headers)
    if resp.status < 300:    
        print('requestId:', resp.requestId)
        # Obtain the object content.
        print('buffer:', resp.body.buffer)
    else:    
        print('errorCode:', resp.errorCode)    
        print('errorMessage:', resp.errorMessage)
except:
    import traceback
    print(traceback.format_exc())
  • If the specified range is invalid (because the start or end position is set to a negative integer or the range is larger than the object length), data of the whole object will be returned.
  • This download method also can be used to concurrently download parts of a large object. For details about the sample code, see concurrent_download_object_sample.