You may fail to download a large object if the network is unstable or other exceptions occur. In some cases, you may fail to download the object even after multiple attempts. To resolve this issue, Object Storage Service (OSS) provides the resumable download feature. In resumable download, OSS splits an object into multiple parts and downloads each part separately. After all parts are downloaded, OSS combines the parts into a complete object.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
To use resumable download, you must have the
oss:GetObjectpermission. For more information, see Attach a custom policy to a RAM user.
Sample code
The following code provides an example of how to perform a resumable download:
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the full path of the object. The full path cannot contain the bucket name. Example: exampledir/exampleobject.txt.
var objectName = "exampleobject.txt";
// Download the object to a local file named examplefile.txt and save it to the specified local path (D:\\localpath). If the local file exists, it is overwritten. If the local file does not exist, it is created.
// If you do not specify a local path, the downloaded file is saved to the local path of the project where the sample program resides.
var downloadFilename = "D:\\localpath\\examplefile.txt";
// Specify the full path of the breakpoint record file. Example: D:\\localpath\\examplefile.txt.dcp.
// You need to specify the breakpoint record file only when the download is interrupted and a breakpoint record file is generated. After the download is complete, this file is deleted.
var checkpointDir = "D:\\localpath\\examplefile.txt.dcp";
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();
// Set the signature version to V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Set multiple parameters in DownloadObjectRequest.
DownloadObjectRequest request = new DownloadObjectRequest(bucketName, objectName, downloadFilename)
{
// Specify the size of each shard for download in bytes.
PartSize = 8 * 1024 * 1024,
// Specify the number of concurrent threads.
ParallelThreadCount = 3,
// checkpointDir is used to save the progress of the resumable download. If a shard fails to download, the download resumes from the recorded breakpoint when you try again. If checkpointDir is set to null, the resumable download feature is disabled. The download restarts from the beginning after each failure.
CheckpointDir = checkpointDir,
};
// Perform a resumable download.
client.ResumableDownloadObject(request);
Console.WriteLine("Resumable download object:{0} succeeded", objectName);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}References
For more information about the resumable download API operation, see GetObject.