All Products
Search
Document Center

Object Storage Service:Map custom domain names

Last Updated:Nov 06, 2024

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 from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions, endpoints and open ports.

  • 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 sample code provides an example on how to create a CNAME token:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# 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. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify the custom domain name. 
test_domain = 'www.example.com'
# Create a CNAME token. 
result = bucket.create_bucket_cname_token(test_domain)
# Show the name of the CNAME record that is mapped to the bucket. 
print(result.cname)
# Show the CNAME token that is returned by OSS. 
print(result.token)
# Show the point in time when the CNAME token expires. 
print(result.expire_time)

Query a CNAME token

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

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify the custom domain name. 
test_domain = 'www.example.com'
# Obtain the created CNAME token. 
result = bucket.get_bucket_cname_token(test_domain)
# Show the name of the CNAME record that is mapped to the bucket. 
print(result.cname)
# Show the CNAME token that is returned by OSS. 
print(result.token)
# Show the point in time when the CNAME token expires. 
print(result.expire_time)

Add a CNAME record

The following sample code provides an example on how to add a CNAME record for a bucket:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# 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. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify the custom domain name. 
test_domain = 'www.example.com'
# Specify the ID of the certificate. 
previous_cert_id = '001'
certificate = '''-----BEGIN CERTIFICATE-----
MIIDWzCCAkOgA******KTgnwyOGU9cv+mxA=
-----END CERTIFICATE-----'''
# Specify the private key of the certificate. 
private_key = '''-----BEGIN PRIVATE KEY-----
MIIEvQIBADAN******1i2t41Q/SC3HUGC5mJjpO8=
-----END PRIVATE KEY-----
'''

cert = oss2.models.CertInfo(certificate=certificate, private_key=private_key)
# Set force to True to forcibly overwrite the certificate. 
# Specify whether to delete the certificate. If you want to delete the certificate, set delete_certificate to True. If you want to retain the certificate, set delete_certificate to False. 
# cert = oss2.models.CertInfo(certificate=certificate, private_key=private_key, force=True, delete_certificate=False)
input = oss2.models.PutBucketCnameRequest(test_domain, cert)
bucket.put_bucket_cname(input)

Query a CNAME record

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

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

list_result = bucket.list_bucket_cname()
for c in list_result.cname:
    # Show the ID of the certificate. 
    print(c.certificate.cert_id)
    # Show the source of the certificate. 
    print(c.certificate.type)
    # Show the status of the certificate. 
    print(c.certificate.status)
    # Show the custom domain name. 
    print(c.domain)
    # Show the point in time when the custom domain name is mapped to the bucket. 
    print(c.last_modified)
    # Show the status of the custom domain name. 
    print(c.status)

Delete a CNAME record

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

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify the custom domain name. 
test_domain = 'www.example.com'
# Delete the CNAME record. 
bucket.delete_bucket_cname(test_domain)

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.