断点续传上传是指将要上传的文件分成若干个分片(Part)分别上传,所有分片都上传完成后,将所有分片合并成完整的文件,完成整个文件的上传。

文件上传时,会在Checkpoint文件中记录当前上传的进度信息,如果上传过程中某一分片上传失败,再次上传时会从Checkpoint记录处继续上传,从而达到断点续传的效果。上传完成后,Checkpoint文件会被删除。

断点续传完整代码请参见GitHub

以下代码用于断点续传上传:

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
var objectName = "<yourObjectName>";
var localFilename = "<yourLocalFilename>";
string checkpointDir = "<yourCheckpointDir>";
// 创建OssClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // 通过UploadFileRequest设置多个参数。
    UploadObjectRequest request = new UploadObjectRequest(bucketName, objectName, localFilename)
    {
        // 指定上传的分片大小。
        PartSize = 8 * 1024 * 1024,
        // 指定并发线程数。
        ParallelThreadCount = 3,
        // checkpointDir保存断点续传的中间状态,用于失败后继续上传。如果checkpointDir为null,断点续传功能不会生效,每次失败后都会重新上传。
        CheckpointDir = checkpointDir,
    };
    // 断点续传上传。
    client.ResumableUploadObject(request);
    Console.WriteLine("Resumable upload 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);
}