You can use the Go SDK for Alibaba Cloud OSS to configure access rules based on the Referer request header, including setting a Referer whitelist, a Referer blacklist, and specifying whether to allow empty Referer headers. This lets you block specific Referers from accessing your OSS files, prevent other websites from hotlinking your resources, and avoid unnecessary traffic costs.
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 and endpoints.
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 Configure a client (Go SDK V1).
To configure hotlink protection, you must have the
oss:PutBucketRefererpermission. To query hotlink protection configurations, you must have theoss:GetBucketRefererpermission. For more information, see Attach a custom policy to a RAM user.
Sample code
References
For the complete sample code for hotlink protection, see GitHub.
For more information about the API operation for setting hotlink protection, see SetBucketRefererV2.
For more information about the API operation for retrieving hotlink protection configurations, see GetBucketReferer.