Todos os produtos
Search
Central de documentação

Object Storage Service:Attach a custom domain name (Python SDK V1)

Última atualização: Sep 22, 2026

Após enviar objetos para um bucket, o Object Storage Service (OSS) gera automaticamente URLs que incluem o endpoint público do bucket para os objetos enviados. Use essas URLs para acessar os objetos. Para acessar os objetos com um nome de domínio personalizado, adicione um registro CNAME e mapeie o nome de domínio personalizado ao bucket que armazena os objetos.

Observações

  • Este tópico usa o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região do OSS, use um endpoint interno. Para obter mais informações sobre as regiões e endpoints do OSS, consulte Regions and endpoints.

  • Neste tópico, você obtém as credenciais de acesso a partir de variáveis de ambiente. Configure as credenciais de acesso conforme descrito em Configure access credentials using OSS SDK for Python 1.0.

  • Este tópico demonstra como criar uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como usar um domínio personalizado ou autenticar com credenciais do Security Token Service (STS), consulte Initialization.

Crie um token CNAME

O código de exemplo a seguir mostra como criar um token 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)

Consulte um token CNAME

O código de exemplo a seguir mostra como consultar um token 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)

Adicione um registro CNAME

O código de exemplo a seguir mostra como adicionar um registro CNAME para um 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)

Consulte um registro CNAME

O código de exemplo a seguir mostra como consultar um registro CNAME para um 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)

Exclua um registro CNAME

O código de exemplo a seguir mostra como excluir o registro CNAME que aponta para um 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)

Referências

  • Para obter mais informações sobre a operação de API usada na criação de um token CNAME para verificação de propriedade do nome de domínio, consulte CreateCnameToken.

  • Se precisar de detalhes sobre a operação de API para consultar um token CNAME, consulte GetCnameToken.

  • Para saber mais sobre a operação de API que adiciona um registro CNAME, acesse PutCname.

  • Consulte ListCname para obter mais informações sobre a operação de API de consulta de um registro CNAME.

  • Para entender como funciona a operação de API que exclui um registro CNAME, veja DeleteCname.