You can use the Alibaba Cloud OSS Go SDK to configure access rules based on the Referer request header. These rules allow you to set a Referer whitelist, a Referer blacklist, and specify whether to allow requests with empty Referer headers. This configuration helps you block access from specific Referer headers, prevent other websites from hotlinking your resources, and avoid unnecessary increases in traffic fees.
Precautions
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 (
cn-hangzhou) and a public endpoint by default. If you access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the mappings between OSS regions and endpoints, see OSS regions and endpoints.The sample code in this topic shows how to obtain access credentials from environment variables. For more information about how to configure access credentials, see Configure access credentials.
To set or clear hotlink protection, you must have the
oss:PutBucketRefererpermission. To retrieve the hotlink protection configuration, you must have theoss:GetBucketRefererpermission. For more information, see Grant custom permissions to a RAM user.
Sample code
References
For more information about the API operation for setting hotlink protection, see PutBucketReferer.
For more information about the API operation for retrieving the hotlink protection configuration, see GetBucketReferer.