This topic describes how to query the endpoints of all supported regions or the endpoints of a specific region using OSS SDK V2, including public endpoints, internal endpoints, and acceleration endpoints.
Usage notes
You can query endpoints for all supported regions or a specific region, regardless of whether you have created a bucket in the regions or a specific region.
The sample code in this topic uses the region ID
cn-hangzhouof the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. If you want to access resources in the bucket from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see OSS 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.
Query the endpoints of all supported regions
The following sample code provides an example on how to query the endpoints of all supported regions:
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 to store the command line parameters.
var (
region string
)
// Specify the init function used to initialize command line parameters and configure the default values and help information of the parameters.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located. Required.")
}
func main() {
// Parse the command-line parameters.
flag.Parse()
// Check whether the region is empty. This parameter is required.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Load the default configurations from environment parameters 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 all the regions.
request := &oss.DescribeRegionsRequest{}
// Send a request to query all the regions.
result, err := client.DescribeRegions(context.TODO(), request)
if err != nil {
log.Fatalf("failed to describe regions %v", err)
}
// Print the query results.
for _, region := range result.RegionInfoList.RegionInfos {
// Print the region information, including the public access domain name, internal access domain name, and acceleration access domain name.
log.Printf("region:%s, public endpoint:%s, internal endpoint:%s, acceleration endpoint:%s\n", *region.Region, *region.InternetEndpoint, *region.InternalEndpoint, *region.AccelerateEndpoint)
}
}
Query the endpoints of a specific region
The following sample code provides an example on how to query the endpoints of a specific region:
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 to store the command line parameters.
var (
region string
)
// Specify the init function used to initialize command line parameters and configure the default values and help information of the parameters.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located. Required.")
}
func main() {
// Parse the command-line parameters.
flag.Parse()
// Check whether the region is empty. This parameter is required.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Load the default configurations from environment parameters 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 all the regions.
request := &oss.DescribeRegionsRequest{
Regions: oss.Ptr("oss-cn-hangzhou"), // Use China (Hangzhou) as an example and fill in oss-cn-hangzhou. For other regions, fill in according to the actual situation
}
// Send a request to query all the regions.
result, err := client.DescribeRegions(context.TODO(), request)
if err != nil {
log.Fatalf("failed to describe regions %v", err)
}
// Print the query results.
for _, region := range result.RegionInfoList.RegionInfos {
// Print the region information, including the public access domain name, internal access domain name, and acceleration access domain name.
log.Printf("region:%s, public endpoint:%s, internal endpoint:%s, acceleration endpoint:%s\n", *region.Region, *region.InternetEndpoint, *region.InternalEndpoint, *region.AccelerateEndpoint)
}
}
Reference
For more information about the API operation that you can call to query the endpoints of all regio DescribeRegions.