L'instabilité du réseau ou d'autres exceptions peuvent empêcher le téléchargement d'un objet volumineux. Parfois, le téléchargement échoue même après plusieurs tentatives. Pour résoudre ce problème, Object Storage Service (OSS) propose la fonctionnalité de téléchargement avec reprise. OSS divise l'objet en plusieurs parties et les télécharge séparément. Une fois toutes les parties téléchargées, OSS les assemble pour reconstituer l'objet complet.
Notes
Cette rubrique utilise l'endpoint public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez Régions et endpoints.
Cette rubrique explique comment créer une instance OSSClient avec un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification avec des identifiants issus du Security Token Service (STS), consultez Initialisation (C# SDK V1).
Pour utiliser le téléchargement avec reprise, vous devez disposer de l'autorisation
oss:GetObject. Pour plus d'informations, consultez Accorder une politique personnalisée.
Exemple de code
L'exemple de code suivant montre comment effectuer un téléchargement avec reprise :
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);
}
Références
Pour plus d'informations sur l'opération API de téléchargement avec reprise, consultez GetObject.