Todos los productos
Search
Centro de documentos

Object Storage Service:Protección contra hotlinking (OSS SDK for Python 1.0)

Última actualización: Sep 03, 2026

Puede configurar una lista blanca o negra de Referer y especificar si se permiten solicitudes con un campo Referer vacío en un bucket de Object Storage Service (OSS). Esto evita el acceso no autorizado a los recursos del bucket y previene cargos inesperados por tráfico.

Notas de uso

  • Antes de configurar la protección contra hotlinking, familiarícese con esta función. Para obtener más información, consulte Hotlink protection.

  • 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, 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 usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Initialization.

  • Para configurar la protección contra hotlinking, debe contar con el permiso oss:PutBucketReferer. Para consultar las configuraciones de protección contra hotlinking, debe contar con el permiso oss:GetBucketReferer. Para obtener más información, consulte Grant a custom policy.

Configurar la protección contra hotlinking para un bucket

El siguiente código de ejemplo muestra cómo configurar la protección contra hotlinking para un 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 las configuraciones de protección contra hotlinking de un bucket

El siguiente código de ejemplo muestra cómo consultar las configuraciones de protección contra hotlinking de un 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))

Elimine las configuraciones de protección contra hotlinking

El siguiente código de ejemplo muestra cómo eliminar las configuraciones de protección contra hotlinking de un 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, []))

Referencias

  • Para obtener más información sobre la operación de API que permite configurar la protección contra hotlinking de un bucket, consulte PutBucketReferer.

  • Para obtener más información sobre la operación de API que permite consultar las configuraciones de protección contra hotlinking de un bucket, consulte GetBucketReferer.