This topic describes how to use the OSS SDK for C# to obtain information about a specified bucket, such as its access tracking status, region, creation date, owner ID, storage class, public endpoint, internal same-region endpoint, cross-region replication status, and versioning status. You can use this information to perform the corresponding operations.
Precautions
The sample code in this topic uses the China (Hangzhou) region (ID:
cn-hangzhou) as an example. The public endpoint is used by default. If you want to access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the regions and endpoints that OSS supports, see OSS regions and endpoints.To obtain bucket information, you must have the
oss:GetBucketInfopermission. For more information, see Grant custom permissions to a RAM user.
Sample code
You can use the following sample code to obtain bucket information.
using OSS = AlibabaCloud.OSS.V2; // Create an alias for Alibaba Cloud OSS SDK to simplify subsequent use.
var region = "cn-hangzhou"; // Required. Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
var bucket = "your bucket name"; // Required. Specify the name of the destination bucket.
var endpoint = null as string; // Optional. Specify the endpoint used to access OSS. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
// Load the default configurations of OSS SDK. The configurations automatically read credential information (such as an AccessKey pair) from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly configure the SDK to obtain credentials from environment variables for identity verification. The format is OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Specify the region of the bucket in the configurations.
cfg.Region = region;
// If an endpoint is specified, it overwrites the default endpoint.
if(endpoint != null)
{
cfg.Endpoint = endpoint;
}
// Create an OSS client instance based on the configurations.
using var client = new OSS.Client(cfg);
// Call the GetBucketInfoAsync method to obtain the basic information about the specified bucket.
var result = await client.GetBucketInfoAsync(new OSS.Models.GetBucketInfoRequest()
{
Bucket = bucket
});
// Print the result.
Console.WriteLine("GetBucketInfo done"); // A message indicating that the operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}"); // The HTTP status code.
Console.WriteLine($"RequestId: {result.RequestId}"); // The request ID, which is used for troubleshooting in Alibaba Cloud.
Console.WriteLine("Response Headers:"); // The response headers.
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value)); // Traverse and print all response headers.
Console.WriteLine($"Location: {result.BucketInfo!.Location}"); // Print the region of the bucket.
Console.WriteLine($"StorageClass: {result.BucketInfo!.StorageClass}"); // Print the storage class of the bucket.
Console.WriteLine($"IntranetEndpoint: {result.BucketInfo!.IntranetEndpoint}"); // Print the internal endpoint of the bucket.
Console.WriteLine($"ExtranetEndpoint: {result.BucketInfo!.ExtranetEndpoint}"); // Print the public endpoint of the bucket.List of common bucket information
Parameter | Description |
BucketInfo.Name | The name of the bucket. |
BucketInfo.AccessMonitor | The access tracking status of the bucket. |
BucketInfo.Location | The region where the bucket is located. |
BucketInfo.CreationDate | The creation date of the bucket. |
BucketInfo.ExtranetEndpoint | The public endpoint of the bucket. |
BucketInfo.IntranetEndpoint | The internal endpoint that is used to access the bucket from an ECS instance in the same region. |
BucketInfo.Owner | This parameter includes the following subparameters: BucketInfo.Owner.ID: The user ID of the bucket owner. |
BucketInfo.StorageClass | The storage class of the bucket. |
BucketInfo.Versioning | The versioning status of the bucket. |
BucketInfo.CrossRegionReplication | The cross-region replication status of the bucket. |
References
For more information about buckets, see Buckets.
For the complete sample code that is used to obtain bucket information, see GetBucketInfo.cs.