Tous les produits
Search
Centre de documentation

Object Storage Service:Téléchargement par plage (C# SDK V1)

Dernière mise à jour :Aug 18, 2026

Le téléchargement par plage permet de télécharger une plage d'octets spécifique d'un objet, plutôt que l'intégralité de celui-ci.

Notes

  • Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Pour 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 explique comment créer une instance OSSClient avec un point de terminaison OSS. Pour d'autres configurations, telles que l'utilisation d'un nom de domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), reportez-vous à la section Initialisation (C# SDK V1).

  • Pour effectuer un téléchargement par plage, vous devez disposer de l'autorisation oss:GetObject. Pour en savoir plus, consultez la page Accorder une stratégie personnalisée.

Exemple de code

L'exemple de code suivant montre comment télécharger une plage d'octets spécifique d'un objet.

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";
// Download the object to D:\\localpath. After the object is downloaded, the local file is named examplefile.txt. If a file with the same name already exists, the downloaded object overwrites the file. Otherwise, the downloaded object is saved in the path. 
// If you do not specify a path for the downloaded object, the downloaded object is saved to the path of the project to which the sample program belongs. 
var downloadFilename = "D:\\localpath\\examplefile.txt";
// 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
{
    var getObjectRequest = new GetObjectRequest(bucketName, objectName);
    // Set the range to byte 20 to byte 100. 
    getObjectRequest.SetRange(20, 100);
    // Start range download. You can use setRange in getObjectRequest to perform range download and resumable download. 
    var obj = client.GetObject(getObjectRequest);
    // Download data and write the data to the file. 
    using (var requestStream = obj.Content)
    {
        byte[] buf = new byte[1024];
        var fs = File.Open(downloadFilename, FileMode.OpenOrCreate);
        var len = 0;
        while ((len = requestStream.Read(buf, 0, 1024)) != 0)
        {
            fs.Write(buf, 0, len);
        }
        fs.Close();
    }
    Console.WriteLine("Get object succeeded");
}
catch (Exception ex)
{
    Console.WriteLine("Get object failed. {0}", ex.Message);
}

Le tableau ci-dessous décrit les paramètres de GetObjectRequest.

Paramètre

Description

Range

Plage d'octets à télécharger.

ModifiedSinceConstraint

Si l'heure spécifiée est antérieure à l'heure de modification réelle de l'objet, l'objet est renvoyé. Sinon, le code d'état HTTP 304 Not Modified est renvoyé.

UnmodifiedSinceConstraint

Si l'heure spécifiée est égale ou postérieure à l'heure de modification réelle de l'objet, l'objet est renvoyé. Sinon, le code d'état HTTP 412 Precondition Failed est renvoyé.

MatchingETagConstraints

Si la valeur ETag spécifiée correspond à la valeur ETag de l'objet, l'objet est renvoyé. Sinon, le code d'état HTTP 412 Precondition Failed est renvoyé.

NonmatchingEtagConstraints

Si la valeur ETag spécifiée ne correspond pas à la valeur ETag de l'objet, l'objet est renvoyé. Sinon, le code d'état HTTP 304 Not Modified est renvoyé.

ResponseHeaderOverrides

En-têtes de réponse personnalisés à inclure dans la réponse de téléchargement.

Références

  • Pour consulter l'exemple de code complet relatif au téléchargement par plage, rendez-vous sur GitHub.

  • Pour en savoir plus sur l'opération API utilisée pour le téléchargement par plage, consultez la section GetObject.