Un bucket es un contenedor que se utiliza para almacenar objetos en Object Storage Service (OSS). Todos los objetos de OSS se guardan dentro de buckets. En este tema se describe cómo consultar la información de un bucket.
Notas de uso
En este tema se utiliza el endpoint público de la región China (Hangzhou). Si desea acceder a OSS mediante otros servicios de Alibaba Cloud ubicados en la misma región, utilice un endpoint interno. Para obtener más información sobre las regiones y los endpoints de OSS, consulte Regions and endpoints.
Aquí se crea una instancia de OSSClient con un endpoint de OSS. Si necesita crear una instancia de OSSClient mediante dominios personalizados o Security Token Service (STS), consulte Initialization (C# SDK V1).
Para consultar la información de un bucket, debe contar con el permiso
oss:GetBucketInfo. Para obtener más detalles, consulte Grant a custom policy.
Código de ejemplo
A continuación se muestra un ejemplo de código para consultar la información de un bucket. Los datos obtenidos incluyen la región, la fecha de creación y la lista de control de acceso (ACL) del bucket.
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);
}
Referencias
Para conocer más sobre la operación de API que permite consultar la información de un bucket, consulte GetBucketInfo.