Search in sources :

Example 6 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.mendmix.common.JeesuiteBaseException) CreateBucketRequest(com.qcloud.cos.model.CreateBucketRequest)

Example 7 with CreateBucketRequest

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

the class BucketDemo method CreateBucketDemo.

// 创建bucket
public static void CreateBucketDemo() {
    // 1 初始化用户身份信息(appid, 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-beijing-1"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名称, 需包含appid
    String bucketName = "publicreadbucket-1251668577";
    CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
    // 设置bucket的权限为PublicRead(公有读私有写), 其他可选有私有读写, 公有读私有写
    createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);
    Bucket bucket = cosclient.createBucket(createBucketRequest);
    // 关闭客户端
    cosclient.shutdown();
}
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) CreateBucketRequest(com.qcloud.cos.model.CreateBucketRequest) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 8 with CreateBucketRequest

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

the class CreateDeleteHeadBucketTest method testCreateDeleteBucketPrivate.

@Test
public void testCreateDeleteBucketPrivate() throws Exception {
    if (!judgeUserInfoValid()) {
        return;
    }
    try {
        String bucketName = String.format("java-pri-%s", appid);
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
        createBucketRequest.setCannedAcl(CannedAccessControlList.Private);
        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 : 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