Search in sources :

Example 1 with LifecyclePrefixPredicate

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

the class BucketLifecycleDemo method addLifeCycle.

public void addLifeCycle(String id, String path) {
    List<BucketLifecycleConfiguration.Rule> rules = new ArrayList<BucketLifecycleConfiguration.Rule>();
    // 规则1  30天后删除路径以 hongkong_movie/ 为开始的文件
    BucketLifecycleConfiguration.Rule deletePrefixRule = new BucketLifecycleConfiguration.Rule();
    deletePrefixRule.setId(id);
    deletePrefixRule.setFilter(new LifecycleFilter(new LifecyclePrefixPredicate(path)));
    // 文件上传或者变更后, 30天后删除
    deletePrefixRule.setExpirationInDays(7);
    // 设置规则为生效状态
    deletePrefixRule.setStatus(BucketLifecycleConfiguration.ENABLED);
    // 规则2  20天后沉降到低频,一年后删除
    BucketLifecycleConfiguration.Rule standardIaRule = new BucketLifecycleConfiguration.Rule();
    standardIaRule.setId(id + System.currentTimeMillis());
    standardIaRule.setFilter(new LifecycleFilter(new LifecyclePrefixPredicate(path)));
    List<BucketLifecycleConfiguration.Transition> standardIaTransitions = new ArrayList<BucketLifecycleConfiguration.Transition>();
    BucketLifecycleConfiguration.Transition standardTransition = new BucketLifecycleConfiguration.Transition();
    standardTransition.setDays(20);
    standardTransition.setStorageClass(StorageClass.Standard_IA.toString());
    standardIaTransitions.add(standardTransition);
    standardIaRule.setTransitions(standardIaTransitions);
    standardIaRule.setStatus(BucketLifecycleConfiguration.ENABLED);
    standardIaRule.setExpirationInDays(30);
    // 将两条规则添加到策略集合中
    rules.add(deletePrefixRule);
    // rules.add(standardIaRule);
    // 生成 bucketLifecycleConfiguration
    BucketLifecycleConfiguration bucketLifecycleConfiguration = new BucketLifecycleConfiguration();
    bucketLifecycleConfiguration.setRules(rules);
    // 存储桶的命名格式为 BucketName-APPID
    SetBucketLifecycleConfigurationRequest setBucketLifecycleConfigurationRequest = new SetBucketLifecycleConfigurationRequest(bucketName, bucketLifecycleConfiguration);
    // 设置生命周期
    cosClient.setBucketLifecycleConfiguration(setBucketLifecycleConfigurationRequest);
}
Also used : BucketLifecycleConfiguration(com.qcloud.cos.model.BucketLifecycleConfiguration) LifecycleFilter(com.qcloud.cos.model.lifecycle.LifecycleFilter) LifecyclePrefixPredicate(com.qcloud.cos.model.lifecycle.LifecyclePrefixPredicate) SetBucketLifecycleConfigurationRequest(com.qcloud.cos.model.SetBucketLifecycleConfigurationRequest) ArrayList(java.util.ArrayList)

Example 2 with LifecyclePrefixPredicate

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

the class PutGetLifeCycleConfigTest method testPutGetDelRules.

private void testPutGetDelRules(List<Rule> rules) {
    BucketLifecycleConfiguration bucketLifecycleConfiguration = new BucketLifecycleConfiguration();
    bucketLifecycleConfiguration.setRules(rules);
    SetBucketLifecycleConfigurationRequest setBucketLifecycleConfigurationRequest = new SetBucketLifecycleConfigurationRequest(bucket, bucketLifecycleConfiguration);
    cosclient.setBucketLifecycleConfiguration(setBucketLifecycleConfigurationRequest);
    try {
        // put 后立刻get会存在获取不到的可能
        Thread.sleep(4000L);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    BucketLifecycleConfiguration queryLifeCycleRet = cosclient.getBucketLifecycleConfiguration(bucket);
    List<Rule> ruleLists = queryLifeCycleRet.getRules();
    assertEquals(rules.size(), ruleLists.size());
    for (int ruleIndex = 0; ruleIndex < rules.size(); ++ruleIndex) {
        Rule ruleSet = rules.get(ruleIndex);
        Rule ruleQuery = ruleLists.get(ruleIndex);
        assertEquals(ruleSet.getId(), ruleQuery.getId());
        assertEquals(ruleSet.getExpirationInDays(), ruleQuery.getExpirationInDays());
        if (ruleSet.getFilter() != null) {
            assertTrue(ruleQuery.getFilter() != null);
            LifecyclePrefixPredicate prefixSet = (LifecyclePrefixPredicate) ruleSet.getFilter().getPredicate();
            LifecyclePrefixPredicate prefixGet = (LifecyclePrefixPredicate) ruleSet.getFilter().getPredicate();
            assertEquals(prefixSet.getPrefix(), prefixGet.getPrefix());
        }
    }
    cosclient.deleteBucketLifecycleConfiguration(bucket);
}
Also used : BucketLifecycleConfiguration(com.qcloud.cos.model.BucketLifecycleConfiguration) LifecyclePrefixPredicate(com.qcloud.cos.model.lifecycle.LifecyclePrefixPredicate) SetBucketLifecycleConfigurationRequest(com.qcloud.cos.model.SetBucketLifecycleConfigurationRequest) Rule(com.qcloud.cos.model.BucketLifecycleConfiguration.Rule)

Example 3 with LifecyclePrefixPredicate

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

Example 4 with LifecyclePrefixPredicate

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

the class PutGetLifeCycleConfigTest method testPutGetDelExpirationDateLifeCycle.

@Test
public void testPutGetDelExpirationDateLifeCycle() throws Exception {
    if (!judgeUserInfoValid()) {
        return;
    }
    List<Rule> rules = new ArrayList<>();
    Rule rule = new Rule();
    rule.setId("delete prefix xxxy at a date");
    rule.setFilter(new LifecycleFilter(new LifecyclePrefixPredicate("test/")));
    Calendar c1 = Calendar.getInstance();
    c1.set(2017, 11, 01, 8, 00, 00);
    rule.setExpirationDate(c1.getTime());
    rule.setStatus(BucketLifecycleConfiguration.ENABLED);
    rules.add(rule);
    testPutGetDelRules(rules);
}
Also used : LifecycleFilter(com.qcloud.cos.model.lifecycle.LifecycleFilter) LifecyclePrefixPredicate(com.qcloud.cos.model.lifecycle.LifecyclePrefixPredicate) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Rule(com.qcloud.cos.model.BucketLifecycleConfiguration.Rule) Test(org.junit.Test)

Example 5 with LifecyclePrefixPredicate

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

the class PutGetLifeCycleConfigTest method testPutGetDelLifeCycleForNormalBucket.

@Test
public void testPutGetDelLifeCycleForNormalBucket() {
    if (!judgeUserInfoValid()) {
        return;
    }
    List<Rule> rules = new ArrayList<>();
    // 30天后删除
    Rule deletePrefixRule = new Rule();
    deletePrefixRule.setId("delete prefix xxxy after 30 days");
    deletePrefixRule.setFilter(new LifecycleFilter(new LifecyclePrefixPredicate("delete_prefix/")));
    deletePrefixRule.setExpirationInDays(30);
    deletePrefixRule.setStatus(BucketLifecycleConfiguration.ENABLED);
    // 20天后沉降到低频,一年后删除
    Rule standardIaRule = new Rule();
    standardIaRule.setId("standard_ia transition");
    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.setExpirationInDays(120);
    rules.add(deletePrefixRule);
    rules.add(standardIaRule);
    testPutGetDelRules(rules);
}
Also used : LifecycleFilter(com.qcloud.cos.model.lifecycle.LifecycleFilter) LifecyclePrefixPredicate(com.qcloud.cos.model.lifecycle.LifecyclePrefixPredicate) ArrayList(java.util.ArrayList) Transition(com.qcloud.cos.model.BucketLifecycleConfiguration.Transition) NoncurrentVersionTransition(com.qcloud.cos.model.BucketLifecycleConfiguration.NoncurrentVersionTransition) Rule(com.qcloud.cos.model.BucketLifecycleConfiguration.Rule) Test(org.junit.Test)

Aggregations

LifecyclePrefixPredicate (com.qcloud.cos.model.lifecycle.LifecyclePrefixPredicate)5 Rule (com.qcloud.cos.model.BucketLifecycleConfiguration.Rule)4 LifecycleFilter (com.qcloud.cos.model.lifecycle.LifecycleFilter)4 ArrayList (java.util.ArrayList)4 BucketLifecycleConfiguration (com.qcloud.cos.model.BucketLifecycleConfiguration)2 NoncurrentVersionTransition (com.qcloud.cos.model.BucketLifecycleConfiguration.NoncurrentVersionTransition)2 Transition (com.qcloud.cos.model.BucketLifecycleConfiguration.Transition)2 SetBucketLifecycleConfigurationRequest (com.qcloud.cos.model.SetBucketLifecycleConfigurationRequest)2 Test (org.junit.Test)2 BucketVersioningConfiguration (com.qcloud.cos.model.BucketVersioningConfiguration)1 SetBucketVersioningConfigurationRequest (com.qcloud.cos.model.SetBucketVersioningConfigurationRequest)1 Calendar (java.util.Calendar)1 Ignore (org.junit.Ignore)1