Todos os produtos
Search
Central de documentação

Object Storage Service:Proteção contra hotlink (OSS SDK for Python 1.0)

Última atualização: Sep 22, 2026

Configure uma lista de permissões ou uma lista de bloqueios de Referer e especifique se o bucket do Object Storage Service (OSS) aceitará requisições com o campo Referer vazio para evitar acesso não autorizado aos recursos e cobranças imprevistas de tráfego.

Notas de uso

  • Antes de configurar a proteção contra hotlink, familiarize-se com essa funcionalidade. Para mais informações, consulte Hotlink protection.

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

  • Neste tópico, as credenciais de acesso são obtidas a partir de variáveis de ambiente. Para saber como configurar suas credenciais, consulte Configure access credentials using OSS SDK for Python 1.0.

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

  • Para configurar a proteção contra hotlink, você deve ter a permissão oss:PutBucketReferer. Para consultar as configurações de proteção contra hotlink, você deve ter a permissão oss:GetBucketReferer. Para mais informações, consulte Grant a custom policy.

Configure a proteção contra hotlink para um bucket

O exemplo de código a seguir mostra como configurar a proteção contra hotlink em um bucket:

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

# Obtain access credentials from environment variables. Before you run the 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 your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Specify that empty Referer fields are allowed. 
allow_empty_referer = True 
# Specify a Referer whitelist. 
referers = ['http://www.aliyun.com', 'https://www.aliyun.com']
# Specify a Referer blacklist. 
# black_referers = ['http://example.com', 'http://*.example.com']
# Specify that query strings can be truncated. 
allow_truncate_query_string = True
# Configure hotlink protection. 
bucket.put_bucket_referer(BucketReferer(allow_empty_referer=allow_empty_referer, referers=referers, black_referers=black_referers,allow_truncate_query_string=allow_truncate_query_string))

Consulte as configurações de proteção contra hotlink de um bucket

O exemplo de código a seguir mostra como consultar as configurações de proteção contra hotlink de um bucket:

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

# Obtain access credentials from environment variables. Before you run the 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 your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

config = bucket.get_bucket_referer()
print('allow empty referer={0}, referers={1}'.format(config.allow_empty_referer, config.referers))

Exclua as configurações de proteção contra hotlink

O exemplo de código a seguir mostra como excluir as configurações de proteção contra hotlink de um bucket:

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

# Obtain access credentials from environment variables. Before you run the 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 your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# The hotlink protection configurations of a bucket cannot be directly cleared. To overwrite the existing hotlink protection configurations, you must configure a new hotlink protection rule that allows requests with an empty Referer header. 
bucket.put_bucket_referer(BucketReferer(True, []))

Referências

  • Para mais informações sobre a operação de API usada para configurar a proteção contra hotlink em um bucket, consulte PutBucketReferer.

  • Para mais informações sobre a operação de API usada para consultar as configurações de proteção contra hotlink de um bucket, consulte GetBucketReferer.