Todos los productos
Search
Centro de documentos

Object Storage Service:Vincular un nombre de dominio personalizado (Python SDK V1)

Última actualización: Sep 03, 2026

Después de cargar objetos en un bucket, Object Storage Service (OSS) genera automáticamente URL que incluyen el endpoint público del bucket para dichos objetos. Estas URL permiten acceder a los objetos. Si desea acceder a los objetos mediante un nombre de dominio personalizado, agregue un registro CNAME para asignar el nombre de dominio personalizado al bucket donde se almacenan los objetos.

Notas

  • En este tema se utiliza el endpoint público de la región China (Hangzhou). Si desea acceder a OSS desde otros servicios de Alibaba Cloud en la misma región que OSS, utilice un endpoint interno. Para obtener más información sobre las regiones y endpoints de OSS, consulte Regions and endpoints.

  • En este tema, las credenciales de acceso se obtienen de variables de entorno. Para obtener más información sobre cómo configurar las credenciales de acceso, consulte Configure access credentials using OSS SDK for Python 1.0.

  • Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para configuraciones alternativas, como el uso de un dominio personalizado o la autenticación con credenciales de Security Token Service (STS), consulte Initialization.

Crear un token de CNAME

El siguiente código de ejemplo muestra cómo crear un token de CNAME:

# -*- 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)

Consultar un token de CNAME

El siguiente código de ejemplo muestra cómo consultar un token de CNAME:

# -*- 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)

Agregar un registro CNAME

El siguiente código de ejemplo muestra cómo agregar un registro CNAME para un 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)

Consultar un registro CNAME

El siguiente código de ejemplo muestra cómo consultar un registro CNAME para un 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)

Eliminar un registro CNAME

El siguiente código de ejemplo muestra cómo eliminar el registro CNAME que apunta a un 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)

Referencias

  • Para obtener más información sobre la operación de API que permite crear un token de CNAME para la verificación de propiedad del nombre de dominio, consulte CreateCnameToken.

  • Para obtener más información sobre la operación de API que permite consultar un token de CNAME, consulte GetCnameToken.

  • Para obtener más información sobre la operación de API que permite agregar un registro CNAME, consulte PutCname.

  • Para obtener más información sobre la operación de API que permite consultar un registro CNAME, consulte ListCname.

  • Para obtener más información sobre la operación de API que permite eliminar un registro CNAME, consulte DeleteCname.