Search in sources :

Example 1 with CreateBucketRequest

use of com.qcloud.cos.model.CreateBucketRequest in project jeesuite-libs by vakinge.

the class QcloudProvider method createBucket.

@Override
public void createBucket(String bucketName, boolean isPrivate) {
    bucketName = buildBucketName(bucketName);
    if (cosclient.doesBucketExist(bucketName)) {
        throw new JeesuiteBaseException(406, "bucketName[" + bucketName + "]已存在");
    }
    CreateBucketRequest request = new CreateBucketRequest(bucketName);
    if (isPrivate) {
        request.setCannedAcl(CannedAccessControlList.Private);
    } else {
        request.setCannedAcl(CannedAccessControlList.PublicRead);
    }
    cosclient.createBucket(request);
}
Also used : JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) CreateBucketRequest(com.qcloud.cos.model.CreateBucketRequest)

Example 2 with CreateBucketRequest

use of com.qcloud.cos.model.CreateBucketRequest in project cos-java-sdk-v5 by tencentyun.

the class AbstractCOSClientTest method createBucket.

private static void createBucket() throws Exception {
    try {
        // 避免有查询缓存,导致创建bucket失败
        Thread.sleep(5000L);
        String bucketName = bucket;
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
        Bucket createdBucket = cosclient.createBucket(createBucketRequest);
        assertEquals(bucketName, createdBucket.getName());
        Thread.sleep(5000L);
        assertTrue(cosclient.doesBucketExist(bucketName));
    } catch (CosServiceException cse) {
        fail(cse.toString());
    }
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) Bucket(com.qcloud.cos.model.Bucket) CreateBucketRequest(com.qcloud.cos.model.CreateBucketRequest)

Example 3 with CreateBucketRequest

use of com.qcloud.cos.model.CreateBucketRequest 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 CreateBucketRequest

use of com.qcloud.cos.model.CreateBucketRequest in project cos-java-sdk-v5 by tencentyun.

the class CreateDeleteHeadBucketTest method testCreateDeleteBucketPublicRead.

@Test
public void testCreateDeleteBucketPublicRead() throws Exception {
    if (!judgeUserInfoValid()) {
        return;
    }
    try {
        String bucketName = String.format("java-pubr-%s", appid);
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
        createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);
        Bucket bucket = cosclient.createBucket(createBucketRequest);
        assertEquals(bucketName, bucket.getName());
        cosclient.headBucket(new HeadBucketRequest(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 : HeadBucketRequest(com.qcloud.cos.model.HeadBucketRequest) 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 CreateBucketRequest

use of com.qcloud.cos.model.CreateBucketRequest 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)

Aggregations

CreateBucketRequest (com.qcloud.cos.model.CreateBucketRequest)8 Bucket (com.qcloud.cos.model.Bucket)5 CosServiceException (com.qcloud.cos.exception.CosServiceException)4 Test (org.junit.Test)4 BucketVersioningConfiguration (com.qcloud.cos.model.BucketVersioningConfiguration)3 AccessControlList (com.qcloud.cos.model.AccessControlList)2 CannedAccessControlList (com.qcloud.cos.model.CannedAccessControlList)2 UinGrantee (com.qcloud.cos.model.UinGrantee)2 JeesuiteBaseException (com.jeesuite.common.JeesuiteBaseException)1 JeesuiteBaseException (com.mendmix.common.JeesuiteBaseException)1 COSClient (com.qcloud.cos.COSClient)1 ClientConfig (com.qcloud.cos.ClientConfig)1 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)1 COSCredentials (com.qcloud.cos.auth.COSCredentials)1 Grant (com.qcloud.cos.model.Grant)1 Grantee (com.qcloud.cos.model.Grantee)1 GroupGrantee (com.qcloud.cos.model.GroupGrantee)1 HeadBucketRequest (com.qcloud.cos.model.HeadBucketRequest)1 Region (com.qcloud.cos.region.Region)1