Help Center> Object Storage Service> SDK Reference> Python> Objects-Related APIs> Uploading Objects - Resumable Transfer

Uploading Objects - Resumable Transfer

API Description

You can use this API to encapsulate and enhance multipart upload in response to an upload failure of large files due to unstable network or program breakdown. This API is used to divide the to-be-uploaded file into multiple parts and upload them separately. The upload result of each part is recorded in the checkpoint file in real time. The result indicating a successful upload is returned only when all parts are successfully uploaded. Otherwise, an exception is thrown to remind you of calling the API again for re-uploading.

Method Definition

ObsClient.uploadFile(bucketName, objectKey, uploadFile, partSize, taskNum, enableCheckpoint, checkpointFile, checkSum, metadata, progressCallback, headers)

Request Parameters

Field

Type

Optional or Mandatory

Description

bucketName

str

Mandatory

Bucket name

objectKey

str

Mandatory

Object name or the name of the uploaded file

uploadFile

str

Mandatory

Local file to be uploaded, for example, aa/bb.txt.

partSize

int

Optional

Part size, in bytes. The value ranges from 100 KB to 5 GB and defaults to 9 MB.

taskNum

int

Optional

Maximum number of parts that can be concurrently uploaded. The default value is 1.

enableCheckpoint

bool

Optional

Whether to enable the resumable upload mode. The default value is False, indicating that this mode is disabled.

checkpointFile

str

Optional

File used to record the upload progress. This parameter is effective only in the resumable upload mode. If the value of this parameter is empty, the file will be in the same directory as the local file to be uploaded.

checkSum

bool

Optional

Whether to verify the file to upload. If this parameter is enabled, the system verifies the file to upload before each task restarts, to check whether the file is the one used during task initialization. The default value is False.

metadata

dict

Optional

Customized metadata of the object

progressCallback

callable

Optional

Callback function for obtaining the upload progress

NOTE:

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

headers

UploadFileHeader

Optional

Additional header of the request for uploading an object

Returned Results

Type

Description

GetResult

SDK common result object

GetResult.body Type

Description

CompleteMultipartUploadResponse

Response result of the request for combining parts

Sample Code

uploadFile = 'localfile'
taskNum = 5
partSize = 10 * 1024 * 1024
enableCheckpoint = True
try:
    resp = obsClient.uploadFile('bucketname', 'objectkey', uploadFile, partSize, taskNum, enableCheckpoint)
    if resp.status < 300:    
        print('requestId:', resp.requestId)    
    else:    
        print('errorCode:', resp.errorCode)    
        print('errorMessage:', resp.errorMessage)
except:
    import traceback
    print(traceback.format_exc())