A bucket is a container that stores objects. This topic describes how to retrieve information about a bucket.
Notes
This topic uses the public endpoint of the China (Hangzhou) region as an example. If you want to access OSS from other Alibaba Cloud products in the same region, use an internal endpoint. For more information about the regions and endpoints that OSS supports, see Regions and endpoints.
This topic uses an OSS domain name to create an OSSClient instance as an example. If you want to create an OSSClient instance using a custom domain name or Security Token Service (STS), see Initialization (C# SDK V1).
To retrieve information about a bucket, you must have the
oss:GetBucketInfopermission. For more information, see Grant custom access policies to a RAM user.
Sample code
The following code retrieves information about a bucket, such as its region, creation date, and permissions.
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Set yourEndpoint to 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 = "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 set.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Set yourbucketname to the name of the bucket.
var bucketName = "yourbucketname";
// Set 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();
// Use Signature V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// The bucket information includes the region (Region or Location), creation date (CreationDate), owner (Owner), and permissions (Grants).
var bucketInfo = client.GetBucketInfo(bucketName);
Console.WriteLine("Get bucket:{0} Info succeeded ", bucketName);
// Get the region where the bucket is located.
Console.WriteLine("bucketInfo Location: {0}", bucketInfo.Bucket.Location);
// Get the creation date of the bucket.
Console.WriteLine("bucketInfo CreationDate: {0}", bucketInfo.Bucket.CreationDate);
// Get the data disaster recovery type of the bucket.
Console.WriteLine("bucketInfo DataRedundancyType: {0}", bucketInfo.Bucket.DataRedundancyType);
// Get the permissions 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);
}References
For more information about the API operation that you can call to retrieve bucket information, see GetBucketInfo.