Tous les produits
Search
Centre de documentation

Object Storage Service:Lister des objets (SDK OSS pour C# V2)

Dernière mise à jour :Aug 18, 2026

Cette rubrique explique comment utiliser le SDK OSS pour C# afin de lister tous les objets d'un bucket spécifié.

Précautions

  • L'exemple de code de cette rubrique utilise la région Chine (Hangzhou) (cn-hangzhou) à titre d'illustration. Par défaut, l'endpoint public est utilisé. Pour accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez l'endpoint interne. Pour plus d'informations sur les régions et les endpoints pris en charge par OSS, consultez la page Régions et endpoints OSS.

  • Cette rubrique montre comment lire les identifiants d'accès à partir de variables d'environnement. Pour savoir comment configurer les identifiants d'accès, consultez la section Configurer les identifiants d'accès.

  • Pour lister des objets, vous devez disposer de l'autorisation oss:ListObjects. Pour plus d'informations, consultez la page Accorder des autorisations personnalisées à un utilisateur RAM.

Exemple de code

Utilisez le code suivant pour lister tous les objets du bucket spécifié.

using OSS = AlibabaCloud.OSS.V2;  // Create an alias for the Alibaba Cloud OSS SDK to simplify subsequent use.

var region = "cn-hangzhou";  // Required. 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.
var endpoint = null as string;  // Optional. Specify the domain name used to access the OSS service. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var bucket = "your bucket name";  // Required. The name of the destination bucket.

// Load the default configurations of the OSS SDK. The configurations automatically read credential information (such as AccessKey) from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly set the use of environment variables to obtain credentials for identity verification (format: OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET).
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region of the bucket in the configuration.
cfg.Region = region;   
// If an endpoint is specified, it overwrites the default endpoint. 
if(endpoint != null) 
{
    cfg.Endpoint = endpoint;
} 

// Create an OSS client instance using the configuration information.
using var client = new OSS.Client(cfg); 

// Call the ListObjectsV2Paginator method to obtain all objects in the destination bucket.
var paginator = client.ListObjectsV2Paginator(new OSS.Models.ListObjectsV2Request()
{
    Bucket = bucket
});

//  Print the information of all objects in the bucket.
Console.WriteLine("Objects:");
await foreach (var page in paginator.IterPageAsync())  // Asynchronously traverse each page of results.
{
    // Traverse each object on the current page.
    foreach (var content in page.Contents ?? [])
    {
        // Print the object information: key, size (in bytes), and last modified time.
        Console.WriteLine($"Object:{content.Key}, {content.Size}, {content.LastModified}");
    }
}

Références

Pour consulter l'exemple de code complet, rendez-vous sur la page ListObjects.cs.