use of com.qcloud.cos.model.UinGrantee in project cos-java-sdk-v5 by tencentyun.
the class SetGetBucketAclDemo method setGetBucketAclDemo.
public static void setGetBucketAclDemo() {
// 1 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket名需包含appid
String bucketName = "mybucket-1251668577";
AccessControlList aclGet = cosclient.getBucketAcl(bucketName);
System.out.println("bucket acl:" + aclGet.getOwner());
System.out.println("bucket acl:" + aclGet.getCannedAccessControl());
for (Grant grant : aclGet.getGrantsAsList()) {
System.out.println(grant.getGrantee().getIdentifier());
System.out.println(grant.getGrantee().getTypeIdentifier());
System.out.println(grant.getPermission());
}
// public-read-write bucket acl
// cosclient.setBucketAcl(bucketName, CannedAccessControlList.PublicReadWrite);
// public-read bucket acl
// cosclient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
// private bucket acl
AccessControlList aclSet = new AccessControlList();
Owner owner = new Owner();
owner.setId("qcs::cam::uin/2000000000:uin/2000000000");
aclSet.setOwner(owner);
UinGrantee uinGrantee = new UinGrantee("qcs::cam::uin/100000000000:uin/100000000000");
aclSet.grantPermission(uinGrantee, Permission.Read);
// 设置公有读
aclSet.grantPermission(GroupGrantee.AllUsers, Permission.Read);
cosclient.setBucketAcl(bucketName, aclSet);
cosclient.setBucketAcl(bucketName, CannedAccessControlList.Private);
System.out.println("===========================");
aclGet = cosclient.getBucketAcl(bucketName);
System.out.println("bucket acl:" + aclGet.getOwner());
System.out.println("bucket acl:" + aclGet.getCannedAccessControl());
for (Grant grant : aclGet.getGrantsAsList()) {
System.out.println(grant.getGrantee().getIdentifier());
System.out.println(grant.getGrantee().getTypeIdentifier());
System.out.println(grant.getPermission());
}
}
use of com.qcloud.cos.model.UinGrantee in project cos-java-sdk-v5 by tencentyun.
the class AclTest method setGetObjectAclTest.
@Test
public void setGetObjectAclTest() throws IOException {
if (!judgeUserInfoValid()) {
return;
}
File localFile = buildTestFile(0L);
String key = "ut/acl_test.txt";
putObjectFromLocalFile(localFile, key);
try {
AccessControlList acl = new AccessControlList();
Owner owner = new Owner();
owner.setId("qcs::cam::uin/2779643970:uin/2779643970");
acl.setOwner(owner);
String id = "qcs::cam::uin/2779643970:uin/734505014";
UinGrantee uinGrantee = new UinGrantee(id);
acl.grantPermission(uinGrantee, Permission.FullControl);
cosclient.setObjectAcl(bucket, key, acl);
AccessControlList aclGet = cosclient.getObjectAcl(bucket, key);
List<Grant> grants = aclGet.getGrantsAsList();
assertEquals(1L, grants.size());
assertEquals(id, grants.get(0).getGrantee().getIdentifier());
assertEquals(Permission.FullControl.toString(), grants.get(0).getPermission().toString());
} finally {
assertTrue(localFile.delete());
clearObject(key);
}
}
use of com.qcloud.cos.model.UinGrantee in project cos-java-sdk-v5 by tencentyun.
the class AclTest method GetAclForNewPubReadBucket.
@Test
public void GetAclForNewPubReadBucket() throws InterruptedException {
if (!judgeUserInfoValid()) {
return;
}
String aclTestBucketName = null;
try {
aclTestBucketName = "javasdkacltest-" + appid;
CreateBucketRequest createBucketRequest = new CreateBucketRequest(aclTestBucketName);
createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);
cosclient.createBucket(createBucketRequest);
AccessControlList aclGet = cosclient.getBucketAcl(aclTestBucketName);
assertEquals(aclGet.getCannedAccessControl(), CannedAccessControlList.PublicRead);
assertNotNull(aclGet.getOwner());
assertNotNull(aclGet.getOwner().getId());
assertNotNull(aclGet.getOwner().getDisplayName());
assertEquals(2, aclGet.getGrantsAsList().size());
Grant firstGrant = aclGet.getGrantsAsList().get(0);
assertEquals(Permission.Read.toString(), firstGrant.getPermission().toString());
assertTrue(firstGrant.getGrantee() instanceof GroupGrantee);
Grant secondGrant = aclGet.getGrantsAsList().get(1);
assertEquals(Permission.FullControl.toString(), secondGrant.getPermission().toString());
assertTrue(secondGrant.getGrantee() instanceof UinGrantee);
// set to PublicReadWrite acl and get canned acl compare
Thread.sleep(5000);
cosclient.setBucketAcl(aclTestBucketName, CannedAccessControlList.PublicReadWrite);
aclGet = cosclient.getBucketAcl(aclTestBucketName);
assertEquals(aclGet.getCannedAccessControl(), CannedAccessControlList.PublicReadWrite);
// set to private and get canned acl compare
Thread.sleep(5000);
cosclient.setBucketAcl(aclTestBucketName, CannedAccessControlList.Private);
aclGet = cosclient.getBucketAcl(aclTestBucketName);
assertEquals(aclGet.getCannedAccessControl(), CannedAccessControlList.Private);
} finally {
if (aclTestBucketName != null) {
cosclient.deleteBucket(aclTestBucketName);
aclTestBucketName = null;
}
}
}
use of com.qcloud.cos.model.UinGrantee in project cos-java-sdk-v5 by tencentyun.
the class CreateDeleteHeadBucketTest method testCreateDeleteBucketPublicReadWrite.
@Test
public void testCreateDeleteBucketPublicReadWrite() throws Exception {
if (!judgeUserInfoValid()) {
return;
}
try {
String bucketName = String.format("java-pubrw-%s", appid);
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
createBucketRequest.setCannedAcl(CannedAccessControlList.PublicReadWrite);
AccessControlList accessControlList = new AccessControlList();
Grantee grantee = new UinGrantee("730123456");
accessControlList.grantPermission(grantee, Permission.Write);
createBucketRequest.setAccessControlList(accessControlList);
Bucket bucket = cosclient.createBucket(createBucketRequest);
assertEquals(bucketName, bucket.getName());
assertTrue(cosclient.doesBucketExist(bucketName));
BucketVersioningConfiguration bucketVersioningConfiguration = cosclient.getBucketVersioningConfiguration(bucketName);
assertEquals(BucketVersioningConfiguration.OFF, bucketVersioningConfiguration.getStatus());
cosclient.deleteBucket(bucketName);
// 删除bucket后, 由于server端有缓存 需要稍后查询, 这里sleep 5 秒
Thread.sleep(5000L);
assertFalse(cosclient.doesBucketExist(bucketName));
} catch (CosServiceException cse) {
fail(cse.toString());
}
}
use of com.qcloud.cos.model.UinGrantee in project cos-java-sdk-v5 by tencentyun.
the class AbstractCOSClientTest method putObjectFromLocalFile.
// 从本地上传
protected static PutObjectResult putObjectFromLocalFile(File localFile, String key, SSECustomerKey sseCKey, SSECOSKeyManagementParams params) {
if (!judgeUserInfoValid()) {
return null;
}
AccessControlList acl = new AccessControlList();
UinGrantee uinGrantee = new UinGrantee("734000014");
acl.grantPermission(uinGrantee, Permission.Read);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
putObjectRequest.setStorageClass(StorageClass.Standard_IA);
if (sseCKey != null) {
putObjectRequest.setSSECustomerKey(sseCKey);
}
if (params != null) {
putObjectRequest.setSSECOSKeyManagementParams(params);
}
// putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead);
// putObjectRequest.setAccessControlList(acl);
PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
assertNotNull(putObjectResult.getRequestId());
assertNotNull(putObjectResult.getDateStr());
String etag = putObjectResult.getETag();
String expectEtag = null;
try {
expectEtag = Md5Utils.md5Hex(localFile);
} catch (IOException e) {
fail(e.toString());
}
if (useClientEncryption) {
assertEquals(false, expectEtag.equals(etag));
} else {
assertEquals(true, expectEtag.equals(etag));
}
return putObjectResult;
}
Aggregations