A bucket is a container used to store objects in Object Storage Service (OSS). All objects in OSS are contained in buckets. This topic describes how to query information about a bucket.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints in OSS, see Regions and endpoints.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
To query information about a bucket, you must have the
oss:GetBucketInfo
permission. For more information, see Attach a custom policy to a RAM user.
Sample code
The following sample code provides an example on how to query information about a bucket, including the region, creation date, and access control list (ACL) of the 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";
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
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);
}
References
For more information about the API operation that you can call to query information about a bucket, see GetBucketInfo.