Tous les produits
Search
Centre de documentation

Object Storage Service:Téléchargement avec reprise (OSS SDK pour C# 1.0)

Dernière mise à jour :Aug 18, 2026

Lorsque vous téléchargez un objet vers Object Storage Service (OSS) via un téléchargement avec reprise, vous pouvez spécifier un répertoire pour le fichier de point de contrôle qui enregistre la progression du téléchargement. Si le téléchargement d'un objet échoue en raison d'une exception réseau ou d'une erreur de programme, la tâche de téléchargement reprend à partir de la position enregistrée dans le fichier de point de contrôle.

Le fichier de point de contrôle enregistre la progression du téléchargement en cours. Si une partie échoue lors du téléchargement, le prochain téléchargement reprend à partir de la position enregistrée dans ce fichier. Le fichier de point de contrôle est supprimé une fois la tâche de téléchargement avec reprise terminée.

Notes

  • Cette rubrique utilise le point de terminaison 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 point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez Régions et points de terminaison.

  • Cette rubrique illustre la création d'une instance OSSClient avec un point de terminaison 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 (SDK C# V1).

  • Pour utiliser le téléchargement avec reprise, vous devez disposer de l'autorisation oss:PutObject. 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;

// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located 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 configured. 
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket. 
var bucketName = "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
var objectName = "exampledir/exampleobject.txt";
// Specify the full path of the local file that you want to upload. Example: D:\\localpath\\examplefile.txt. 
// By default, if you specify only the name of the local file such as examplefile.txt without specifying the local path, the local file is uploaded from the path of the project to which the sample program belongs. 
var localFilename = "D:\\localpath\\examplefile.txt";
// Specify the checkpoint file. The checkpoint file stores information about the upload progress. 
string checkpointDir = "yourCheckpointDir";
// Specify the region in which the bucket is located. For example, if the bucket is located 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 based on your requirements.
var conf = new ClientConfiguration();

// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;

// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
    // Configure parameters by using UploadFileRequest. 
    UploadObjectRequest request = new UploadObjectRequest(bucketName, objectName, localFilename)
    {
        // Specify the size of each part. 
        PartSize = 8 * 1024 * 1024,
        // Specify the number of concurrent threads. 
        ParallelThreadCount = 3,
        // Specify the checkpointDir parameter to store the state of the resumable upload task, which is used to resume the upload task if the upload task fails. 
        // If you set checkpointDir to null, resumable upload does not take effect and the object is re-uploaded if the object fails to be uploaded. 
        CheckpointDir = checkpointDir,
    };
    // Start resumable upload. 
    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);
}

Références

Pour consulter l'exemple de code complet utilisé pour effectuer un téléchargement avec reprise, rendez-vous sur GitHub.