Search in sources :

Example 1 with SetBucketLifecycleRequest

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

the class BucketOperationsSample method doBucketLifecycleOperations.

private static void doBucketLifecycleOperations() {
    final String ruleId0 = "delete obsoleted files";
    final String matchPrefix0 = "obsoleted/";
    final String ruleId1 = "delete temporary files";
    final String matchPrefix1 = "temporary/";
    SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName);
    request.AddLifecycleRule(new LifecycleRule(ruleId0, matchPrefix0, RuleStatus.Enabled, 3));
    request.AddLifecycleRule(new LifecycleRule(ruleId1, matchPrefix1, RuleStatus.Enabled, parseISO8601Date("2022-10-12T00:00:00.000Z")));
    System.out.println("Setting bucket lifecycle\n");
    client.setBucketLifecycle(request);
    System.out.println("Getting bucket lifecycle:");
    List<LifecycleRule> rules = client.getBucketLifecycle(bucketName);
    LifecycleRule r0 = rules.get(0);
    LifecycleRule r1 = rules.get(1);
    System.out.println("\tRule0: Id=" + r0.getId() + ", Prefix=" + r0.getPrefix() + ", Status=" + r0.getStatus() + ", ExpirationDays=" + r0.getExpirationDays());
    System.out.println("\tRule1: Id=" + r1.getId() + ", Prefix=" + r1.getPrefix() + ", Status=" + r1.getStatus() + ", ExpirationTime=" + formatISO8601Date(r1.getExpirationTime()));
    System.out.println();
    System.out.println("Deleting bucket lifecycle\n");
    client.deleteBucketLifecycle(bucketName);
}
Also used : SetBucketLifecycleRequest(com.aliyun.oss.model.SetBucketLifecycleRequest) LifecycleRule(com.aliyun.oss.model.LifecycleRule)

Example 2 with SetBucketLifecycleRequest

use of com.aliyun.oss.model.SetBucketLifecycleRequest 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 3 with SetBucketLifecycleRequest

use of com.aliyun.oss.model.SetBucketLifecycleRequest 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)

Example 4 with SetBucketLifecycleRequest

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

the class SecurityTokenTest method testBucketOperationsWithToken.

@SuppressWarnings("deprecation")
@Test
public void testBucketOperationsWithToken() throws JSONException {
    List<String> actions = new ArrayList<String>();
    actions.add("oss:ListBuckets");
    List<String> resources = new ArrayList<String>();
    resources.add("acs:oss:*:" + STS_USER + ":*");
    // List buckets with security token is not supported
    OSSClient sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.listBuckets();
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Delete bucket if already exists
    final String bucketName = "test-bucket-operations-with-token";
    actions.add("oss:DeleteBucket");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.deleteBucket(bucketName);
    } catch (OSSException oe) {
        Assert.assertEquals(OSSErrorCode.NO_SUCH_BUCKET, oe.getErrorCode());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    waitForCacheExpiration(2);
    // Put bucket with valid security token
    actions.add("oss:PutBucket");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.createBucket(bucketName);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
    }
    // Put unmatched bucket with valid security token
    String unmatchedBucketName = bucketName + DUMMY_SUFFIX;
    try {
        sessionClient.createBucket(unmatchedBucketName);
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.ACCESS_DENIED, e.getErrorCode());
        Assert.assertTrue(e.getMessage().startsWith(SECURITY_TOKEN_ACCESS_DENIED_ERR));
    } finally {
        actions.clear();
        resources.clear();
    }
    // Put bucket with non-existent username && valid security token
    final String nonexistentUser = "non-existent-user";
    actions.add("oss:PutBucket");
    resources.add("acs:oss:*:" + nonexistentUser + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.createBucket(unmatchedBucketName);
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.ACCESS_DENIED, e.getErrorCode());
        Assert.assertTrue(e.getMessage().startsWith(SECURITY_TOKEN_ACCESS_DENIED_ERR));
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Allow anyone to trigger operations start with 'Put'
    final String anyone = "*";
    actions.add("oss:Put*");
    resources.add("acs:oss:*:" + anyone + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.createBucket(bucketName);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Put bucket acl
    actions.add("oss:PutBucketAcl");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
    }
    try {
        sessionClient.getBucketAcl(bucketName);
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.ACCESS_DENIED, e.getErrorCode());
        Assert.assertTrue(e.getMessage().startsWith(SECURITY_TOKEN_ACCESS_DENIED_ERR));
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Get bucket acl
    actions.add("oss:GetBucketAcl");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        AccessControlList returnedAcl = sessionClient.getBucketAcl(bucketName);
        Set<Grant> grants = returnedAcl.getGrants();
        Assert.assertEquals(1, grants.size());
        Grant grant = (Grant) grants.toArray()[0];
        Assert.assertEquals(GroupGrantee.AllUsers, grant.getGrantee());
        Assert.assertEquals(Permission.Read, grant.getPermission());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
    }
    try {
        sessionClient.setBucketAcl(bucketName, CannedAccessControlList.Private);
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.ACCESS_DENIED, e.getErrorCode());
        Assert.assertTrue(e.getMessage().startsWith(SECURITY_TOKEN_ACCESS_DENIED_ERR));
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Put bucket logging
    final String targetPrefix = "bucket-logging-prefix";
    actions.add("oss:PutBucketLogging");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        SetBucketLoggingRequest request = new SetBucketLoggingRequest(bucketName);
        request.setTargetBucket(bucketName);
        request.setTargetPrefix(targetPrefix);
        sessionClient.setBucketLogging(request);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
    }
    try {
        sessionClient.getBucketLogging(bucketName);
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.ACCESS_DENIED, e.getErrorCode());
        Assert.assertTrue(e.getMessage().startsWith(SECURITY_TOKEN_ACCESS_DENIED_ERR));
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Get bucket logging
    actions.add("oss:GetBucketLogging");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        BucketLoggingResult result = sessionClient.getBucketLogging(bucketName);
        Assert.assertEquals(bucketName, result.getTargetBucket());
        Assert.assertEquals(targetPrefix, result.getTargetPrefix());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
    }
    try {
        SetBucketLoggingRequest request = new SetBucketLoggingRequest(bucketName);
        request.setTargetBucket(bucketName);
        request.setTargetPrefix(targetPrefix);
        sessionClient.setBucketLogging(request);
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.ACCESS_DENIED, e.getErrorCode());
        Assert.assertTrue(e.getMessage().startsWith(SECURITY_TOKEN_ACCESS_DENIED_ERR));
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Delete bucket logging
    actions.add("oss:DeleteBucketLogging");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.deleteBucketLogging(bucketName);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Put bucket website
    final String indexDocument = "index.html";
    final String errorDocument = "error.html";
    actions.add("oss:PutBucketWebsite");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(bucketName);
        request.setIndexDocument(indexDocument);
        request.setErrorDocument(errorDocument);
        sessionClient.setBucketWebsite(request);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Put bucket referer
    final String referer0 = "http://www.aliyun.com";
    final String referer1 = "https://www.aliyun.com";
    final String referer2 = "http://www.*.com";
    final String referer3 = "https://www.?.aliyuncs.com";
    actions.add("oss:PutBucketReferer");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        // Set non-empty referer list
        BucketReferer r = new BucketReferer();
        List<String> refererList = new ArrayList<String>();
        refererList.add(referer0);
        refererList.add(referer1);
        refererList.add(referer2);
        refererList.add(referer3);
        r.setRefererList(refererList);
        sessionClient.setBucketReferer(bucketName, r);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Put bucket lifecycle
    final String ruleId0 = "delete obsoleted files";
    final String matchPrefix0 = "obsoleted/";
    final String ruleId1 = "delete temporary files";
    final String matchPrefix1 = "temporary/";
    actions.add("oss:PutBucketLifecycle");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        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")));
        sessionClient.setBucketLifecycle(request);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Put bucket cors
    actions.add("oss:PutBucketCors");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        SetBucketCORSRequest request = new SetBucketCORSRequest(bucketName);
        CORSRule r0 = new CORSRule();
        r0.addAllowdOrigin("http://www.a.com");
        r0.addAllowdOrigin("http://www.b.com");
        r0.addAllowedMethod("GET");
        r0.addAllowedHeader("Authorization");
        r0.addExposeHeader("x-oss-test");
        r0.addExposeHeader("x-oss-test1");
        r0.setMaxAgeSeconds(100);
        request.addCorsRule(r0);
        sessionClient.setBucketCORS(request);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // List objects
    actions.add("oss:ListObjects");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        ObjectListing objectListing = sessionClient.listObjects(bucketName);
        Assert.assertEquals(0, objectListing.getObjectSummaries().size());
        Assert.assertEquals(bucketName, objectListing.getBucketName());
        Assert.assertNull(objectListing.getDelimiter());
        Assert.assertNull(objectListing.getPrefix());
        Assert.assertNull(objectListing.getMarker());
        Assert.assertNull(objectListing.getNextMarker());
        Assert.assertFalse(objectListing.isTruncated());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Cleanup bucket if already exists
    actions.add("oss:DeleteBucket");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.deleteBucket(bucketName);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
}
Also used : CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList) AccessControlList(com.aliyun.oss.model.AccessControlList) Grant(com.aliyun.oss.model.Grant) BucketLoggingResult(com.aliyun.oss.model.BucketLoggingResult) OSSClient(com.aliyun.oss.OSSClient) ArrayList(java.util.ArrayList) CORSRule(com.aliyun.oss.model.SetBucketCORSRequest.CORSRule) OSSException(com.aliyun.oss.OSSException) ObjectListing(com.aliyun.oss.model.ObjectListing) BucketReferer(com.aliyun.oss.model.BucketReferer) LifecycleRule(com.aliyun.oss.model.LifecycleRule) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) SetBucketCORSRequest(com.aliyun.oss.model.SetBucketCORSRequest) SetBucketWebsiteRequest(com.aliyun.oss.model.SetBucketWebsiteRequest) SetBucketLifecycleRequest(com.aliyun.oss.model.SetBucketLifecycleRequest) SetBucketLoggingRequest(com.aliyun.oss.model.SetBucketLoggingRequest) Test(org.junit.Test)

Example 5 with SetBucketLifecycleRequest

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

the class LifecycleConfigTest method testLifecycleConfig.

@Ignore
public void testLifecycleConfig() {
    OSS client = new OSSClientBuilder().build(endpoint, accessId, accessKey);
    try {
        SetBucketLifecycleRequest req = new SetBucketLifecycleRequest(bucketName);
        req.AddLifecycleRule(new LifecycleRule("delete obsoleted files", "obsoleted/", RuleStatus.Enabled, 3));
        req.AddLifecycleRule(new LifecycleRule("delete temporary files", "temporary/", RuleStatus.Enabled, DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z")));
        // set bucket lifecycle
        client.setBucketLifecycle(req);
        // get bucket lifecycle
        List<LifecycleRule> rules = client.getBucketLifecycle(bucketName);
        Assert.assertEquals(rules.size(), 2);
        LifecycleRule r0 = rules.get(0);
        Assert.assertEquals(r0.getId(), "delete obsoleted files");
        Assert.assertEquals(r0.getPrefix(), "obsoleted/");
        Assert.assertEquals(r0.getStatus(), RuleStatus.Enabled);
        Assert.assertEquals(r0.getExpirationDays(), 3);
        LifecycleRule r1 = rules.get(1);
        Assert.assertEquals(r1.getId(), "delete temporary files");
        Assert.assertEquals(r1.getPrefix(), "temporary/");
        Assert.assertEquals(r1.getStatus(), RuleStatus.Enabled);
        Assert.assertEquals(DateUtil.formatIso8601Date(r1.getExpirationTime()), "2022-10-12T00:00:00.000Z");
        // delete bucket lifecycle
        client.deleteBucketLifecycle(bucketName);
        // try to get bucket lifecycle again
        try {
            client.getBucketLifecycle(bucketName);
        } catch (OSSException ex) {
            Assert.assertEquals(OSSErrorCode.NO_SUCH_LIFECYCLE, ex.getErrorCode());
        }
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : SetBucketLifecycleRequest(com.aliyun.oss.model.SetBucketLifecycleRequest) OSSException(com.aliyun.oss.OSSException) LifecycleRule(com.aliyun.oss.model.LifecycleRule) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) OSSException(com.aliyun.oss.OSSException) Ignore(org.junit.Ignore)

Aggregations

LifecycleRule (com.aliyun.oss.model.LifecycleRule)6 SetBucketLifecycleRequest (com.aliyun.oss.model.SetBucketLifecycleRequest)6 OSSException (com.aliyun.oss.OSSException)4 StorageTransition (com.aliyun.oss.model.LifecycleRule.StorageTransition)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 TestUtils.genRandomString (com.aliyun.oss.integrationtests.TestUtils.genRandomString)2 AbortMultipartUpload (com.aliyun.oss.model.LifecycleRule.AbortMultipartUpload)2 OSS (com.aliyun.oss.OSS)1 OSSClient (com.aliyun.oss.OSSClient)1 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)1 AccessControlList (com.aliyun.oss.model.AccessControlList)1 BucketLoggingResult (com.aliyun.oss.model.BucketLoggingResult)1 BucketReferer (com.aliyun.oss.model.BucketReferer)1 CannedAccessControlList (com.aliyun.oss.model.CannedAccessControlList)1 Grant (com.aliyun.oss.model.Grant)1 ObjectListing (com.aliyun.oss.model.ObjectListing)1 SetBucketCORSRequest (com.aliyun.oss.model.SetBucketCORSRequest)1 CORSRule (com.aliyun.oss.model.SetBucketCORSRequest.CORSRule)1 SetBucketLoggingRequest (com.aliyun.oss.model.SetBucketLoggingRequest)1