You can use Object Storage Service (OSS) SDK for Ruby to configure a Referer-based filtering policy to block specific Referers from accessing data in your bucket. This way, you can prevent unauthorized access to your data and avoid increased 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 by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.
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 Create an OSSClient instance.
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.
Configure hotlink protection for a bucket
The following code provides an example on how to configure hotlink protection for a bucket:
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# Specify the name of the bucket. Example: examplebucket.
bucket = client.get_bucket('examplebucket')
# Configure a Referer whitelist for the bucket.
bucket.referer = Aliyun::OSS::BucketReferer.new(
allow_empty: true, whitelist: ['http://www.aliyun.com', 'https:www.aliyun.com'])
Query the hotlink protection configurations of a bucket
The following code provides an example on how to query the hotlink protection configurations of a bucket:
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# Specify the name of the bucket. Example: examplebucket.
bucket = client.get_bucket('examplebucket')
ref = bucket.referer
puts ref.to_s
Delete the hotlink protection configurations of a bucket
The following code provides an example on how to delete the hotlink protection configurations of a bucket:
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# Specify the name of the bucket. Example: examplebucket.
bucket = client.get_bucket('examplebucket')
bucket.referer = Aliyun::OSS::BucketReferer.new(allow_empty: true, whitelist: [])
References
For more information about the API operation that you can call to configure hotlink protection for a bucket, see PutBucketReferer.
For more information about the API operation that you can call to query the hotlink protection configurations of a bucket, see GetBucketReferer.