This topic describes how to configure, query, and delete the policies of a bucket.

Background information

Bucket policies provide resource-based authorization for users. Bucket policies apply to the following scenarios:

  • Authorize RAM users of other accounts to access your OSS resources.

    You can authorize RAM users of other accounts to access your OSS resources.

  • Authorize anonymous users to access your OSS resources using specific IP addresses or IP ranges.

    In some cases, you must authorize anonymous users to access OSS resources using specific IP addresses or IP ranges. For example, confidential documents of an enterprise are only allowed to be accessed within the enterprise but not in other regions. Previously, configuring RAM policies for every user was a tedious and complex task because of the potential for a large number of internal users. To resolve this issue, you can configure access policies with IP restrictions based on bucket policies to authorize a large number of users easily and efficiently.

For more information about bucket policy configurations and use cases, see Use bucket policies to authorize other users to access OSS resources. For more information about bucket policy syntax, see Policy structure and syntax.

Configure bucket policies

The following code provides an example on how to configure a bucket policy:

// This example uses China (Hangzhou) as the endpoint. Specify the actual endpoint based on your requirements. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to https://ram.console.aliyun.com. String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";

// Create an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
 // Specify the policyText.
String policyText = "{\"Statement\": [{\"Effect\": \"Allow\", \"Action\": [\"oss:GetObject\", \"oss:ListObjects\"], \"Resource\": [\"acs:oss:*:*:*/user1/*\"]}], \"Version\": \"1\"}";

// Configure the bucket policy.
ossClient.setBucketPolicy(bucketName, policyText);
 // Shut down the OSSClient instance. ossClient.shutdown();

For more information about how to configure bucket policies, see PutBucketPolicy.

Query bucket policies

The following code provides an example on how to query the policies configured for a bucket:

// This example uses China (Hangzhou) as the endpoint. Specify the actual endpoint based on your requirements. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to https://ram.console.aliyun.com. String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";

// Create an OSSClient instance. OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Query the policies configured for the bucket. GetBucketPolicyResult result = ossClient.getBucketPolicy(bucketName);
System.out.println("policyText:"+ result.getPolicyText());

// Shut down the OSSClient instance. 
ossClient.shutdown();

For more information about how to query bucket policies, see GetBucketPolicy.

Delete bucket policies

The following code provides an example on how to delete the policies configured for a bucket:

// This example uses China (Hangzhou) as the endpoint. Specify the actual endpoint based on your requirements. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to https://ram.console.aliyun.com. String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";

// Create an OSSClient instance. OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Delete the policies configured for the bucket.
ossClient.deleteBucketPolicy(bucketName);

// Shut down the OSSClient instance. 
ossClient.shutdown();

For more information about how to delete bucket policies, see DeleteBucketPolicy.