Resumable download lets you reliably download large objects by splitting them into parts, downloading each part separately, and combining them into a complete object. If a download is interrupted, OSS resumes from the last recorded checkpoint instead of restarting from the beginning.
When to use resumable download
Use ResumableDownloadObject when:
The network is unstable or the download may be interrupted by exceptions.
A full restart on failure is unacceptable for large objects.
Prerequisites
Before you begin, make sure you have:
An OSS bucket with at least one object to download
The
oss:GetObjectpermission on the target object — see Attach a custom policy to a RAM userThe OSS C++ SDK installed and initialized
If your application runs in the same region as your OSS bucket, use the internal endpoint. For endpoint details, see Regions and endpoints.
How it works
ResumableDownloadObjectsplits the object into parts based on the configured part size.Parts are downloaded concurrently using the configured number of threads.
If
checkpointDiris set, OSS writes progress to a checkpoint file during the download. On failure, the next attempt reads the checkpoint file and skips already-downloaded parts.After all parts are downloaded successfully, OSS combines them into the final file. The checkpoint file is then deleted automatically.
Setting checkpointDir is what enables resumable behavior. Without it, a failed download starts over from the beginning.
Parameters
ResumableDownloadObject accepts a DownloadObjectRequest object with the following parameters:
| Parameter | Description | Required | Default |
|---|---|---|---|
bucket | The name of the bucket | Yes | — |
key | The full path of the object in OSS, excluding the bucket name. For example, exampledir/exampleobject.txt. | Yes | — |
filePath | The full local path for the downloaded file. If omitted, the file is saved using the object name in the current working directory. | No | Object name |
partSize | The size of each part. Must be between 100 KB and 5 GB. | No | 8 MB |
threadNum | The number of parts to download concurrently | No | 3 |
checkpointDir | The directory for storing the checkpoint file. Set this parameter to enable resumable download. If an interrupted download left a checkpoint file in this directory, the next run resumes from where it stopped. The checkpoint file is deleted after a successful download. | No | Same directory as filePath |
How to set each parameter:
bucket,key,filePath,threadNum, andcheckpointDircan be set in theDownloadObjectRequestconstructor.partSizeis set usingsetPartSize.threadNumandcheckpointDircan also be set usingsetThreadNumandsetCheckpointDir.
Download an object with resumable download
The following example downloads exampledir/exampleobject.txt from examplebucket to a local file, with checkpoint-based resumability enabled.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Set the endpoint for the region where your bucket is located.
For example, https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou). */
std::string Endpoint = "yourEndpoint";
/* Set the region ID for your bucket.
For example, cn-hangzhou for China (Hangzhou). */
std::string Region = "yourRegion";
/* The name of the bucket. */
std::string BucketName = "examplebucket";
/* The full path of the object in OSS, excluding the bucket name. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* The full local path for the downloaded file.
If the file exists, it is overwritten. If it does not exist, it is created. */
std::string DownloadFilePath = "D:\\localpath\\examplefile.txt";
/* The directory for the checkpoint file.
This directory must already exist. The checkpoint file records progress and
is deleted automatically after the download completes. */
std::string CheckpointFilePath = "D:\\localpath";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Load credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Perform a resumable download. */
DownloadObjectRequest request(BucketName, ObjectName, DownloadFilePath, CheckpointFilePath);
auto outcome = client.ResumableDownloadObject(request);
if (!outcome.isSuccess()) {
std::cout << "ResumableDownloadObject fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}Replace the following placeholders:
| Placeholder | Description | Example |
|---|---|---|
yourEndpoint | Endpoint for the region where your bucket is located | https://oss-cn-hangzhou.aliyuncs.com |
yourRegion | Region ID for your bucket | cn-hangzhou |
examplebucket | Your bucket name | — |
exampledir/exampleobject.txt | Full object path in OSS | — |
D:\\localpath\\examplefile.txt | Local destination path | — |
D:\\localpath | Directory for the checkpoint file | — |
What's next
For the complete sample code, see GitHub sample.
For the underlying API operation, see GetObject.
To create an OSSClient instance using custom domain names or Security Token Service (STS), see Create an OSSClient instance.