You can use the Alibaba Cloud OSS Python SDK to configure access rules based on the request header Referer
, including setting a whitelist Referer
, a blacklist Referer
, and whether to allow an empty Referer
. With these configurations, you can block specific Referer
from accessing your OSS files to prevent other websites from misusing your resources, thereby avoiding unnecessary traffic cost increases.
Precautions
-
Before configuring hotlink protection, ensure that you are familiar with this feature. For more information, see hotlink protection.
-
This topic includes sample code that uses the China (Hangzhou) region ID
cn-hangzhou
as an example. By default, the public endpoint is utilized. To access OSS from other Alibaba Cloud products within the same region, you should use the internal endpoint. For more information about the correlation between OSS-supported regions and their endpoints, see OSS regions and endpoints. -
To configure or remove hotlink protection, you must have the
oss:PutBucketReferer
permission. To use hotlink protection, you must have theoss:GetBucketReferer
permission. For detailed instructions, see grant custom policy to RAM users.
Method definitions
Set hotlink protection
put_bucket_referer(request: PutBucketRefererRequest, **kwargs) → PutBucketRefererResult
Obtain hotlink protection settings
get_bucket_referer(request: GetBucketRefererRequest, **kwargs) → GetBucketRefererResult
Request parameters
Parameter name | Type | Value |
request | PutBucketRefererRequest | Set request parameters. For more information, see PutBucketRefererRequest |
GetBucketRefererRequest | Set request parameters. For more information, see GetBucketRefererRequest |
Return values
Type | Value |
PutBucketRefererResult | Return value. For more information, see PutBucketRefererResult |
GetBucketRefererResult | Return value. For more information, see GetBucketRefererResult |
For the complete method definition to set hotlink protection, see put_bucket_referer.
For the complete method definition to obtain hotlink protection settings, see get_bucket_referer.
Example code
References
-
For assistance with common errors associated with hotlink protection, see 33-REFERER.
-
For the complete sample code to set hotlink protection, see put_bucket_referer.py.
-
For the complete sample code to retrieve hotlink protection settings, see get_bucket_referer.py.