Bucket权限管理
设置Bucket的访问权限
如下代码将Bucket的权限设置为了private。
1public void setBucketPrivate (BosClient client, String bucketName) {
2 client.setBucketAcl(<bucketName>, CannedAccessControlList.Private);
3}
CannedAccessControlList是枚举类型,包含三个值: Private
、 PublicRead
、 PublicReadWrite
,它们分别对应相关权限。具体内容可以参考BOS API文档 使用CannedAcl方式的权限控制。
设置指定用户对Bucket的访问权限
BOS还可以实现设置指定用户对Bucket的访问权限,参考如下代码实现:
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
12// 授权给特定用户
13grantees.add(new Grantee("user_id1"));
14grantees.add(new Grantee("user_id2"));
15grantees.add(new Grantee("user_id3"));
16
17//授权给Everyone
18grantees.add(new Grantee("*"));
19
20
21//设置权限
22permissions.add(Permission.WRITE);
23permissions.add(Permission.READ);
24permissions.add(Permission.LIST);
25
26// 设置ip
27ipAddress.add("ipAddress1");
28ipAddress.add("ipAddress2");
29ipAddress.add("ipAddress3");
30condition.setIpAddress(ipAddress);
31
32//设置 refer stringLike
33stringLike.add("http://www.example1.com/");
34stringLike.add("http://www.example2.com/");
35stringLike.add("http://www.example3.com/");
36referer.setStringLike(stringLike);
37condition.setReferer(referer);
38
39// 设置 refer stringEquals
40stringEquals.add("http://www.baidu.com");
41stringEquals.add("http://www.xiaomi.com");
42stringEquals.add("http://www.google.com");
43referer.setStringEquals(stringEquals);
44condition.setReferer(referer);
45
46// 设置 resource
47resource.add("yourBucketName");
48
49
50//设置notResource
51List<String> notResouce = new ArrayList<String>();
52notResouce.add("yourBucketName");
53notResouce.add("yourBucketName/*");
54
55Grant grant = new Grant();
56
57grant.setGrantee(grantees);
58grant.setPermission(permissions);
59grant.setCondition(condition);
60grant.setResource(resource);
61
62List<Grantee> grantees1 = new ArrayList<Grantee>();
63List<Permission> permissions1 = new ArrayList<Permission>();
64List<String> ipAddress1 = new ArrayList<String>();
65List<String> stringLike1 = new ArrayList<String>();
66List<String> stringEquals1 = new ArrayList<String>();
67List<String> resource1 = new ArrayList<String>();
68List<String> notResource1 = new ArrayList<String>();
69Referer referer1 = new Referer();
70Condition condition1 = new Condition();
71
72// 授权给特定用户
73grantees1.add(new Grantee("user_id4"));
74grantees1.add(new Grantee("user_id5"));
75grantees1.add(new Grantee("user_id6"));
76
77//授权给Everyone
78grantees.add(new Grantee("*"));
79
80//设置权限
81permissions.add(Permission.FULL_CONTROL);
82permissions1.add(Permission.WRITE);
83permissions1.add(Permission.READ);
84permissions1.add(Permission.LIST);
85
86// 设置ip
87ipAddress1.add("ipAddress4");
88ipAddress1.add("ipAddress5");
89ipAddress1.add("ipAddress6");
90condition1.setIpAddress(ipAddress1);
91
92//设置 refer stringLike
93stringLike1.add("http://www.example4.com/");
94stringLike1.add("http://www.example5.com/");
95stringLike1.add("http://www.example6.com/");
96referer1.setStringLike(stringLike1);
97condition1.setReferer(referer1);
98
99// 设置 refer stringEquals
100stringEquals1.add("http://www.baidu1.com");
101stringEquals1.add("http://www.xiaomi1.com");
102stringEquals1.add("http://www.google1.com");
103referer1.setStringEquals(stringEquals1);
104condition1.setReferer(referer1);
105
106// 设置 resource
107resource1.add("yourBucketName");
108
109// 设置notResource
110List<String> notResouce = new ArrayList<String>();
111notResouce.add("yourBucketName");
112notResouce.add("yourBucketName/*");
113
114Grant grant1 = new Grant();
115
116grant1.setGrantee(grantees1);
117grant1.setPermission(permissions1);
118grant1.setCondition(condition1);
119grant1.setResource(resource1);
120
121accessControlList.add(grant);
122accessControlList.add(grant1);
123
124SetBucketAclRequest request = new SetBucketAclRequest("yourBucketName",accessControlList);
125client.setBucketAcl(request);
注意: resource和notResource不能同时设置 Permission中的权限设置包含三个值:
READ
、WRITE
、FULL_CONTROL
,它们分别对应相关权限。具体内容可以参考BOS API文档 上传ACL文件方式的权限控制。
设置更多Bucket访问权限
- 通过设置refer白名单方式设置防盗链
1String jsonAcl = "";
2client.setBucketAcl("bucketName", jsonAcl)
其中jsonAcl为{\"accessControlList\":["+ "{\"grantee\":[{\"id\":\"*\"}], "+ "\"permission\":[\"FULL_CONTROL\"], "+ "\"condition\":{\"referer\":{\"stringEquals\":[\"http://test/index\"]}" + "}}]}
- 限制客户端IP访问,只允许部分客户端IP访问
1String jsonAcl = "";
2client.setBucketAcl("bucketName", jsonAcl)
其中jsonAcl为{\"accessControlList\":["+ "{\"grantee\":[{\"id\":\"*\"}], "+ "\"permission\":[\"FULL_CONTROL\"], "+ "\"condition\":{\"ipAddress\":[\"192.170.0.6\"]" + "}}]}"}
设置STS临时token权限
对于通过STS方式创建的临时访问身份,管理员也可进行专门的权限设定。 STS的简介及设置临时权限的方式可参见临时授权访问。
使用BOS JAVA SDK设置STS临时token权限可参考使用STS创建BosClient
查看Bucket的权限
如下代码可以查看Bucket的权限:
1GetBucketAclResponse aclResponse = client.getBucketAcl("bucketName");
2System.out.println(aclResponse.getAccessControlList().toString());
getBucketAcl
方法返回的解析类中可供调用的参数有:
参数 | 说明 |
---|---|
owner | Bucket owner信息 |
id | Bucket owner的用户ID |
acl | 标识Bucket的权限列表 |
grantee | 标识被授权人 |
-id | 被授权人ID |
permission | 标识被授权人的权限 |