Downloading Objects - Binary
API Description
You can use this API to download a specified file in binary 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 |
Optional |
Additional parameter of the request for downloading an object |
|
|
headers |
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 |
|---|---|
|
SDK common result object |
|
GetResult.body Type |
Description |
|---|---|
|
Response result of the request for downloading an object |
Sample Code
try:
resp = obsClient.getObject('bucketname', 'objectname', loadStreamInMemory=True)
if resp.status < 300:
print('requestId:', resp.requestId)
# Obtain the object content.
print('buffer:', resp.body.buffer)
print('size:', resp.body.size)
else:
print('errorCode:', resp.errorCode)
print('errorMessage:', resp.errorMessage)
except:
import traceback
print(traceback.format_exc())
In a binary download, if loadStreamInMemory is set to True, the object content is contained in the body.buffer field in returned result.
Last Article: Object Download Overview
Next Article: Downloading Objects - Streaming
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.