All Products
Search
Document Center

Query the regions of buckets

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 the region of a bucket.

The following code provides an example on how to query the region or location of a 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
{
    // Query the region of the bucket.
    var result = client.GetBucketLocation(bucketName);
    Console.WriteLine("Get bucket:{0} Info succeeded ", bucketName);
    Console.WriteLine("bucket Location: {0}", result.Location);
   
}
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);
}