Search in sources :

Example 1 with Bucket

use of com.qcloud.cos.model.Bucket 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 2 with Bucket

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

the class GetServiceTest method testGetServiceForNullRegion.

@Test
public void testGetServiceForNullRegion() {
    if (!judgeUserInfoValid()) {
        return;
    }
    Region oldRegion = clientConfig.getRegion();
    clientConfig.setRegion(null);
    try {
        List<Bucket> buckets = cosclient.listBuckets();
        for (Bucket bucketElement : buckets) {
            if (bucketElement.getName().equals(bucket)) {
                assertEquals(oldRegion.getRegionName(), bucketElement.getLocation());
                return;
            }
        }
        fail("GetService result not contain bucket: " + bucket);
    } finally {
        clientConfig.setRegion(oldRegion);
    }
}
Also used : Bucket(com.qcloud.cos.model.Bucket) Region(com.qcloud.cos.region.Region) Test(org.junit.Test)

Example 3 with Bucket

use of com.qcloud.cos.model.Bucket 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 4 with Bucket

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

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

the class BucketDemo method ListBuckets.

public static void ListBuckets() {
    // 1 初始化用户身份信息(appid, secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "****************************");
    // 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);
    List<Bucket> buckets = cosclient.listBuckets();
    for (Bucket bucket : buckets) {
        System.out.println(bucket.getName());
        System.out.println(bucket.getLocation());
        System.out.println(bucket.getOwner());
        System.out.println(bucket.getType());
        System.out.println(bucket.getBucketType());
    }
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Bucket(com.qcloud.cos.model.Bucket) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Aggregations

Bucket (com.qcloud.cos.model.Bucket)8 CreateBucketRequest (com.qcloud.cos.model.CreateBucketRequest)5 Test (org.junit.Test)5 CosServiceException (com.qcloud.cos.exception.CosServiceException)4 BucketVersioningConfiguration (com.qcloud.cos.model.BucketVersioningConfiguration)3 Region (com.qcloud.cos.region.Region)3 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 AccessControlList (com.qcloud.cos.model.AccessControlList)1 CannedAccessControlList (com.qcloud.cos.model.CannedAccessControlList)1 Grantee (com.qcloud.cos.model.Grantee)1 HeadBucketRequest (com.qcloud.cos.model.HeadBucketRequest)1 UinGrantee (com.qcloud.cos.model.UinGrantee)1