You can use the Alibaba Cloud OSS SDK for Python to configure access rules based on the Referer request header, such as setting a Referer whitelist, a Referer blacklist, and specifying whether to allow empty Referer headers. These configurations allow you to block specific Referer headers from accessing your OSS files, prevent unauthorized use of your resources, and avoid unnecessary traffic costs.
Prerequisites
Before you configure hotlink protection, make sure that you understand this feature. For more information, see Hotlink protection.
The sample code in this topic uses the China (Hangzhou) region (ID:
cn-hangzhou) as an example. By default, the public endpoint is used. If you want to access OSS from other Alibaba Cloud services in the same region, use the internal endpoint. For more information about OSS regions and their corresponding endpoints, see OSS regions and endpoints.To set or delete hotlink protection configurations, you must have the
oss:PutBucketRefererpermission. To retrieve hotlink protection configurations, you must have theoss:GetBucketRefererpermission. For more information, see Grant custom permissions to a RAM user.
Method definitions
Set hotlink protection
put_bucket_referer(request: PutBucketRefererRequest, **kwargs) → PutBucketRefererResultObtain hotlink protection settings
get_bucket_referer(request: GetBucketRefererRequest, **kwargs) → GetBucketRefererResultRequest parameters
Parameter | Type | Description |
request | PutBucketRefererRequest | The request parameters. For more information, see PutBucketRefererRequest |
GetBucketRefererRequest | The request parameters. For more information, see GetBucketRefererRequest |
Return values
Type | Description |
PutBucketRefererResult | The return value. For more information, see PutBucketRefererResult |
GetBucketRefererResult | The return value. For more information, see GetBucketRefererResult |
For the complete definition of the method for setting hotlink protection, see put_bucket_referer.
For the complete definition of the method for retrieving hotlink protection settings, see get_bucket_referer.
Examples
References
For information about how to troubleshoot common errors that occur when you configure hotlink protection, see 33-REFERER.
For the complete sample code for setting hotlink protection, see put_bucket_referer.py.
For the complete sample code for retrieving hotlink protection configurations, see get_bucket_referer.py.