Search in sources :

Example 1 with UinGrantee

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());
    }
}
Also used : COSClient(com.qcloud.cos.COSClient) CannedAccessControlList(com.qcloud.cos.model.CannedAccessControlList) AccessControlList(com.qcloud.cos.model.AccessControlList) UinGrantee(com.qcloud.cos.model.UinGrantee) Grant(com.qcloud.cos.model.Grant) Owner(com.qcloud.cos.model.Owner) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 2 with UinGrantee

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);
    }
}
Also used : CannedAccessControlList(com.qcloud.cos.model.CannedAccessControlList) AccessControlList(com.qcloud.cos.model.AccessControlList) UinGrantee(com.qcloud.cos.model.UinGrantee) Grant(com.qcloud.cos.model.Grant) Owner(com.qcloud.cos.model.Owner) File(java.io.File) Test(org.junit.Test)

Example 3 with UinGrantee

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;
        }
    }
}
Also used : CannedAccessControlList(com.qcloud.cos.model.CannedAccessControlList) AccessControlList(com.qcloud.cos.model.AccessControlList) UinGrantee(com.qcloud.cos.model.UinGrantee) Grant(com.qcloud.cos.model.Grant) GroupGrantee(com.qcloud.cos.model.GroupGrantee) CreateBucketRequest(com.qcloud.cos.model.CreateBucketRequest) Test(org.junit.Test)

Example 4 with UinGrantee

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());
    }
}
Also used : CannedAccessControlList(com.qcloud.cos.model.CannedAccessControlList) AccessControlList(com.qcloud.cos.model.AccessControlList) UinGrantee(com.qcloud.cos.model.UinGrantee) UinGrantee(com.qcloud.cos.model.UinGrantee) Grantee(com.qcloud.cos.model.Grantee) CosServiceException(com.qcloud.cos.exception.CosServiceException) Bucket(com.qcloud.cos.model.Bucket) CreateBucketRequest(com.qcloud.cos.model.CreateBucketRequest) BucketVersioningConfiguration(com.qcloud.cos.model.BucketVersioningConfiguration) Test(org.junit.Test)

Example 5 with UinGrantee

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;
}
Also used : AccessControlList(com.qcloud.cos.model.AccessControlList) UinGrantee(com.qcloud.cos.model.UinGrantee) PutObjectResult(com.qcloud.cos.model.PutObjectResult) IOException(java.io.IOException) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Aggregations

AccessControlList (com.qcloud.cos.model.AccessControlList)7 UinGrantee (com.qcloud.cos.model.UinGrantee)7 CannedAccessControlList (com.qcloud.cos.model.CannedAccessControlList)6 Grant (com.qcloud.cos.model.Grant)5 Owner (com.qcloud.cos.model.Owner)4 Test (org.junit.Test)4 COSClient (com.qcloud.cos.COSClient)2 ClientConfig (com.qcloud.cos.ClientConfig)2 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)2 COSCredentials (com.qcloud.cos.auth.COSCredentials)2 CreateBucketRequest (com.qcloud.cos.model.CreateBucketRequest)2 Region (com.qcloud.cos.region.Region)2 CosServiceException (com.qcloud.cos.exception.CosServiceException)1 Bucket (com.qcloud.cos.model.Bucket)1 BucketVersioningConfiguration (com.qcloud.cos.model.BucketVersioningConfiguration)1 Grantee (com.qcloud.cos.model.Grantee)1 GroupGrantee (com.qcloud.cos.model.GroupGrantee)1 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)1 PutObjectResult (com.qcloud.cos.model.PutObjectResult)1 File (java.io.File)1