You can configure a Referer whitelist or a Referer blacklist and specify whether to allow a request with an empty Referer for an Object Storage Service (OSS) bucket to prevent unauthorized access to resources in the bucket and unexpected traffic fees.
Usage notes
Before you configure hotlink protection, make sure that you familiarize yourself with this feature. For more information, see Hotlink protection.
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions, endpoints and open ports.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
To configure hotlink protection, you must have the
oss:PutBucketReferer
permission. To query hotlink protection configurations, you must have theoss:GetBucketReferer
permission. For more information, see Attach a custom policy to a RAM user.
Sample code
Configure a Referer whitelist for a bucket
Query the hotlink protection configurations of a bucket
Delete the hotlink protection configurations of a bucket
References
For the complete sample code of hotlink protection, visit GitHub.
For more information about the API operation that you can call to configure hotlink protection for a bucket, see SetBucketRefererV2.
For more information about the API operation that you can call to query the hotlink protection configurations of a bucket, see GetBucketReferer.