All Products
Search
Document Center

Object Storage Service:Obtain the region of a bucket (C# SDK V2)

Last Updated:Aug 06, 2025

This topic describes how to obtain the region of a bucket using OSS SDK for C# V2.

Notes

  • The sample code in this topic uses the China (Hangzhou) region as an example. The ID of this region is cn-hangzhou. By default, the public endpoint is used. If you want to access OSS from other Alibaba Cloud services in the same region, use the internal endpoint. For more information about the mappings between OSS regions and endpoints, see Regions and endpoints.

  • This topic provides an example of reading access credentials from environment variables. For more information about how to configure access credentials, see [Configure access credentials for .NET SDK 2.0].

  • To obtain the region of a bucket, you must have the oss:GetBucketLocation permission. For more information, see Grant custom permissions to a RAM user.

Sample code

You can use the following code to obtain the region of a bucket.

using OSS = AlibabaCloud.OSS.V2; // Create an alias for the Alibaba Cloud OSS SDK to simplify subsequent use.

var region = "cn-hangzhou"; // Required. Set the region in which the bucket is located. This topic uses China (Hangzhou) as an example. Set Region to cn-hangzhou.
var bucket = "your bucket name";  // Required. Set the name of the destination bucket. 
var endpoint = null as string;  // Optional. Specify the domain name used to access OSS. This topic uses China (Hangzhou) as an example. Set Endpoint to https://oss-cn-hangzhou.aliyuncs.com.

// Load the default configurations of the OSS SDK. These configurations automatically read credential information, such as an AccessKey, from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly specify that environment variables are used to obtain credentials for identity verification. Format: OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region of the bucket in the configuration.
cfg.Region = region;
// If an endpoint is specified, it overwrites the default endpoint.
if(endpoint != null)
{
    cfg.Endpoint = endpoint;
} 

// Create an OSS client instance using the configuration information.
using var client = new OSS.Client(cfg);

// Call the GetBucketLocationAsync method to obtain the region information of the specified bucket. 
var result = await client.GetBucketLocationAsync(new OSS.Models.GetBucketLocationRequest()
{
    Bucket = bucket
});

// Print the result. 
Console.WriteLine("GetBucketLocation done");  // Indicate that the operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}");  // HTTP status code
Console.WriteLine($"RequestId: {result.RequestId}");  // The request ID, which is used by Alibaba Cloud to troubleshoot issues.
Console.WriteLine("Response Headers:");  // Response header information
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));  // Traverse and print all response headers.
Console.WriteLine($"Location: {result.LocationConstraint}");  // Print the region to which the bucket belongs. 

References

For the complete sample code to obtain the region of a bucket, see GetBucketLocation.cs.