Um bucket é um contêiner que armazena objetos no Object Storage Service (OSS). O OSS armazena todos os objetos em buckets. Este tópico descreve como consultar as informações de um bucket.
Observações de uso
Este tópico usa o endpoint público da região China (Hangzhou). Para acessar o OSS por meio de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para mais detalhes sobre as regiões e os endpoints do OSS, consulte Regions and endpoints.
O exemplo cria uma instância OSSClient usando um endpoint do OSS. Para criar uma instância OSSClient com nomes de domínio personalizados ou com o Security Token Service (STS), consulte Initialization (C# SDK V1).
Para consultar as informações de um bucket, você precisa da permissão
oss:GetBucketInfo. Para saber mais, consulte Grant a custom policy.
Exemplos de código
O exemplo de código a seguir mostra como consultar as informações de um bucket, incluindo sua região, data de criação e lista de controle de acesso (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);
}
Referências
Para mais detalhes sobre a operação de API usada para consultar as informações de um bucket, consulte GetBucketInfo.