All Products
Search
Document Center

Query bucket information

Last Updated: Aug 20, 2020

A bucket is a container for objects stored in OSS. Every object is contained in a bucket. This topic describes how to query information about a bucket.

The following code provides an example on how to query the information about a bucket, including the region, creation date, and ACL of the bucket.

using Aliyun.OSS;
using Aliyun.OSS.Common;
// The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
var endpoint = "<https://oss-cn-hangzhou.aliyuncs.com>";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";

// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // Information about a bucket 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 time of the bucket.
    Console.WriteLine("bucketInfo CreationDate: {0}", bucketInfo.Bucket.CreationDate);
    // Query the type of disaster recovery for a bucket.
    Console.WriteLine("bucketInfo DataRedundancyType: {0}", bucketInfo.Bucket.DataRedundancyType);
    // Query the ACL information 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);
}