All Products
Search
Document Center

Object Storage Service:Use the SDK for Android to configure Referer-based hotlink protection to prevent unauthorized reference to OSS objects

Last Updated:Feb 02, 2024

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 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 Initialization.

  • To configure hotlink protection, you must have the oss:PutBucketReferer permission. To query hotlink protection configurations, you must have the oss:GetBucketReferer permission. For more information, see Attach a custom policy to a RAM user.

Configure hotlink protection for a bucket

The following sample code provides an example on how to configure hotlink protection for a bucket:

PutBucketRefererRequest request = new PutBucketRefererRequest();
request.setBucketName("examplebucket");
// Specify a Referer whitelist. You can use asterisks (*) and question marks (?) as wildcard characters in Referers. 
ArrayList<String> referers = new ArrayList<String>();
referers.add("http://www.aliyun.com");
referers.add("https://www.aliyun.com");
// referers.add("http://www.help.alibabacloud.com");
// referers.add("http://www.?.aliyuncs.com");
request.setReferers(referers);

OSSAsyncTask task = oss.asyncPutBucketReferer(request, new OSSCompletedCallback<PutBucketRefererRequest, PutBucketRefererResult>() {
    @Override
    public void onSuccess(PutBucketRefererRequest request, PutBucketRefererResult result) {
        OSSLog.logInfo("code: " + result.getStatusCode());
    }
    @Override
    public void onFailure(PutBucketRefererRequest request, ClientException clientException, ServiceException serviceException) {
        OSSLog.logError("error: "+serviceException.getRawMessage());
    }
});
task.waitUntilFinished();

Query the hotlink protection configurations of a bucket

The following sample code provides an example on how to query the hotlink configurations of a bucket:

GetBucketRefererRequest request = new GetBucketRefererRequest();
request.setBucketName("yourBucketName");
OSSAsyncTask task = oss.asyncGetBucketReferer(request, new OSSCompletedCallback<GetBucketRefererRequest, GetBucketRefererResult>() {
    @Override
    public void onSuccess(GetBucketRefererRequest request, GetBucketRefererResult result) {
        // Query the Referer whitelist of the bucket. 
        ArrayList<String> list = result.getReferers();
        for (String ref : list){
            OSSLog.logInfo("info: " + ref);
        }
    }
    @Override
    public void onFailure(GetBucketRefererRequest request, ClientException clientException, ServiceException serviceException) {
        OSSLog.logError("error: "+serviceException.getRawMessage());
    }
});
task.waitUntilFinished();

Clear the hotlink protection configurations of a bucket

The following sample code provides an example on how to delete the hotlink protection configurations of a bucket:

PutBucketRefererRequest request = new PutBucketRefererRequest();
request.setBucketName("yourBucketName");
request.setAllowEmpty(true);
// The hotlink protection configurations of a bucket cannot be directly cleared. To overwrite the existing hotlink protection configurations, you need to configure a new hotlink protection rule that allows requests with an empty Referer header. 
ArrayList<String> referers = new ArrayList<String>();

request.setReferers(referers);
OSSAsyncTask task = oss.asyncPutBucketReferer(request, new OSSCompletedCallback<PutBucketRefererRequest, PutBucketRefererResult>() {
    @Override
    public void onSuccess(PutBucketRefererRequest request, PutBucketRefererResult result) {
        OSSLog.logInfo("code: " + result.getStatusCode());

    }
    @Override
    public void onFailure(PutBucketRefererRequest request, ClientException clientException, ServiceException serviceException) {
        OSSLog.logError("error: "+serviceException.getRawMessage());

    }
});
task.waitUntilFinished();

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 PutBucketReferer.

  • For more information about the API operation that you can call to query the hotlink protection configurations of a bucket, see GetBucketReferer.