Tüm Ürünler
Search
Belge Merkezi

:Özel alan adını eşleme (Go SDK V2)

Son Güncelleme:Oct 02, 2026

Object Storage Service (OSS), yüklenen her nesne için paketin genel uç noktasına dayalı bir URL oluşturur. Nesnelere bunun yerine özel bir alan adı kullanarak erişmek istiyorsanız, alan adını pakete eşlemek için bir CNAME kaydı ekleyin.

Notlar

  • Örnek kod, China (Hangzhou) bölgesi için bölge kimliği olarak cn-hangzhou değerini kullanır. Varsayılan olarak genel uç nokta kullanılır. Paketinizle aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek için dahili uç nokta kullanın. Desteklenen bölgeler ve uç noktalar hakkında daha fazla bilgi için bkz. Bölgeler ve Uç Noktalar.

  • Bu konudaki örnekler, erişim kimlik bilgilerini ortam değişkenlerinden okur. Erişim kimlik bilgilerini yapılandırma hakkında daha fazla bilgi için bkz. Erişim kimlik bilgilerini yapılandırma (Go SDK V1).

Örnek kod

CNAME belirteci oluşturma

CNAME belirteci oluşturmak için aşağıdaki kodu kullanın.

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"
)

// Define global variables.
var (
	region     string // The region where the bucket is located.
	bucketName string // The name of the bucket.
)

// The init function is used to initialize command-line flags.
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 the command-line flags.
	flag.Parse()

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

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

	// Load the default configuration, and set the credentials provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

	// Create a CNAME token.
	request := &oss.CreateCnameTokenRequest{
		Bucket: oss.Ptr(bucketName),
		BucketCnameConfiguration: &oss.BucketCnameConfiguration{
			Cname: &oss.Cname{
				Domain: oss.Ptr("www.example.com"), // Specify the custom domain name.
			},
		},
	}

	// Execute the request to create the CNAME token.
	result, err := client.CreateCnameToken(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to create bucket cname token %v", err)
	}

	// Print the CNAME token information.
	log.Printf("Cname: %s", *result.CnameToken.Cname)
	log.Printf("Token: %s", *result.CnameToken.Token)
	log.Printf("ExpireTime: %s", *result.CnameToken.ExpireTime)
}

CNAME belirteci alma

CNAME belirteci almak için aşağıdaki kodu kullanın.

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"
)

// Define global variables.
var (
	region     string // The region where the bucket is located.
	bucketName string // The name of the bucket.
)

// The init function is used to initialize command-line flags.
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 the command-line flags.
	flag.Parse()

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

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

	// Load the default configuration, and set the credentials provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

	// Create a request to get the CNAME token for the bucket.
	request := &oss.GetCnameTokenRequest{
		Bucket: oss.Ptr(bucketName),        // The name of the bucket.
		Cname:  oss.Ptr("www.example.com"), // Specify the custom domain name.
	}

	// Execute the request to get the CNAME token.
	result, err := client.GetCnameToken(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get bucket cname token %v", err)
	}

	// Print the CNAME token information.
	log.Printf("Cname: %s", result.CnameToken.Cname)
	log.Printf("Token: %s", result.CnameToken.Token)
	log.Printf("ExpireTime: %s", result.CnameToken.ExpireTime)
}

CNAME kaydı ekleme

Özel alan adını eşleme

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"
)

// Define global variables.
var (
	region     string // The region where the bucket is located.
	bucketName string // The name of the bucket.
)

// The init function is used to initialize command-line flags.
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 the command-line flags.
	flag.Parse()

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

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

	// Load the default configuration, and set the credentials provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

	// Create a request to add a CNAME record for the bucket.
	request := &oss.PutCnameRequest{
		Bucket: oss.Ptr(bucketName),
		BucketCnameConfiguration: &oss.BucketCnameConfiguration{
			Cname: &oss.Cname{
				Domain: oss.Ptr("www.example.com"), // Specify the custom domain name.
			},
		},
	}

	// Execute the request to add the CNAME record.
	result, err := client.PutCname(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put bucket cname %v", err)
	}

	// Print the result.
	log.Printf("put bucket cname result:%#v\n", result)
}

Alan adını ve sertifikayı eşleme

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"
)

// Define global variables.
var (
	region     string // The region where the bucket is located.
	bucketName string // The name of the bucket.
)

// The init function is used to initialize command-line flags.
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 the command-line flags.
	flag.Parse()

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

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

	// Load the default configuration, and set the credentials provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

	// Create a request to map a custom domain name and associate a certificate.
	request := &oss.PutCnameRequest{
		Bucket: oss.Ptr(bucketName),
		BucketCnameConfiguration: &oss.BucketCnameConfiguration{
			Cname: &oss.Cname{
				Domain: oss.Ptr("www.example.com"), // Specify the custom domain name.
				CertificateConfiguration: &oss.CertificateConfiguration{
					CertId:      oss.Ptr("92******-cn-hangzhou"),
					Certificate: oss.Ptr("-----BEGIN CERTIFICATE-----MIIFBzCCA++gT2H2hT6Wb3nwxjpLIfXmSVcV*****-----END CERT"),
					PrivateKey:  oss.Ptr("-----BEGIN CERTIFICATE-----MIIFBzCCA++gT2H2hT6Wb3nwxjpLIfXmSVcV*****-----END CERTIFICATE-----"),
					Force:       oss.Ptr(true),
				},
			},
		},
	}

	// Execute the request.
	result, err := client.PutCname(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put bucket cname %v", err)
	}

	// Print the result.
	log.Printf("put bucket cname result:%#v\n", result)
}

Sertifikayı ayırma

Alan adı için sertifikaya artık ihtiyaç duyulmuyorsa sertifikayı ayırabilirsiniz.

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"
)

// Define global variables.
var (
	region     string // The region where the bucket is located.
	bucketName string // The name of the bucket.
)

// The init function is used to initialize command-line flags.
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 the command-line flags.
	flag.Parse()

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

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

	// Load the default configuration, and set the credentials provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

	// Create a request to disassociate the certificate.
	request := &oss.PutCnameRequest{
		Bucket: oss.Ptr(bucketName),
		BucketCnameConfiguration: &oss.BucketCnameConfiguration{
			Cname: &oss.Cname{
				Domain: oss.Ptr("www.example.com"), // Specify the custom domain name.
				CertificateConfiguration: &oss.CertificateConfiguration{
					DeleteCertificate: oss.Ptr(true), // Disassociate the certificate.
				},
			},
		},
	}

	// Execute the request to disassociate the certificate.
	result, err := client.PutCname(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put bucket cname %v", err)
	}

	// Print the result.
	log.Printf("put bucket cname result:%#v\n", result)
}

CNAME kayıtlarını listeleme

Bir paket için CNAME kayıtlarını listelemek üzere aşağıdaki kodu kullanın.

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"
)

// Define global variables.
var (
	region     string // The region where the bucket is located.
	bucketName string // The name of the bucket.
)

// The init function is used to initialize command-line flags.
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 the command-line flags.
	flag.Parse()

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

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

	// Load the default configuration, and set the credentials provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

	// Create a request to list the CNAME records for the bucket.
	request := &oss.ListCnameRequest{
		Bucket: oss.Ptr(bucketName),
	}

	// Execute the request to list the CNAME records.
	result, err := client.ListCname(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to list bucket cname %v", err)
	}

	log.Printf("Bucket: %s", result.Bucket)
	log.Printf("Owner: %s", result.Owner)

	if len(result.Cnames) > 0 {
		for _, cnameInfo := range result.Cnames {
			// Print the custom domain name.
			log.Printf("Domain: %s", cnameInfo.Domain)
			// Print the last modified time.
			log.Printf("LastModified: %s", cnameInfo.LastModified)
			// Print the status of the domain name.
			log.Printf("Status: %s", cnameInfo.Status)
		}
	}
}

CNAME kaydını silme

CNAME kaydını silmek için aşağıdaki kodu kullanın.

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"
)

// Define global variables.
var (
	region     string // The region where the bucket is located.
	bucketName string // The name of the bucket.
)

// The init function is used to initialize command-line flags.
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 the command-line flags.
	flag.Parse()

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

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

	// Load the default configuration, and set the credentials provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

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

	// Create a request to delete a CNAME record for the bucket.
	request := &oss.DeleteCnameRequest{
		Bucket: oss.Ptr(bucketName), // The name of the bucket.
		BucketCnameConfiguration: &oss.BucketCnameConfiguration{
			Cname: &oss.Cname{
				Domain: oss.Ptr("www.example.com"), // Specify the custom domain name.
			},
		},
	}

	// Execute the request to delete the CNAME record.
	result, err := client.DeleteCname(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete bucket cname %v", err)
	}

	// Print the result.
	log.Printf("delete bucket cname result:%#v\n", result)
}

Referanslar

  • Alan adı sahipliği doğrulaması için CNAME belirteci oluşturan API işlemi hakkında bilgi edinmek için bkz. CreateCnameToken.

  • CNAME belirteci alan API işlemi hakkında bilgi edinmek için bkz. GetCnameToken.

  • CNAME kaydı ekleyen API işlemi hakkında bilgi edinmek için bkz. PutCname.

  • CNAME kayıtlarını listeleyen API işlemi hakkında bilgi edinmek için bkz. ListCname.

  • CNAME kaydını silen API işlemi hakkında bilgi edinmek için bkz. DeleteCname.