Before you upload an object to Object Storage Service (OSS) by using resumable upload, you can specify a directory for the checkpoint file that stores resumable upload records. If an object fails to upload due to a network exception or program error, the upload task is resumed from the position recorded in the checkpoint file to upload the remaining data.

Notice
  • Multiple threads are used in resumable upload. Therefore, you do not need to encapsulate multiple upload threads when you call the resumable upload method. Otherwise, data may be transmitted repeatedly.
  • We recommend that you increase the size of each part when the network connection is stable and decrease the size of each part when the network connection is unstable.

Sample code

The following code provides an example on how to perform resumable upload by using multipart upload:

# -*- coding: utf-8 -*-
import oss2
# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the bucket name. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# Set yourObjectName to the full path of the object. The full path cannot contain the bucket name. Example: exampledir/exampleobject.txt. 
# Set yourLocalFile to the full path of the local file. Example: D:\\localpath\\examplefile.txt. By default, if you do not specify the full path of the local file, the local file is uploaded from the path of the project to which the sample program belongs. 
oss2.resumable_upload(bucket, 'exampledir/exampleobject.txt', 'D:\\localpath\\examplefile.txt')
# If you do not specify a directory by using the store parameter, the py-oss-upload directory is created in the HOME directory to store the checkpoint information. 

# OSS SDK for Python V2.1.0 and later support the configuration of optional parameters in resumable upload. The following code provides an example on how to configure optional parameters in resumable upload: 
# import sys
# # If the size of the data to upload cannot be determined, the total_bytes parameter is set to None. 
# def percentage(consumed_bytes, total_bytes):
#     if total_bytes:
#         rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
#         print('\r{0}% '.format(rate), end='')
#         sys.stdout.flush()
# If you use the store parameter to specify a directory, the checkpoint information is stored in the specified directory. If you use the num_threads parameter to specify the number of concurrent upload threads, make sure that the oss2.defaults.connection_pool_size value is greater than or equal to the number of concurrent upload threads. The default number of concurrent upload threads is 1. 
# oss2.resumable_upload(bucket, '<yourObjectName>', '<yourLocalFile>',
#                       store=oss2.ResumableStore(root='/tmp'),
#                       # Specify that multipart upload is used when the object size is greater than or equal to the value of the multipart_threshold parameter. The default value of the parameter is 10 MB. 
#                       multipart_threshold=100*1024,
#                       # Specify the size of each part. Unit: bytes. Valid values: 100 KB to 5 GB. Default value: 100 KB. 
#                       part_size=100*1024,
#                       # Configure the function used to indicate the progress of the resumable upload task by implementing upload callback. 
#                       progress_callback=percentage,
#                       # If you use the num_threads parameter to specify the number of concurrent upload threads, make sure that the oss2.defaults.connection_pool_size value is greater than or equal to the number of concurrent upload threads. The default number of concurrent upload threads is 1. 
#                       num_threads=4)

References

For the complete sample code that is used to perform resumable upload, visit GitHub.