Downloading Objects - File-Based
API Description
You can use this API to download an object in a specified bucket as a file.
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 |
Object name or the name of the file 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', downloadPath='localfile')
if resp.status < 300:
print('requestId:', resp.requestId)
print('url:', resp.body.url)
else:
print('errorCode:', resp.errorCode)
print('errorMessage:', resp.errorMessage)
except:
import traceback
print(traceback.format_exc())
- Use the downloadPath to set the save path for the file to be downloaded.
- When loadStreamInMemory is set to True, downloadPath is ineffective.
If the value of downloadPath is a file path instead of a folder path, the object is downloaded as the file.
Last Article: Downloading Objects - Streaming
Next Article: Downloading Objects - Partially
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.