Search in sources :

Example 6 with BucketVersioningConfiguration

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

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

the class BatchDeleteTest method batchDeleteAllExistFileForVersionEnabled.

@Test
public void batchDeleteAllExistFileForVersionEnabled() throws IOException, InterruptedException {
    if (!judgeUserInfoValid()) {
        return;
    }
    BucketVersioningConfiguration bucketVersioningEnabled = new BucketVersioningConfiguration(BucketVersioningConfiguration.ENABLED);
    cosclient.setBucketVersioningConfiguration(new SetBucketVersioningConfigurationRequest(bucket, bucketVersioningEnabled));
    Thread.sleep(5000L);
    DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucket);
    ArrayList<KeyVersion> keyList = new ArrayList<>();
    long deleteFileCount = 30;
    for (long fileIndex = 0; fileIndex < deleteFileCount; ++fileIndex) {
        File localFile = buildTestFile(fileIndex * 1024);
        String key = "ut/" + localFile.getName();
        PutObjectResult putObjectResult = putObjectFromLocalFile(localFile, key);
        keyList.add(new KeyVersion(key, putObjectResult.getVersionId()));
    }
    deleteObjectsRequest.setKeys(keyList);
    DeleteObjectsResult deleteObjectsResult = cosclient.deleteObjects(deleteObjectsRequest);
    assertEquals(deleteFileCount, deleteObjectsResult.getDeletedObjects().size());
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) KeyVersion(com.qcloud.cos.model.DeleteObjectsRequest.KeyVersion) SetBucketVersioningConfigurationRequest(com.qcloud.cos.model.SetBucketVersioningConfigurationRequest) ArrayList(java.util.ArrayList) BucketVersioningConfiguration(com.qcloud.cos.model.BucketVersioningConfiguration) DeleteObjectsResult(com.qcloud.cos.model.DeleteObjectsResult) File(java.io.File) DeleteObjectsRequest(com.qcloud.cos.model.DeleteObjectsRequest) Test(org.junit.Test)

Example 8 with BucketVersioningConfiguration

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

the class BatchDeleteTest method batchDeletePartExistFileForVersionSuspended.

@Test
public void batchDeletePartExistFileForVersionSuspended() throws IOException, InterruptedException {
    if (!judgeUserInfoValid()) {
        return;
    }
    BucketVersioningConfiguration bucketVersioningEnabled = new BucketVersioningConfiguration(BucketVersioningConfiguration.SUSPENDED);
    cosclient.setBucketVersioningConfiguration(new SetBucketVersioningConfigurationRequest(bucket, bucketVersioningEnabled));
    Thread.sleep(5000L);
    DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucket);
    ArrayList<KeyVersion> keyList = new ArrayList<>();
    long deleteFileCount = 5;
    for (long fileIndex = 0; fileIndex < deleteFileCount; ++fileIndex) {
        File localFile = buildTestFile(fileIndex * 1024);
        String key = "ut/" + localFile.getName();
        PutObjectResult putObjectResult = putObjectFromLocalFile(localFile, key);
        keyList.add(new KeyVersion(key, "null"));
    }
    keyList.add(new KeyVersion("ut/not_exist_key.txt", "null"));
    deleteObjectsRequest.setKeys(keyList);
    try {
        DeleteObjectsResult deleteObjectsResult = cosclient.deleteObjects(deleteObjectsRequest);
        List<DeletedObject> deleteObjectResultArray = deleteObjectsResult.getDeletedObjects();
    } catch (MultiObjectDeleteException mde) {
        List<DeletedObject> deleteObjects = mde.getDeletedObjects();
        List<DeleteError> deleteErrors = mde.getErrors();
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) KeyVersion(com.qcloud.cos.model.DeleteObjectsRequest.KeyVersion) CosClientException(com.qcloud.cos.exception.CosClientException) ArrayList(java.util.ArrayList) DeleteObjectsResult(com.qcloud.cos.model.DeleteObjectsResult) DeleteObjectsRequest(com.qcloud.cos.model.DeleteObjectsRequest) CosServiceException(com.qcloud.cos.exception.CosServiceException) MultiObjectDeleteException(com.qcloud.cos.exception.MultiObjectDeleteException) SetBucketVersioningConfigurationRequest(com.qcloud.cos.model.SetBucketVersioningConfigurationRequest) BucketVersioningConfiguration(com.qcloud.cos.model.BucketVersioningConfiguration) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) DeletedObject(com.qcloud.cos.model.DeleteObjectsResult.DeletedObject) Test(org.junit.Test)

Example 9 with BucketVersioningConfiguration

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

the class BucketReplicationTest method setGetDelBucketReplicationTest.

@Test
public void setGetDelBucketReplicationTest() {
    if (!judgeUserInfoValid()) {
        return;
    }
    BucketVersioningConfiguration bucketVersioningEnabled = new BucketVersioningConfiguration(BucketVersioningConfiguration.ENABLED);
    cosclient.setBucketVersioningConfiguration(new SetBucketVersioningConfigurationRequest(bucket, bucketVersioningEnabled));
    try {
        Thread.sleep(5000L);
    } catch (InterruptedException e) {
        fail(e.toString());
    }
    ReplicationRule replicationRule = new ReplicationRule();
    String prefix = "copy1_folder/";
    replicationRule.setPrefix(prefix);
    replicationRule.setStatus(ReplicationRuleStatus.Enabled);
    ReplicationDestinationConfig replicationDestinationConfig = new ReplicationDestinationConfig();
    String bucketQCS = "qcs:id/0:cos:ap-chengdu:appid/1251668577:chengwus3cd";
    replicationDestinationConfig.setBucketQCS(bucketQCS);
    replicationDestinationConfig.setStorageClass(StorageClass.Standard);
    replicationRule.setDestinationConfig(replicationDestinationConfig);
    BucketReplicationConfiguration bucketReplicationConfiguration = new BucketReplicationConfiguration();
    String ruleName = "qcs::cam::uin/123456789:uin/987654543";
    bucketReplicationConfiguration.setRoleName(ruleName);
    String ruleId = "cctest1";
    replicationRule.setID(ruleId);
    bucketReplicationConfiguration.addRule(replicationRule);
    cosclient.setBucketReplicationConfiguration(bucket, bucketReplicationConfiguration);
    // replication设置后, 立刻获取会需要一段时间
    try {
        Thread.sleep(5000L);
    } catch (InterruptedException e) {
        fail(e.toString());
    }
    BucketReplicationConfiguration replicaConfigGet = cosclient.getBucketReplicationConfiguration(bucket);
    assertEquals(ruleName, replicaConfigGet.getRoleName());
    assertEquals(1, replicaConfigGet.getRules().size());
    ReplicationRule replicationRule2 = replicaConfigGet.getRules().get(0);
    assertEquals(bucketQCS, replicationRule2.getDestinationConfig().getBucketQCS());
    assertEquals(StorageClass.Standard.toString(), replicationRule2.getDestinationConfig().getStorageClass());
    assertEquals(ReplicationRuleStatus.Enabled.toString(), replicationRule2.getStatus());
    assertEquals(prefix, replicationRule2.getPrefix());
    cosclient.deleteBucketReplicationConfiguration(bucket);
}
Also used : ReplicationDestinationConfig(com.qcloud.cos.model.ReplicationDestinationConfig) BucketReplicationConfiguration(com.qcloud.cos.model.BucketReplicationConfiguration) SetBucketVersioningConfigurationRequest(com.qcloud.cos.model.SetBucketVersioningConfigurationRequest) BucketVersioningConfiguration(com.qcloud.cos.model.BucketVersioningConfiguration) ReplicationRule(com.qcloud.cos.model.ReplicationRule) Test(org.junit.Test)

Example 10 with BucketVersioningConfiguration

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

the class PutGetLifeCycleConfigTest method testPutGetDelLifeCycleForBucketWithVersions.

@Ignore
public void testPutGetDelLifeCycleForBucketWithVersions() {
    if (!judgeUserInfoValid()) {
        return;
    }
    BucketVersioningConfiguration bucketVersionConfig = new BucketVersioningConfiguration(BucketVersioningConfiguration.ENABLED);
    SetBucketVersioningConfigurationRequest setBucketVersionReq = new SetBucketVersioningConfigurationRequest(bucket, bucketVersionConfig);
    cosclient.setBucketVersioningConfiguration(setBucketVersionReq);
    List<Rule> rules = new ArrayList<>();
    Rule standardIaRule = new Rule();
    standardIaRule.setId("standard_ia transition for version");
    standardIaRule.setFilter(new LifecycleFilter(new LifecyclePrefixPredicate("standard_ia/")));
    List<Transition> standardIaTransitions = new ArrayList<>();
    Transition standardTransition = new Transition();
    standardTransition.setDays(20);
    standardTransition.setStorageClass(StorageClass.Standard_IA.toString());
    standardIaTransitions.add(standardTransition);
    standardIaRule.setTransitions(standardIaTransitions);
    standardIaRule.setStatus(BucketLifecycleConfiguration.DISABLED);
    standardIaRule.setNoncurrentVersionExpirationInDays(360);
    List<NoncurrentVersionTransition> noncurrentVersionTransitions = new ArrayList<>();
    NoncurrentVersionTransition noncurrentVersionTransition = new NoncurrentVersionTransition();
    noncurrentVersionTransition.setDays(30);
    noncurrentVersionTransition.setStorageClass(StorageClass.Standard_IA);
    standardIaRule.setNoncurrentVersionTransitions(noncurrentVersionTransitions);
    standardIaRule.setExpirationInDays(120);
    rules.add(standardIaRule);
    testPutGetDelRules(rules);
    bucketVersionConfig.setStatus(BucketVersioningConfiguration.SUSPENDED);
    SetBucketVersioningConfigurationRequest closeBucketVersionReq = new SetBucketVersioningConfigurationRequest(bucket, bucketVersionConfig);
    cosclient.setBucketVersioningConfiguration(closeBucketVersionReq);
}
Also used : NoncurrentVersionTransition(com.qcloud.cos.model.BucketLifecycleConfiguration.NoncurrentVersionTransition) LifecycleFilter(com.qcloud.cos.model.lifecycle.LifecycleFilter) LifecyclePrefixPredicate(com.qcloud.cos.model.lifecycle.LifecyclePrefixPredicate) SetBucketVersioningConfigurationRequest(com.qcloud.cos.model.SetBucketVersioningConfigurationRequest) ArrayList(java.util.ArrayList) Transition(com.qcloud.cos.model.BucketLifecycleConfiguration.Transition) NoncurrentVersionTransition(com.qcloud.cos.model.BucketLifecycleConfiguration.NoncurrentVersionTransition) BucketVersioningConfiguration(com.qcloud.cos.model.BucketVersioningConfiguration) Rule(com.qcloud.cos.model.BucketLifecycleConfiguration.Rule) Ignore(org.junit.Ignore)

Aggregations

BucketVersioningConfiguration (com.qcloud.cos.model.BucketVersioningConfiguration)13 Test (org.junit.Test)10 SetBucketVersioningConfigurationRequest (com.qcloud.cos.model.SetBucketVersioningConfigurationRequest)9 CosServiceException (com.qcloud.cos.exception.CosServiceException)4 Bucket (com.qcloud.cos.model.Bucket)3 CreateBucketRequest (com.qcloud.cos.model.CreateBucketRequest)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 COSVersionSummary (com.qcloud.cos.model.COSVersionSummary)2 DeleteObjectsRequest (com.qcloud.cos.model.DeleteObjectsRequest)2 KeyVersion (com.qcloud.cos.model.DeleteObjectsRequest.KeyVersion)2 DeleteObjectsResult (com.qcloud.cos.model.DeleteObjectsResult)2 ListVersionsRequest (com.qcloud.cos.model.ListVersionsRequest)2 PutObjectResult (com.qcloud.cos.model.PutObjectResult)2 VersionListing (com.qcloud.cos.model.VersionListing)2 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 CosClientException (com.qcloud.cos.exception.CosClientException)1