Configure uma lista de permissões ou uma lista de bloqueios de Referer e defina se solicitações com Referer vazio são permitidas para um bucket do Object Storage Service (OSS). Essa configuração evita acessos não autorizados aos recursos do bucket e previne custos inesperados de tráfego.
Observações de uso
Antes de configurar a proteção contra hotlink, familiarize-se com esse recurso. Para mais informações, consulte Proteção contra hotlink.
Este tópico utiliza o endpoint público da região China (Hangzhou). Caso precise acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, utilize um endpoint interno. Para mais detalhes sobre regiões e endpoints do OSS, consulte Regiões e endpoints.
Neste tópico, as credenciais de acesso são obtidas de variáveis de ambiente. Para saber como configurar credenciais de acesso, consulte Configurar credenciais de acesso (Python SDK V1).
Este tópico demonstra a criação de uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como uso de domínio personalizado ou autenticação com credenciais do Security Token Service (STS), consulte Inicialização.
A configuração da proteção contra hotlink exige a permissão
oss:PutBucketReferer. A consulta das configurações exige a permissãooss:GetBucketReferer. Para mais informações, consulte Conceder uma política personalizada.
Configurar a proteção contra hotlink para um bucket
O código de exemplo a seguir mostra como configurar a proteção contra hotlink para 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))
Consultar as configurações de proteção contra hotlink de um bucket
O exemplo de código abaixo ilustra 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))
Limpar configurações de proteção contra hotlink
Utilize o código a seguir para 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 detalhes sobre a operação de API usada na configuração da proteção contra hotlink de um bucket, consulte PutBucketReferer.
Para consultar as configurações de proteção contra hotlink via API, consulte a documentação da operação GetBucketReferer.