すべてのプロダクト
Search
ドキュメントセンター

Object Storage Service:OSS SDK for Goを使用したカスタムドメイン名のマップ

最終更新日:Nov 07, 2024

オブジェクトをバケットにアップロードすると、オブジェクトストレージサービス (OSS) は、アップロードされたオブジェクトのバケットのパブリックエンドポイントを含むURLを自動的に生成します。 これらのURLを使用してオブジェクトにアクセスできます。 カスタムドメイン名を使用してオブジェクトにアクセスする場合は、CNAMEレコードを追加して、カスタムドメイン名をオブジェクトが格納されているバケットにマップする必要があります。

使用上の注意

  • このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョン、エンドポイント、オープンポート」をご参照ください。

  • このトピックでは、アクセス資格情報は環境変数から取得します。 アクセス資格情報の設定方法の詳細については、「アクセス資格情報の設定」をご参照ください。

  • このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSTS (Security Token Service) を使用してOSSClientインスタンスを作成する場合は、「初期化」をご参照ください。

CNAMEトークンの作成

次のサンプルコードは、CNAMEトークンを作成する方法の例を示しています。

package main

import (
	"log"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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 of the bucket. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	// Specify the custom domain name. 
	cname := "www.example.com"

	// Create a CNAME token.
	cbResult, err := client.CreateBucketCnameToken(bucketName, cname)
	if err != nil {
		log.Fatalf("Failed to create CNAME token: %v", err)
	}

	// Display information about the CNAME token.
	log.Printf("Cname: %s", cbResult.Cname)
	log.Printf("Token: %s", cbResult.Token)
	log.Printf("ExpireTime: %s", cbResult.ExpireTime)
}

CNAMEトークンの照会

次のサンプルコードは、CNAMEトークンを照会する方法の例を示しています。

package main

import (
	"log"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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 of the bucket. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	// Specify the custom domain name. 
	cname := "www.example.com"

	// Query the CNAME token.
	cbResult, err := client.GetBucketCnameToken(bucketName, cname)
	if err != nil {
		log.Fatalf("Failed to get CNAME token: %v", err)
	}

	// Display information about the CNAME token.
	log.Printf("Cname: %s", cbResult.Cname)
	log.Printf("Token: %s", cbResult.Token)
	log.Printf("ExpireTime: %s", cbResult.ExpireTime)
}

CNAMEレコードの追加

カスタムドメイン名のマップ

package main

import (
	"log"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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 of the bucket. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	// Specify the custom domain name. 
	cname := "www.example.com"

	// Map the custom domain name to the bucket.
	err = client.PutBucketCname(bucketName, cname)
	if err != nil {
		log.Fatalf("Failed to put bucket CNAME: %v", err)
	}

	// Display the result.
	log.Println("Put Bucket Cname Success!")
}

カスタムドメイン名をマップし、証明書をドメイン名に関連付ける

package main

import (
	"log"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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 of the bucket. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"

	// Add a CNAME record and configure a certificate.
	putCnameConfig := oss.PutBucketCname{
		Cname: "www.example.com",
		CertificateConfiguration: &oss.CertificateConfiguration{
			CertId:      "92******-cn-hangzhou",
			Certificate: "-----BEGIN CERTIFICATE-----MIIGeDCCBOCgAwIBAgIRAPj4FWpW5XN6kwgU7*******-----END CERTIFICATE-----",
			PrivateKey:  "-----BEGIN CERTIFICATE-----MIIFBzCCA++gT2H2hT6Wb3nwxjpLIfXmSVcV*****-----END CERTIFICATE-----",
			Force:       true,
		},
	}

	// Map a custom domain name and associate a certificate with the domain name.
	err = client.PutBucketCnameWithCertificate(bucketName, putCnameConfig)
	if err != nil {
		log.Fatalf("Failed to bind CNAME and certificate: %v", err)
	}

	// Display the result.
	log.Println("Bind Certificate Success!")
}

証明書の関連付けを解除

ドメイン名で証明書を引き続き使用しない場合は、ドメイン名から証明書の関連付けを解除できます。

package main

import (
	"log"

	"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 {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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 of the bucket. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"

	// Add a CNAME record and set the DeleteCertificate parameter to true.
	putCnameConfig := oss.PutBucketCname{
		Cname: "www.example.com",
		CertificateConfiguration: &oss.CertificateConfiguration{
			DeleteCertificate: true,
		},
	}

	// Map a custom domain name and disassociate the certificate from the domain name.
	err = client.PutBucketCnameWithCertificate(bucketName, putCnameConfig)
	if err != nil {
		log.Fatalf("Failed to unbind CNAME and delete certificate: %v", err)
	}

	// Display the result.
	log.Println("Unbind Certificate Success!")
}

CNAMEレコードの照会

次のサンプルコードは、カスタムドメインをバケットにマップするために使用されるCNAMEレコードを照会する方法の例を示しています。

package main

import (
	"log"

	"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 {
		log.Fatalf("Error: %v", err)
	}

	// 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 of the bucket. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"

	// Query the CNAME record. 
	cnResult, err := client.ListBucketCname(bucketName)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}

	var certificate oss.Certificate
	log.Printf("Bucket: %s", cnResult.Bucket)
	log.Printf("Owner: %s", cnResult.Owner)

	if len(cnResult.Cname) > 0 {
		for _, cnameInfo := range cnResult.Cname {
			// Display the custom domain name. 
			log.Printf("Domain: %s", cnameInfo.Domain)
			// Display the point in time when the custom domain name is mapped to the bucket. 
			log.Printf("LastModified: %s", cnameInfo.LastModified)
			// Display the status of the domain name. 
			log.Printf("Status: %s", cnameInfo.Status)
			if cnameInfo.Certificate != certificate {
				// Display the source of the certificate. 
				log.Printf("Type: %s", cnameInfo.Certificate.Type)
				// Display the ID of the certificate. 
				log.Printf("CertId: %s", cnameInfo.Certificate.CertId)
				// Display the status of the certificate. 
				log.Printf("Status: %s", cnameInfo.Certificate.Status)
				// Display the point in time when the certificate is associated with the domain name. 
				log.Printf("CreationDate: %s", cnameInfo.Certificate.CreationDate)
				// Display the signature of the certificate. 
				log.Printf("Fingerprint: %s", cnameInfo.Certificate.Fingerprint)
				// Display the point in time when the certificate takes effect. 
				log.Printf("ValidStartDate: %s", cnameInfo.Certificate.ValidStartDate)
				// Display the point in time when the certificate expires. 
				log.Printf("ValidEndDate: %s", cnameInfo.Certificate.ValidEndDate)
			}
		}
	}
}

CNAMEレコードの削除

次のサンプルコードは、カスタムドメインをバケットにマップするために使用されるCNAMEレコードを削除する方法の例を示しています。

package main

import (
	"log"

	"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 {
		log.Fatalf("Error: %v", err)
	}

	// 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 of the bucket. For example, if your bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.  
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the signature version.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	// Specify the custom domain name. 
	cname := "www.example.com"

	// Delete the CNAME record.
	err = client.DeleteBucketCname(bucketName, cname)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}

	// Display the result.
	log.Println("Delete Bucket Cname Success!")
}

関連ドキュメント

  • ドメイン名の所有権を確認するためにCNAMEトークンを作成するために呼び出すことができるAPI操作の詳細については、「CreateBucketCnameToken」をご参照ください。

  • CNAMEトークンを照会するために呼び出すAPI操作の詳細については、「GetBucketCnameToken」をご参照ください。

  • CNAMEレコードを追加してカスタムドメイン名をバケットにマップするために呼び出すAPI操作の詳細については、「PutBucketCname」をご参照ください。

  • カスタムドメインをバケットにマップするために使用されるCNAMEレコードを照会するために呼び出すことができるAPI操作の詳細については、「ListBucketCname」をご参照ください。

  • カスタムドメインをバケットにマップするために使用されるCNAMEレコードを削除するために呼び出すことができるAPI操作の詳細については、「DeleteBucketCname」をご参照ください。