Hotlink protection (Java SDK V2)

Updated at:

Manage bucket hotlink protection rules by calling the PutBucketReferer and GetBucketReferer operations with OSS Java SDK V2.

Prerequisites

  • OSS Java SDK V2 is installed. If you use Maven, add the dependency com.aliyun:alibabacloud-oss-v2 to your pom.xml.

  • Access credentials are configured by using environment variables. The sample code in this topic uses EnvironmentVariableCredentialsProvider to read credentials from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.

Permissions

By default, an Alibaba Cloud account has all permissions, but RAM users and RAM roles under the account have none. The Alibaba Cloud account or an account administrator must grant the permissions required to perform the operations.

API

Action

Description

PutBucketReferer

oss:PutBucketReferer

Set or query the hotlink protection rules of a bucket.

Sample code

import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;
import java.util.Arrays;

public class BucketRefererSample {
    public static void main(String[] args) throws Exception {
        String region = "cn-hangzhou";
        String bucket = "examplebucket";

        CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
        OSSClientBuilder clientBuilder = OSSClient.newBuilder()
                .credentialsProvider(provider)
                .region(region);

        try (OSSClient client = clientBuilder.build()) {
            // Configure hotlink protection
            RefererList referers = RefererList.newBuilder()
                    .referers(Arrays.asList(
                            "http://www.aliyun.com",
                            "https://www.aliyun.com"))
                    .build();

            RefererConfiguration refererConfig = RefererConfiguration.newBuilder()
                    .allowEmptyReferer(false)
                    .refererList(referers)
                    .build();

            PutBucketRefererResult putResult = client.putBucketReferer(
                    PutBucketRefererRequest.newBuilder()
                            .bucket(bucket)
                            .refererConfiguration(refererConfig)
                            .build());

            System.out.printf("PutBucketReferer status code:%d%n", putResult.statusCode());

            // Query the hotlink protection configuration
            GetBucketRefererResult getResult = client.getBucketReferer(
                    GetBucketRefererRequest.newBuilder().bucket(bucket).build());

            RefererConfiguration config = getResult.refererConfiguration();
            System.out.printf("Allow empty referer: %s%n", config.allowEmptyReferer());
            for (String referer : config.refererList().referers()) {
                System.out.printf("Referer: %s%n", referer);
            }

        } catch (Exception e) {
            System.out.printf("error:%n%s", e);
        }
    }
}

References