Un bucket est un conteneur servant à stocker des objets dans Object Storage Service (OSS). Tous les objets OSS résident dans des buckets. Cette rubrique explique comment interroger les informations relatives à un bucket.
Notes d’utilisation
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, privilégiez un point de terminaison interne. Pour en savoir plus sur les régions et les points de terminaison OSS, consultez Régions et points de terminaison.
L’instance OSSClient présentée ici est créée via un point de terminaison OSS. Pour créer une instance OSSClient à l’aide de noms de domaine personnalisés ou du Security Token Service (STS), reportez-vous à la section Initialisation (C# SDK V1).
L’interrogation des informations d’un bucket requiert l’autorisation
oss:GetBucketInfo. Pour plus de détails, consultez Accorder une politique personnalisée.
Exemple de code
Le code ci-dessous illustre la récupération des informations d’un bucket, telles que sa région, sa date de création et sa liste de contrôle d’accès (ACL).
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.
var bucketName = "yourbucketname";
// 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
{
// Bucket information includes the region (Region or Location), creation date (CreationDate), owner (Owner), and ACL (Grants) of the bucket.
var bucketInfo = client.GetBucketInfo(bucketName);
Console.WriteLine("Get bucket:{0} Info succeeded ", bucketName);
// Query the region of the bucket.
Console.WriteLine("bucketInfo Location: {0}", bucketInfo.Bucket.Location);
// Query the creation date of the bucket.
Console.WriteLine("bucketInfo CreationDate: {0}", bucketInfo.Bucket.CreationDate);
// Query the disaster recovery type of the bucket.
Console.WriteLine("bucketInfo DataRedundancyType: {0}", bucketInfo.Bucket.DataRedundancyType);
// Query the ACL of the bucket.
Console.WriteLine("bucketInfo Grant: {0}", bucketInfo.Bucket.AccessControlList.Grant);
}
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);
}
Références
Pour plus d’informations sur l’opération API permettant d’interroger les informations d’un bucket, consultez GetBucketInfo.