Cette rubrique explique comment répertorier tous les buckets de votre compte. Les buckets sont affichés par ordre alphabétique.
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 montre comment créer une instance
OSSClientavec un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Initialisation (C# SDK V1).Pour répertorier les buckets, vous devez disposer de l'autorisation
oss:ListBuckets. Pour plus d'informations, consultez Accorder une politique personnalisée.
Exemple de code
Le code suivant montre comment répertorier tous les buckets de votre compte.
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify 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 = "https://oss-cn-hangzhou.aliyuncs.com";
// 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 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);
// List all buckets in your account.
try
{
var buckets = client.ListBuckets();
Console.WriteLine("List bucket succeeded");
foreach (var bucket in buckets)
{
Console.WriteLine("Bucket name: {0}, Location: {1}, Owner: {2}", bucket.Name, bucket.Location, bucket.Owner);
}
}
catch (Exception ex)
{
Console.WriteLine("List bucket failed. {0}", ex.Message);
}
Références
Pour obtenir l'exemple de code complet permettant de répertorier les buckets, consultez l'exemple GitHub.
Pour plus d'informations sur l'opération API de répertoriation des buckets, consultez ListBuckets (GetService).