All Products
Search
Document Center

Object Storage Service:Map custom domain names

Last Updated:Oct 08, 2023

After you upload objects to a bucket, Object Storage Service (OSS) automatically generates URLs that include the public endpoint of the bucket for the uploaded objects. You can use these URLs to access the objects. If you want to access the objects by using a custom domain name, you must add a CNAME record to map the custom domain name to the bucket in which the objects are stored.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.

Create a CNAME token

The following code provides an example on how to create a CNAME token:

package main

import (
    "fmt"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
    "os"
)

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. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    // Specify the name of the bucket. Example: examplebucket. 
    bucketName := "examplebucket"
    // Specify the custom domain name. 
    cname := "www.example.com"
    cbResult,err := client.CreateBucketCnameToken(bucketName,cname)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    fmt.Printf("Cname: %s\n",cbResult.Cname)
    fmt.Printf("Token: %s\n",cbResult.Token)
    fmt.Printf("ExpireTime: %s\n",cbResult.ExpireTime)
}

Query a CNAME token

The following code provides an example on how to query a CNAME token:

package main

import (
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"os"
)

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. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// 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 {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Display the name of the CNAME record that is mapped to the bucket. 
	fmt.Printf("Cname: %s\n", cbResult.Cname)
	// Display the CNAME token that is returned by OSS. 
	fmt.Printf("Token: %s\n", cbResult.Token)
	// Display the point in time when the CNAME token expires. 
	fmt.Printf("ExpireTime: %s\n", cbResult.ExpireTime)
}

Add a CNAME record

Map a custom domain name

package main

import (
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"os"
)

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. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	// Specify the custom domain name. 
	cname := "www.example.com"
	err = client.PutBucketCname(bucketName, cname)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Printf("Put Bucket Cname Success!")
}

Map a custom domain name and associate a certificate with the domain name

package main

import (
    "fmt"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
    "os"
)

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. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    // Specify the name of the bucket. Example: examplebucket. 
    bucketName := "examplebucket"
    var putCnameConfig oss.PutBucketCname
    var CertificateConfig oss.CertificateConfiguration
    // Specify the custom domain name. 
    putCnameConfig.Cname = "www.example.com"
    // Specify the public key of the certificate. 
    certificate := "-----BEGIN CERTIFICATE-----MIIGeDCCBOCgAwIBAgIRAPj4FWpW5XN6kwgU7*******-----END CERTIFICATE-----"
    // Specify the private key of the certificate. 
    privateKey := "-----BEGIN CERTIFICATE-----MIIFBzCCA++gT2H2hT6Wb3nwxjpLIfXmSVcV*****-----END CERTIFICATE-----"
    // Specify the ID of the certificate. 
    CertificateConfig.CertId = "92******-cn-hangzhou"
    CertificateConfig.Certificate = certificate
    CertificateConfig.PrivateKey = privateKey
    CertificateConfig.Force = true
    putCnameConfig.CertificateConfiguration = &CertificateConfig
    err = client.PutBucketCnameWithCertificate(bucketName,putCnameConfig)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    fmt.Printf("Bind Certificate Success!")
}

Disassociate a certificate

If you do not want the domain name to continue using the certificate, you can disassociate the certificate.

package main

import (
    "fmt"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
    "os"
)

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. 
    client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    // Specify the name of the bucket. Example: examplebucket. 
    bucketName := "examplebucket"
    var putCnameConfig oss.PutBucketCname
    var CertificateConfig oss.CertificateConfiguration
    // Specify the custom domain name. 
    putCnameConfig.Cname = "www.example.com"
    // Delete the certificate. 
    CertificateConfig.DeleteCertificate = true
    putCnameConfig.CertificateConfiguration = &CertificateConfig
    err = client.PutBucketCnameWithCertificate(bucketName,putCnameConfig)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    fmt.Printf("Unbind Certificate Success!")
}

Query a CNAME record

The following code provides an example on how to query a CNAME record mapped to a bucket:

package main

import (
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"os"
)

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. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	// Query the CNAME record. 
	cnResult, err := client.ListBucketCname(bucketName)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	var certificate oss.Certificate
	fmt.Printf("Bucket:%s\n", cnResult.Bucket)
	fmt.Printf("Owner:%s\n", cnResult.Owner)
	if len(cnResult.Cname) > 0 {
		for _, cnameInfo := range cnResult.Cname {
			// Display the custom domain name. 
			fmt.Printf("Domain:%s\n", cnameInfo.Domain)
			// Display the point in time when the custom domain name is mapped to the bucket. 
			fmt.Printf("LastModified:%s\n", cnameInfo.LastModified)
			// Display the status of the domain name. 
			fmt.Printf("Status:%s\n", cnameInfo.Status)
			if cnameInfo.Certificate != certificate {
				// Display the source of the certificate. 
				fmt.Printf("Type:%s\n", cnameInfo.Certificate.Type)
				// Display the ID of the certificate. 
				fmt.Printf("CertId:%s\n", cnameInfo.Certificate.CertId)
				// Display the status of the certificate. 
				fmt.Printf("Status:%s\n", cnameInfo.Certificate.Status)
				// Display the point in time when the certificate is associated with the domain name. 
				fmt.Printf("CreationDate:%s\n", cnameInfo.Certificate.CreationDate)
				// Display the signature of the certificate. 
				fmt.Printf("Fingerprint:%s\n", cnameInfo.Certificate.Fingerprint)
				// Display the point in time when the certificate takes effect. 
				fmt.Printf("ValidStartDate:%s\n", cnameInfo.Certificate.ValidStartDate)
				// Display the point in time when the certificate expires. 
				fmt.Printf("ValidEndDate:%s\n", cnameInfo.Certificate.ValidEndDate)
			}
		}
	}
}

Delete a CNAME record

The following code provides an example on how to delete the CNAME record mapped to a bucket:

package main

import (
    "fmt"
    "github.com/aliyun/aliyun-oss-go-sdk/oss"
    "os"
)

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. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
    // 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 {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    fmt.Println("Delete Bucket Cname Success!")
}

References

  • For more information about the API operation that you can call to create a CNAME token for domain name ownership verification, see CreateCnameToken.

  • For more information about the API operation that you can call to query a CNAME token, see GetCnameToken.

  • For more information about the API operation that you can call to add a CNAME record, see PutCname.

  • For more information about the API operation that you can call to query a CNAME record, see ListCname.

  • For more information about the API operation that you can call to delete a CNAME record, see DeleteCname.