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 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 using OSS SDK for Python 1.0.
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 where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the bucket is located, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set the bucket name to examplebucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Specify the custom domain name.
test_domain = 'www.example.com'
# Create a CnameToken.
result = bucket.create_bucket_cname_token(test_domain)
# Print the CNAME.
print(result.cname)
# Print the CnameToken returned by OSS.
print(result.token)
# Print the expiration time of the CnameToken.
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 where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the bucket is located, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set the bucket name to examplebucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Specify the custom domain name.
test_domain = 'www.example.com'
# Get the CnameToken.
result = bucket.get_bucket_cname_token(test_domain)
# Print the CNAME.
print(result.cname)
# Print the CnameToken returned by OSS.
print(result.token)
# Print the expiration time of the CnameToken.
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 where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the bucket is located, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set the bucket name to examplebucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
# Specify the custom domain name.
test_domain = 'www.example.com'
# Specify the ID of the previous certificate.
previous_cert_id = '001'
certificate = '''-----BEGIN CERTIFICATE-----
MIIDWzCCAkOgA******KTgnwyOGU9cv+mxA=
-----END CERTIFICATE-----'''
# Set 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=True to forcefully overwrite the previous certificate.
# Set delete_certificate to specify whether to delete the certificate. Set delete_certificate=True to delete the certificate. Set delete_certificate=False to not delete the certificate.
# 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 where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the bucket is located, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set the bucket name to examplebucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
list_result = bucket.list_bucket_cname()
for c in list_result.cname:
# Print the certificate ID.
print(c.certificate.cert_id)
# Print the source of the certificate.
print(c.certificate.type)
# Print the status of the certificate.
print(c.certificate.status)
# Print the custom domain name.
print(c.domain)
# Print the time when the custom domain name was bound.
print(c.last_modified)
# Print the status of the 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 where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the bucket is located, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set the bucket name to examplebucket.
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.