このトピックでは、パブリックエンドポイント、内部エンドポイント、アクセラレーションエンドポイントなど、Object Storage Service (OSS) でサポートされているすべてのリージョンまたは特定のリージョンのエンドポイントをクエリする方法について説明します。
使用上の注意
OSS SDK for Go V2.2.8以降は、エンドポイントクエリをサポートしています。
リージョンまたは特定のリージョンでバケットを作成したかどうかに関係なく、サポートされているすべてのリージョンまたは特定のリージョンのエンドポイントをクエリできます。
このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョン、エンドポイント、オープンポート」をご参照ください。
このトピックでは、アクセス資格情報は環境変数から取得します。 アクセス資格情報の設定方法の詳細については、「アクセス資格情報の設定」をご参照ください。
このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSTS (Security Token Service) を使用してOSSClientインスタンスを作成する場合は、「初期化」をご参照ください。
サポートされているすべてのリージョンのエンドポイントの照会
次のサンプルコードでは、サポートされているすべてのリージョンのエンドポイントをクエリする方法の例を示します。
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Specify the version of the signature algorithm.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
list, err := client.DescribeRegions()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
for _, region := range list.Regions {
// Show all supported regions.
fmt.Printf("Region:%s\n", region.Region)
// Show the public endpoints of all supported regions.
fmt.Printf("Region Internet Endpoint:%s\n", region.InternetEndpoint)
// Show the internal endpoints of all supported regions. Internal endpoints are intended for access over an Elastic Compute Service instance or a virtual private cloud.
fmt.Printf("Region Internal Endpoint:%s\n", region.InternalEndpoint)
// Show the acceleration endpoints of all supported regions. Acceleration endpoints accelerate uploads and downloads.
fmt.Printf("Region Accelerate Endpoint:%s\n", region.AccelerateEndpoint)
}
fmt.Println("List Describe Regions Success")
}
特定のリージョンのエンドポイントの照会
次のサンプルコードは、特定のリージョンのエンドポイントをクエリする方法の例を示しています。
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Specify the version of the signature algorithm.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
list, err := client.DescribeRegions(oss.AddParam("regions", "oss-cn-hangzhou"))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
for _, region := range list.Regions {
// Show the specified region.
fmt.Printf("Region:%s\n", region.Region)
// Show the public endpoint of the region.
fmt.Printf("Region Internet Endpoint:%s\n", region.InternetEndpoint)
// Show the internal endpoint of the region. Internal endpoints are intended for access over an Elastic Compute Service instance or a virtual private cloud.
fmt.Printf("Region Internal Endpoint:%s\n", region.InternalEndpoint)
// Show the acceleration endpoint of the region. Acceleration endpoints accelerate uploads and downloads.
fmt.Printf("Region Accelerate Endpoint:%s\n", region.AccelerateEndpoint)
}
fmt.Println("List Describe Regions Success")
}
参考情報
OSSでサポートされているすべてのリージョンまたは特定のリージョンのエンドポイントを照会するために呼び出すことができるAPI操作の詳細については、「DescribeRegions」をご参照ください。