Query the region in which a bucket is located.
Usage notes
-
The sample code uses the region ID
cn-hangzhouof the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket by using other Alibaba Cloud services in the same region in which the bucket is located, use the internal endpoint. For more information about OSS regions and endpoints, see Regions and Endpoints. -
The sample code obtains access credentials from environment variables. For more information, see Configure access credentials.
-
To query the region of a bucket, you must have the
oss:GetBucketLocationpermission. For more information, see Grant a custom policy.
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 |
The request parameters, 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. Valid when err is nil. For more information, see GetBucketLocationResult. |
|
err |
error |
The error message. A non-nil value indicates a failed request. |
Examples
The following sample code queries 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(®ion, "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, visit GitHub.
-
For the API reference, see GetBucketLocation.