Search in sources :

Example 1 with AbortMultipartUpload

use of com.aliyun.oss.model.LifecycleRule.AbortMultipartUpload in project aliyun-oss-java-sdk by aliyun.

the class BucketLifecycleTest method testNormalSetBucketLifecycle.

@Test
public void testNormalSetBucketLifecycle() throws ParseException {
    final String bucketName = "normal-set-bucket-lifecycle";
    final String ruleId0 = "delete obsoleted files";
    final String matchPrefix0 = "obsoleted0/";
    final String ruleId1 = "delete temporary files";
    final String matchPrefix1 = "temporary0/";
    final String ruleId2 = "delete obsoleted multipart files";
    final String matchPrefix2 = "obsoleted1/";
    final String ruleId3 = "delete temporary multipart files";
    final String matchPrefix3 = "temporary1/";
    final String ruleId4 = "delete temporary files(2)";
    final String matchPrefix4 = "temporary2/";
    final String ruleId5 = "delete temporary files(3)";
    final String matchPrefix5 = "temporary3/";
    try {
        ossClient.createBucket(bucketName);
        SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName);
        request.AddLifecycleRule(new LifecycleRule(ruleId0, matchPrefix0, RuleStatus.Enabled, 3));
        request.AddLifecycleRule(new LifecycleRule(ruleId1, matchPrefix1, RuleStatus.Enabled, DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z")));
        LifecycleRule rule = new LifecycleRule(ruleId2, matchPrefix2, RuleStatus.Enabled, 3);
        LifecycleRule.AbortMultipartUpload abortMultipartUpload = new LifecycleRule.AbortMultipartUpload();
        abortMultipartUpload.setExpirationDays(3);
        rule.setAbortMultipartUpload(abortMultipartUpload);
        request.AddLifecycleRule(rule);
        rule = new LifecycleRule(ruleId3, matchPrefix3, RuleStatus.Enabled, 30);
        abortMultipartUpload = new LifecycleRule.AbortMultipartUpload();
        abortMultipartUpload.setCreatedBeforeDate(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
        rule.setAbortMultipartUpload(abortMultipartUpload);
        List<StorageTransition> storageTransitions = new ArrayList<StorageTransition>();
        StorageTransition storageTransition = new StorageTransition();
        storageTransition.setStorageClass(StorageClass.IA);
        storageTransition.setExpirationDays(10);
        storageTransitions.add(storageTransition);
        storageTransition = new LifecycleRule.StorageTransition();
        storageTransition.setStorageClass(StorageClass.Archive);
        storageTransition.setExpirationDays(20);
        storageTransitions.add(storageTransition);
        rule.setStorageTransition(storageTransitions);
        request.AddLifecycleRule(rule);
        rule = new LifecycleRule(ruleId4, matchPrefix4, RuleStatus.Enabled);
        rule.setCreatedBeforeDate(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
        request.AddLifecycleRule(rule);
        rule = new LifecycleRule(ruleId5, matchPrefix5, RuleStatus.Enabled);
        storageTransition = new LifecycleRule.StorageTransition();
        storageTransition.setStorageClass(StorageClass.Archive);
        storageTransition.setCreatedBeforeDate(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
        storageTransitions = new ArrayList<StorageTransition>();
        storageTransitions.add(storageTransition);
        rule.setStorageTransition(storageTransitions);
        request.AddLifecycleRule(rule);
        ossClient.setBucketLifecycle(request);
        List<LifecycleRule> rules = ossClient.getBucketLifecycle(bucketName);
        Assert.assertEquals(rules.size(), 6);
        LifecycleRule r0 = rules.get(0);
        Assert.assertEquals(r0.getId(), ruleId0);
        Assert.assertEquals(r0.getPrefix(), matchPrefix0);
        Assert.assertEquals(r0.getStatus(), RuleStatus.Enabled);
        Assert.assertEquals(r0.getExpirationDays(), 3);
        Assert.assertTrue(r0.getAbortMultipartUpload() == null);
        LifecycleRule r1 = rules.get(1);
        Assert.assertEquals(r1.getId(), ruleId1);
        Assert.assertEquals(r1.getPrefix(), matchPrefix1);
        Assert.assertEquals(r1.getStatus(), RuleStatus.Enabled);
        Assert.assertEquals(DateUtil.formatIso8601Date(r1.getExpirationTime()), "2022-10-12T00:00:00.000Z");
        Assert.assertTrue(r1.getAbortMultipartUpload() == null);
        LifecycleRule r2 = rules.get(2);
        Assert.assertEquals(r2.getId(), ruleId2);
        Assert.assertEquals(r2.getPrefix(), matchPrefix2);
        Assert.assertEquals(r2.getStatus(), RuleStatus.Enabled);
        Assert.assertEquals(r2.getExpirationDays(), 3);
        Assert.assertNotNull(r2.getAbortMultipartUpload());
        Assert.assertEquals(r2.getAbortMultipartUpload().getExpirationDays(), 3);
        LifecycleRule r3 = rules.get(3);
        Assert.assertEquals(r3.getId(), ruleId3);
        Assert.assertEquals(r3.getPrefix(), matchPrefix3);
        Assert.assertEquals(r3.getStatus(), RuleStatus.Enabled);
        Assert.assertEquals(r3.getExpirationDays(), 30);
        Assert.assertNotNull(r3.getAbortMultipartUpload());
        Assert.assertEquals(DateUtil.formatIso8601Date(r3.getAbortMultipartUpload().getCreatedBeforeDate()), "2022-10-12T00:00:00.000Z");
        Assert.assertTrue(r3.hasStorageTransition());
        Assert.assertTrue(r3.getStorageTransition().get(0).getExpirationDays() == 10);
        Assert.assertEquals(r3.getStorageTransition().get(0).getStorageClass(), StorageClass.IA);
        Assert.assertTrue(r3.getStorageTransition().get(1).getExpirationDays() == 20);
        Assert.assertEquals(r3.getStorageTransition().get(1).getStorageClass(), StorageClass.Archive);
        LifecycleRule r4 = rules.get(4);
        Assert.assertEquals(r4.getId(), ruleId4);
        Assert.assertEquals(r4.getPrefix(), matchPrefix4);
        Assert.assertEquals(r4.getStatus(), RuleStatus.Enabled);
        Assert.assertEquals(DateUtil.formatIso8601Date(r4.getCreatedBeforeDate()), "2022-10-12T00:00:00.000Z");
        Assert.assertTrue(r4.getAbortMultipartUpload() == null);
        LifecycleRule r5 = rules.get(5);
        Assert.assertEquals(r5.getId(), ruleId5);
        Assert.assertEquals(r5.getPrefix(), matchPrefix5);
        Assert.assertEquals(r5.getStatus(), RuleStatus.Enabled);
        Assert.assertFalse(r5.hasCreatedBeforeDate());
        Assert.assertFalse(r5.hasExpirationTime());
        Assert.assertFalse(r5.hasExpirationDays());
        Assert.assertFalse(r5.hasAbortMultipartUpload());
        Assert.assertTrue(r5.hasStorageTransition());
        Assert.assertEquals(DateUtil.formatIso8601Date(r5.getStorageTransition().get(0).getCreatedBeforeDate()), "2022-10-12T00:00:00.000Z");
        Assert.assertEquals(r5.getStorageTransition().get(0).getStorageClass(), StorageClass.Archive);
        // Override existing lifecycle rules
        final String nullRuleId = null;
        request.clearLifecycles();
        request.AddLifecycleRule(new LifecycleRule(nullRuleId, matchPrefix0, RuleStatus.Enabled, 7));
        ossClient.setBucketLifecycle(request);
        waitForCacheExpiration(5);
        rules = ossClient.getBucketLifecycle(bucketName);
        Assert.assertEquals(rules.size(), 1);
        r0 = rules.get(0);
        Assert.assertEquals(matchPrefix0, r0.getPrefix());
        Assert.assertEquals(r0.getStatus(), RuleStatus.Enabled);
        Assert.assertEquals(r0.getExpirationDays(), 7);
        ossClient.deleteBucketLifecycle(bucketName);
        // Try get bucket lifecycle again
        try {
            ossClient.getBucketLifecycle(bucketName);
        } catch (OSSException e) {
            Assert.assertEquals(OSSErrorCode.NO_SUCH_LIFECYCLE, e.getErrorCode());
            Assert.assertTrue(e.getMessage().startsWith(NO_SUCH_LIFECYCLE_ERR));
        }
    } catch (OSSException e) {
        Assert.fail(e.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : SetBucketLifecycleRequest(com.aliyun.oss.model.SetBucketLifecycleRequest) AbortMultipartUpload(com.aliyun.oss.model.LifecycleRule.AbortMultipartUpload) AbortMultipartUpload(com.aliyun.oss.model.LifecycleRule.AbortMultipartUpload) ArrayList(java.util.ArrayList) StorageTransition(com.aliyun.oss.model.LifecycleRule.StorageTransition) StorageTransition(com.aliyun.oss.model.LifecycleRule.StorageTransition) OSSException(com.aliyun.oss.OSSException) TestUtils.genRandomString(com.aliyun.oss.integrationtests.TestUtils.genRandomString) LifecycleRule(com.aliyun.oss.model.LifecycleRule) Test(org.junit.Test)

Example 2 with AbortMultipartUpload

use of com.aliyun.oss.model.LifecycleRule.AbortMultipartUpload in project aliyun-oss-java-sdk by aliyun.

the class BucketLifecycleTest method testUnormalSetBucketLifecycle.

@Test
public void testUnormalSetBucketLifecycle() throws ParseException {
    final String bucketName = "unormal-set-bucket-lifecycle";
    final String ruleId0 = "delete obsoleted files";
    final String matchPrefix0 = "obsoleted/";
    try {
        ossClient.createBucket(bucketName);
        // Set non-existent bucket
        final String nonexistentBucket = "nonexistent-bucket";
        final LifecycleRule r = new LifecycleRule(ruleId0, matchPrefix0, RuleStatus.Enabled, 3);
        try {
            SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(nonexistentBucket);
            request.AddLifecycleRule(r);
            ossClient.setBucketLifecycle(request);
            Assert.fail("Set bucket lifecycle should not be successful");
        } catch (OSSException e) {
            Assert.assertEquals(OSSErrorCode.NO_SUCH_BUCKET, e.getErrorCode());
            Assert.assertTrue(e.getMessage().startsWith(NO_SUCH_BUCKET_ERR));
        }
        // Set bucket without ownership
        final String bucketWithoutOwnership = "oss";
        try {
            SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketWithoutOwnership);
            request.AddLifecycleRule(r);
            ossClient.setBucketLifecycle(request);
            Assert.fail("Set bucket lifecycle should not be successful");
        } catch (OSSException e) {
            Assert.assertEquals(OSSErrorCode.ACCESS_DENIED, e.getErrorCode());
        }
        // Set length of rule id exceeding RULE_ID_MAX_LENGTH(255)
        final String ruleId256 = genRandomString(MAX_RULE_ID_LENGTH + 1);
        try {
            SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName);
            request.AddLifecycleRule(new LifecycleRule(ruleId256, matchPrefix0, RuleStatus.Enabled, 3));
            Assert.fail("Set bucket lifecycle should not be successful");
        } catch (Exception e) {
            Assert.assertTrue(e instanceof IllegalArgumentException);
        }
        // Set size of lifecycle rules exceeding LIFECYCLE_RULE_MAX_LIMIT(1000)
        try {
            SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(nonexistentBucket);
            for (int i = 0; i < (MAX_LIFECYCLE_RULE_LIMIT + 1); i++) {
                request.AddLifecycleRule(r);
            }
            Assert.fail("Set bucket lifecycle should not be successful");
        } catch (Exception e) {
            Assert.assertTrue(e instanceof IllegalArgumentException);
        }
        // Set both rule id and prefix null
        final String nullRuleId = null;
        final String nullMatchPrefix = null;
        try {
            SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName);
            request.AddLifecycleRule(new LifecycleRule(nullRuleId, nullMatchPrefix, RuleStatus.Enabled, 3));
            ossClient.setBucketLifecycle(request);
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
        // Set both expiration day and expiration time
        try {
            SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(nonexistentBucket);
            LifecycleRule invalidRule = new LifecycleRule();
            invalidRule.setId(ruleId0);
            invalidRule.setPrefix(matchPrefix0);
            invalidRule.setStatus(RuleStatus.Enabled);
            invalidRule.setExpirationTime(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
            invalidRule.setExpirationDays(3);
            request.AddLifecycleRule(invalidRule);
            Assert.fail("Set bucket lifecycle should not be successful");
        } catch (Exception e) {
            Assert.assertTrue(e instanceof IllegalArgumentException);
        }
        // Set neither expiration day nor expiration time
        try {
            SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(nonexistentBucket);
            LifecycleRule invalidRule = new LifecycleRule();
            invalidRule.setId(ruleId0);
            invalidRule.setPrefix(matchPrefix0);
            invalidRule.setStatus(RuleStatus.Enabled);
            request.AddLifecycleRule(invalidRule);
            Assert.fail("Set bucket lifecycle should not be successful");
        } catch (Exception e) {
            Assert.assertTrue(e instanceof IllegalArgumentException);
        }
        // With abort multipart upload option
        try {
            SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(nonexistentBucket);
            LifecycleRule invalidRule = new LifecycleRule();
            invalidRule.setId(ruleId0);
            invalidRule.setPrefix(matchPrefix0);
            invalidRule.setStatus(RuleStatus.Enabled);
            invalidRule.setExpirationDays(3);
            LifecycleRule.AbortMultipartUpload abortMultipartUpload = new AbortMultipartUpload();
            abortMultipartUpload.setExpirationDays(3);
            abortMultipartUpload.setCreatedBeforeDate(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
            invalidRule.setAbortMultipartUpload(abortMultipartUpload);
            request.AddLifecycleRule(invalidRule);
            Assert.fail("Set bucket lifecycle should not be successful");
        } catch (Exception e) {
            Assert.assertTrue(e instanceof IllegalArgumentException);
        }
        // With storage transition option
        try {
            SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(nonexistentBucket);
            LifecycleRule invalidRule = new LifecycleRule();
            invalidRule.setId(ruleId0);
            invalidRule.setPrefix(matchPrefix0);
            invalidRule.setStatus(RuleStatus.Enabled);
            invalidRule.setExpirationDays(3);
            LifecycleRule.StorageTransition storageTransition = new StorageTransition();
            storageTransition.setExpirationDays(3);
            storageTransition.setCreatedBeforeDate(DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
            List<StorageTransition> storageTransitions = new ArrayList<StorageTransition>();
            storageTransitions.add(storageTransition);
            invalidRule.setStorageTransition(storageTransitions);
            request.AddLifecycleRule(invalidRule);
            Assert.fail("Set bucket lifecycle should not be successful");
        } catch (Exception e) {
            Assert.assertTrue(e instanceof IllegalArgumentException);
        }
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : StorageTransition(com.aliyun.oss.model.LifecycleRule.StorageTransition) ArrayList(java.util.ArrayList) OSSException(com.aliyun.oss.OSSException) StorageTransition(com.aliyun.oss.model.LifecycleRule.StorageTransition) TestUtils.genRandomString(com.aliyun.oss.integrationtests.TestUtils.genRandomString) LifecycleRule(com.aliyun.oss.model.LifecycleRule) OSSException(com.aliyun.oss.OSSException) ParseException(java.text.ParseException) SetBucketLifecycleRequest(com.aliyun.oss.model.SetBucketLifecycleRequest) AbortMultipartUpload(com.aliyun.oss.model.LifecycleRule.AbortMultipartUpload) AbortMultipartUpload(com.aliyun.oss.model.LifecycleRule.AbortMultipartUpload) Test(org.junit.Test)

Aggregations

OSSException (com.aliyun.oss.OSSException)2 TestUtils.genRandomString (com.aliyun.oss.integrationtests.TestUtils.genRandomString)2 LifecycleRule (com.aliyun.oss.model.LifecycleRule)2 AbortMultipartUpload (com.aliyun.oss.model.LifecycleRule.AbortMultipartUpload)2 StorageTransition (com.aliyun.oss.model.LifecycleRule.StorageTransition)2 SetBucketLifecycleRequest (com.aliyun.oss.model.SetBucketLifecycleRequest)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ParseException (java.text.ParseException)1