サイズが大きいオブジェクトは、ネットワークが不安定であったり、Java プログラムが異常終了することがあるため、ダウンロードに失敗することがあります。その際は、オブジェクト全体を再度ダウンロードする必要があります。 オブジェクトは複数回試行してもダウンロードに失敗することがあります。 そのため、OSS は再開可能ダウンロードを提供しています。
再開可能ダウンロードは次のコードを実行します。
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>";
var downloadFilename = "<yourDownloadFilename>";
var checkpointDir = "<yourCheckpointDir>";
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
// Configure parameters with DownloadObjectRequest.
DownloadObjectRequest request = new DownloadObjectRequest(bucketName, objectName, downloadFilename)
{
// Specify the size of a part you want to download.
PartSize = 8 * 1024 * 1024,
// Specifies the number of concurrent download threads.
ParallelThreadCount = 3,
// Specify the checkpointDir to store the download progress information. ダウンロードが失敗した場合は、進行状況に基づいて続行できます。 checkpointDir ディレクトリが null の場合、再開可能アップロードは有効になりません。ダウンロードに失敗したオブジェクトはもう一度ダウンロードされます。
CheckpointDir = checkpointDir,
};
// Start 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 e) {
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
再開可能ダウンロードの詳細については、「OSS 開発者ガイド」内の「再開可能ダウンロード」をご参照ください。