All Products
Search
Document Center

Object Storage Service:Query the region of a bucket (Go SDK V2)

Last Updated:Aug 02, 2025

This topic describes how to query the region of a bucket.

Usage notes

  • The sample code in this topic uses the region ID cn-hangzhou for the China (Hangzhou) region. By default, resources in a bucket are accessed using a public endpoint. To access bucket resources 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.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure the access credentials, see Configure access credentials.

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

Method

func (c *Client) GetBucketLocation(ctx context.Context, request *GetBucketLocationRequest, optFns ...func(*Options)) (*GetBucketLocationResult, error) 

Request parameters

Parameter

Type

Description

ctx

context.Context

The context of the request, which can be used to specify the total duration of the request.

request

*GetBucketLocationRequest

Specifies the parameters of a specific API operation, such as the bucket name. For more information, see GetBucketLocationRequest.

optFns

...func(*Options)

Optional. The operation-level parameter. For more information, see Options.

Response parameters

Response parameter

Type

Description

result

*GetBucketLocationResult

The response to the operation. This parameter is valid when the value of err is nil. For more information, see GetBucketLocationResult.

err

error

The status of the request. If the request fails, the value of err cannot be nil.

Examples

The following sample code provides an example on how to query the region of a bucket:

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Specify the global variables.
var (
	region     string // The region.
	bucketName string // The name of the bucket.
)

// Specify the init function used to initialize command line parameters.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The `name` of the bucket.")
}

func main() {
	// Parse command line parameters.
	flag.Parse()

	// Check whether the bucket name is empty.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is empty.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to query the region of the bucket.
	request := &oss.GetBucketLocationRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
	}

	// Execute the request to query the region of the bucket and process the result.
	result, err := client.GetBucketLocation(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get bucket location %v", err)
	}

	// Display the region of the bucket.
	log.Printf("get bucket location:%#v\n", *result.LocationConstraint)
}

References

  • For the complete sample code that is used to query the region of a bucket, visit GitHub.

  • For more information about the API operation that you can call to query the region of a bucket, see GetBucketLocation.