When downloading large objects, network fluctuations and similar issues may result in download failures. The TOS C++ SDK provides a retry mechanism to enhance the stability of object downloads; however, it is possible that, even after multiple retries, the download may still not be completed. To address the aforementioned situation, the TOS C++ SDK provides a resumable download feature, utilizing a local CheckPoint mechanism to record successfully downloaded file segments. If a download is interrupted due to network anomalies or machine failures, this interface can be invoked again to resume the download.
Resumable download divides the target object into multiple segments and supports concurrent downloading. Once all segments have been downloaded, they are merged into a complete file. You can configure the chunk size for resumable downloads, the number of threads for downloading chunks, client-side download speed limits, event callback functions, etc. It is also possible to cancel the task during the execution of a resumable download task.
Precautions
- Before downloading an object, you must have the tos:GetObject permission. For detailed instructions, please refer to the Permission configuration guide.
- For buckets with versioning enabled, you must have the tos:GetObjectVersion permission to download a specified version of an object. For detailed instructions, please refer to the Permission configuration guide.
- If an application downloads the same object in large quantities simultaneously, your access speed will be restricted by TOS bandwidth and regional limitations. It is recommended that you utilize CDN products to enhance performance while simultaneously reducing your costs. For detailed information on accessing TOS via CDN, please refer to Using CDN to accelerate access to TOS Resources.
- The SDK records the download status information in the Checkpoint file, so the program must have write permissions for the Checkpoint file.
- When performing a resumable download, the progress information of the file is recorded in the Checkpoint file. If a segment fails to download during the process, the subsequent download will resume from the point recorded in the Checkpoint file, thereby achieving the effect of download resumption. After the download is complete, the Checkpoint file will be deleted.
- If, during the download process, the file's ETag changes or any part is missing or modified, the file will be re-downloaded.
Sample code
Resumable download
The following code is used to perform a resumable download of the object exampledir/exampleobject.txt from the bucket examplebucket to a local file, and to retry the download in case of failure. If a network timeout error occurs during the download process, you can call downloadFile with the same parameters to resume the download from the breakpoint and re-enter the process.
Configure progress bar
The following code configures the resumable download progress bar functionality.
Handle event callbacks
During the invocation of the downloadFile API, events such as temporary file creation, file segment download, and temporary file renaming are triggered. You may pass in a custom interface to monitor these download-related events and implement your own business logic.
The following code is used to customize the callback function for resumable downloads.
Manage client-side rate limiting
The following code is used to set the speed limit for a resumable download client.
Pause or cancel resumable download
The downloadFile interface allows you to proactively pause or cancel a resumable transfer task. Pausing a resumable download task will stop the download. When downloadFile is invoked again, the download can resume from the position where it was previously paused.
The following code demonstrates how to pause or cancel a resumable transfer.