Multipart Upload Overview

To upload a large file, multipart upload is recommended. Multipart upload is applicable to many scenarios, when:

  • files to be uploaded are larger than 100 MB.
  • the network condition is poor. Connection to the OBS server is constantly down.
  • sizes of files to be uploaded are uncertain.

Multipart upload consists of three steps:

  1. Initialize a multipart upload (ObsClient.initiateMultipartUpload).
  2. Upload parts one by one or concurrently (ObsClient.uploadPart).
  3. Combine parts (ObsClient.completeMultipartUpload) or abort the multipart upload (ObsClient.abortMultipartUpload).

The following code shows how to perform multipart upload:

# Import the module.
from obs import ObsClient

# Create an instance of ObsClient.
obsClient = ObsClient(
    access_key_id='*** Provide your Access Key ***',    
    secret_access_key='*** Provide your Secret Key ***',    
    server='https://your-endpoint'
)

try:
    uploadId = None
    etag1 = None
    etag2 = None

    # Initialize a multipart upload.
    resp = obsClient.initiateMultipartUpload('bucketname', 'objectkey', contentType='text/plain') 

    if resp.status < 300: 
        print('requestId:', resp.requestId) 
        print('bucketName:', resp.body.bucketName) 
        print('objectKey:', resp.body.objectKey) 
        print('uploadId:', resp.body.uploadId) 
        uploadId = resp.body.uploadId
    else: 
        print('errorCode:', resp.errorCode) 
        print('errorMessage:', resp.errorMessage)

    # Upload a part.
    resp = obsClient.uploadPart('bucketname', 'objectkey', 'partNumber1', uploadId, 'localFilePath', isFile=True)
    if resp.status < 300: 
        print('requestId:', resp.requestId) 
        print('etag:', resp.body.etag) 
        etag1 = resp.body.etag
    else: 
        print('errorCode:', resp.errorCode) 
        print('errorMessage:', resp.errorMessage)
    resp = obsClient.uploadPart('bucketname', 'objectkey', 'partNumber2', uploadId, 'localFilePath', isFile=True)
    if resp.status < 300: 
        print('requestId:', resp.requestId) 
        print('etag:', resp.body.etag) 
        etag2 = resp.body.etag
    else: 
        print('errorCode:', resp.errorCode) 
        print('errorMessage:', resp.errorMessage)

    # Combine parts.
    from obs import CompleteMultipartUploadRequest, CompletePart 
      
    part1 = CompletePart(partNum='partNumber1', etag=etag1) 
    part2 = CompletePart(partNum='partNumber2', etag=etag2) 
      
    completeMultipartUploadRequest = CompleteMultipartUploadRequest(parts=[part1, part2]) 
      
    resp = obsClient.completeMultipartUpload('bucketname', 'objectkey', uploadId, completeMultipartUploadRequest) 
        
    if resp.status < 300: 
        print('requestId:', resp.requestId) 
        print('etag:', resp.body.etag) 
        print('bucket:', resp.body.bucket) 
        print('key:', resp.body.key) 
        print('location:', resp.body.location) 
        print('versionId:', resp.body.versionId) 
    else: 
        print('errorCode:', resp.errorCode) 
        print('errorMessage:', resp.errorMessage)
except:
    import traceback
    print(traceback.format_exc())

# Close ObsClient.
obsClient.close()

For details about other operations of multipart upload, see: