Map a custom domain name (Go SDK V2)
Object Storage Service (OSS) generates a URL based on the bucket's public endpoint for each uploaded object. To access objects by using a custom domain name instead, add a CNAME record to map the domain name to the bucket.
Notes
-
The sample code uses the region ID
cn-hangzhoufor the China (Hangzhou) region. By default, the public endpoint is used. To access OSS from other Alibaba Cloud services in the same region as your bucket, use an internal endpoint. For more information about supported regions and endpoints, see Regions and Endpoints. -
The examples in this topic read access credentials from environment variables. For more information about configuring access credentials, see Configure access credentials (Go SDK V1).
Sample code
Create a CNAME token
Use the following code to create a CNAME token.
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(®ion, "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)
}
Get a CNAME token
Use the following code to get a CNAME token.
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(®ion, "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)
}
Add a CNAME record
Map custom domain name
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(®ion, "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)
}
Map domain and certificate
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(®ion, "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)
}
Disassociate a certificate
If the certificate is no longer needed for the domain name, you can disassociate it.
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(®ion, "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)
}
List CNAME records
Use the following code to list the CNAME records for 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"
)
// 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(®ion, "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)
}
}
}
Delete a CNAME record
Use the following code to delete a CNAME record.
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(®ion, "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)
}
References
-
For the API operation that creates a CNAME token for domain ownership verification, see CreateCnameToken.
-
For the API operation that gets a CNAME token, see GetCnameToken.
-
For the API operation that adds a CNAME record, see PutCname.
-
For the API operation that lists CNAME records, see ListCname.
-
For the API operation that deletes a CNAME record, see DeleteCname.