Bucket permission management
Set bucket access permissions
The following example sets the bucket's permission to private.
1public void setBucketPrivate (BosClient client, String bucketName) {
2 client.setBucketAcl(<bucketName>, CannedAccessControlList.Private);
3}
CannedAccessControlList an enumerated type and contains three values: Private, PublicRead and PublicReadWrite, which correspond to relevant permissions respectively. For details, refer to BOS API Documentation [Permission Control Using CannedAcl](BOS/API Reference/Access control.md#Permission control by CannedAcl).
Set access permissions for a specific user on the bucket
BOS can also set the access permissions of a specified user to the bucket. Refer to the following code for implementation:
1List<Grant> accessControlList = new ArrayList<Grant>();
2List<Grantee> grantees = new ArrayList<Grantee>();
3List<Permission> permissions = new ArrayList<Permission>();
4List<String> ipAddress = new ArrayList<String>();
5List<String> stringLike = new ArrayList<String>();
6List<String> stringEquals = new ArrayList<String>();
7List<String> resource = new ArrayList<String>();
8List<String> notResource = new ArrayList<String>();
9Referer referer = new Referer();
10Condition condition = new Condition();
11 // Grant permission to specific user
12grantees.add(new Grantee("user_id1"));
13grantees.add(new Grantee("user_id2"));
14grantees.add(new Grantee("user_id3"));
15 //Grant permission to Everyone
16grantees.add(new Grantee("*"));
17 //Set permissions
18permissions.add(Permission.WRITE);
19permissions.add(Permission.READ);
20permissions.add(Permission.LIST);
21 // Set ip
22ipAddress.add("ipAddress1");
23ipAddress.add("ipAddress2");
24ipAddress.add("ipAddress3");
25condition.setIpAddress(ipAddress);
26 // Set refer stringLike
27stringLike.add("http://www.example1.com/");
28stringLike.add("http://www.example2.com/");
29stringLike.add("http://www.example3.com/");
30referer.setStringLike(stringLike);
31condition.setReferer(referer);
32 // Set refer stringEquals
33stringEquals.add("http://www.baidu.com");
34stringEquals.add("http://www.xiaomi.com");
35stringEquals.add("http://www.google.com");
36referer.setStringEquals(stringEquals);
37condition.setReferer(referer);
38 // Set resource
39resource.add("yourBucketName");
40 // Set notResource
41List<String> notResouce = new ArrayList<String>();
42notResouce.add("yourBucketName");
43notResouce.add("yourBucketName/*");
44Grant grant = new Grant();
45grant.setGrantee(grantees);
46grant.setPermission(permissions);
47grant.setCondition(condition);
48grant.setResource(resource);
49List<Grantee> grantees1 = new ArrayList<Grantee>();
50List<Permission> permissions1 = new ArrayList<Permission>();
51List<String> ipAddress1 = new ArrayList<String>();
52List<String> stringLike1 = new ArrayList<String>();
53List<String> stringEquals1 = new ArrayList<String>();
54List<String> resource1 = new ArrayList<String>();
55List<String> notResource1 = new ArrayList<String>();
56Referer referer1 = new Referer();
57Condition condition1 = new Condition();
58 // Grant permission to specific user
59grantees1.add(new Grantee("user_id4"));
60grantees1.add(new Grantee("user_id5"));
61grantees1.add(new Grantee("user_id6"));
62 //Grant permission to Everyone
63grantees.add(new Grantee("*"));
64 //Set permissions
65permissions.add(Permission.FULL_CONTROL);
66permissions1.add(Permission.WRITE);
67permissions1.add(Permission.READ);
68permissions1.add(Permission.LIST);
69 // Set ip
70ipAddress1.add("ipAddress4");
71ipAddress1.add("ipAddress5");
72ipAddress1.add("ipAddress6");
73condition1.setIpAddress(ipAddress1);
74 // Set refer stringLike
75stringLike1.add("http://www.example4.com/");
76stringLike1.add("http://www.example5.com/");
77stringLike1.add("http://www.example6.com/");
78referer1.setStringLike(stringLike1);
79condition1.setReferer(referer1);
80 // Set refer stringEquals
81stringEquals1.add("http://www.baidu1.com");
82stringEquals1.add("http://www.xiaomi1.com");
83stringEquals1.add("http://www.google1.com");
84referer1.setStringEquals(stringEquals1);
85condition1.setReferer(referer1);
86 // Set resource
87resource1.add("yourBucketName");
88 // Set notResource
89List<String> notResouce = new ArrayList<String>();
90notResouce.add("yourBucketName");
91notResouce.add("yourBucketName/*");
92Grant grant1 = new Grant();
93grant1.setGrantee(grantees1);
94grant1.setPermission(permissions1);
95grant1.setCondition(condition1);
96grant1.setResource(resource1);
97accessControlList.add(grant);
98accessControlList.add(grant1);
99SetBucketAclRequest request = new SetBucketAclRequest("yourBucketName",accessControlList);
100client.setBucketAcl(request);
Note: resource and notResource cannot be set at the same time The permission settings in Permission include three values:
READ,WRITE,FULL_CONTROL, which correspond to relevant permissions respectively. For details, refer to BOS API Documentation - [Permission Control via Uploading ACL Files](BOS/API Reference/Access control.md#Permission control by uploading ACL files).
Set more bucket access permissions
- Set anti-leech through refer allow list
1String jsonAcl = "";
2client.setBucketAcl("bucketName", jsonAcl)
Among them, jsonAcl is {"accessControlList":["+ "{"grantee":[{"id":"*"}], "+ ""permission":["FULL_CONTROL"], "+ ""condition":{"referer":{"stringEquals":["http://test/index"]}" + "}}]}
- Restrict client IP access, only allow some client IPs to access
1String jsonAcl = "";
2client.setBucketAcl("bucketName", jsonAcl)
Among them, jsonAcl is {\"accessControlList\":["+ "{\"grantee\":[{\"id\":\"*\"}], "+ "\"permission\":[\"FULL_CONTROL\"], "+ "\"condition\":{\"ipAddress\":[\"192.170.0.6\"]" + "}}]}")
Set STS temporary token permissions
For temporary access identities created through STS, administrators can also set specific permissions. For an introduction to STS and how to set temporary permissions, please refer to [Temporary Authorization Access](BOS/API Reference/Access control.md#Temporary authorized access).
For setting STS temporary token permissions using the BOS JAVA SDK, you can refer to [Creating BosClient with STS](BOS/SDK/Java-SDK/Initialization.md#Create a BosClient with STS)
View bucket permissions
The following code can be used to view the bucket permissions:
1GetBucketAclResponse aclResponse = client.getBucketAcl("bucketName");
2System.out.println(aclResponse.getAccessControlList().toString());
The parameters available for calling in the resolution class returned by the getBucketAcl method are as follows:
| Parameters | Description |
|---|---|
| owner | Bucket owner information |
| id | User ID of bucket owner |
| acl | Identify the permission list of the bucket |
| grantee | Identify the grantee |
| -id | Authorized person ID |
| permission | Identify the grantee permissions |
