Search in sources :

Example 1 with SetBucketWebsiteRequest

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

the class BucketOperationsSample method doBucketWebsiteOperations.

private static void doBucketWebsiteOperations() {
    SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(bucketName);
    request.setIndexDocument("inde.html");
    request.setErrorDocument("error.html");
    System.out.println("Setting bucket website\n");
    client.setBucketWebsite(request);
    System.out.println("Getting bucket website:");
    BucketWebsiteResult result = client.getBucketWebsite(bucketName);
    System.out.println("\tIndex document " + result.getIndexDocument() + ", error document=" + result.getErrorDocument() + "\n");
    System.out.println("Deleting bucket website\n");
    client.deleteBucketWebsite(bucketName);
}
Also used : SetBucketWebsiteRequest(com.aliyun.oss.model.SetBucketWebsiteRequest) BucketWebsiteResult(com.aliyun.oss.model.BucketWebsiteResult)

Example 2 with SetBucketWebsiteRequest

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

the class BucketWebsiteTest method testNormalSetBucketWebsiteWithCDNRedirect.

@Test
public void testNormalSetBucketWebsiteWithCDNRedirect() {
    final String bucketName = "normal-set-bucket-website-redirect-cdn";
    final String indexDocument = "index.html";
    try {
        ossClient.createBucket(bucketName);
        // Set RoutingRule
        SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(bucketName);
        RoutingRule rule = new RoutingRule();
        rule.setNumber(1);
        rule.getCondition().setKeyPrefixEquals("~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.AliCDN);
        rule.getRedirect().setHostName("oss.aliyuncs.com");
        rule.getRedirect().setProtocol(RoutingRule.Protocol.Https);
        rule.getRedirect().setReplaceKeyWith("${key}.jpg");
        rule.getRedirect().setHttpRedirectCode(302);
        request.setIndexDocument(indexDocument);
        request.AddRoutingRule(rule);
        ossClient.setBucketWebsite(request);
        waitForCacheExpiration(5);
        BucketWebsiteResult result = ossClient.getBucketWebsite(bucketName);
        Assert.assertEquals(indexDocument, result.getIndexDocument());
        Assert.assertEquals(result.getRoutingRules().size(), 1);
        RoutingRule rr = result.getRoutingRules().get(0);
        Assert.assertEquals(rr.getNumber().intValue(), 1);
        Assert.assertEquals(rr.getCondition().getKeyPrefixEquals(), "~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        Assert.assertEquals(rr.getCondition().getHttpErrorCodeReturnedEquals().intValue(), 404);
        Assert.assertEquals(rr.getRedirect().getRedirectType(), RoutingRule.RedirectType.AliCDN);
        Assert.assertEquals(rr.getRedirect().getHostName(), "oss.aliyuncs.com");
        Assert.assertEquals(rr.getRedirect().getProtocol(), RoutingRule.Protocol.Https);
        Assert.assertEquals(rr.getRedirect().getReplaceKeyWith(), "${key}.jpg");
        Assert.assertEquals(rr.getRedirect().getHttpRedirectCode().intValue(), 302);
        ossClient.deleteBucketWebsite(bucketName);
        // Set RoutingRule
        request = new SetBucketWebsiteRequest(bucketName);
        rule = new RoutingRule();
        rule.setNumber(2);
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.AliCDN);
        rule.getRedirect().setHostName("oss.aliyuncs.com");
        rule.getRedirect().setProtocol(RoutingRule.Protocol.Https);
        rule.getRedirect().setReplaceKeyWith("${key}.jpg");
        rule.getRedirect().setHttpRedirectCode(302);
        request.AddRoutingRule(rule);
        rule = new RoutingRule();
        rule.setNumber(5);
        rule.getCondition().setHttpErrorCodeReturnedEquals(403);
        rule.getRedirect().setHostName("oss.aliyuncs.com");
        rule.getRedirect().setProtocol(RoutingRule.Protocol.Http);
        rule.getRedirect().setReplaceKeyPrefixWith("~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        rule.getRedirect().setHttpRedirectCode(303);
        request.AddRoutingRule(rule);
        request.setIndexDocument(indexDocument);
        ossClient.setBucketWebsite(request);
        waitForCacheExpiration(5);
        result = ossClient.getBucketWebsite(bucketName);
        Assert.assertEquals(indexDocument, result.getIndexDocument());
        Assert.assertEquals(result.getRoutingRules().size(), 2);
        Assert.assertEquals(result.getRequestId().length(), REQUEST_ID_LEN);
        rr = result.getRoutingRules().get(0);
        Assert.assertEquals(rr.getNumber().intValue(), 2);
        Assert.assertEquals(rr.getCondition().getHttpErrorCodeReturnedEquals().intValue(), 404);
        Assert.assertEquals(rr.getRedirect().getRedirectType(), RoutingRule.RedirectType.AliCDN);
        Assert.assertEquals(rr.getRedirect().getHostName(), "oss.aliyuncs.com");
        Assert.assertEquals(rr.getRedirect().getProtocol(), RoutingRule.Protocol.Https);
        Assert.assertEquals(rr.getRedirect().getReplaceKeyWith(), "${key}.jpg");
        Assert.assertEquals(rr.getRedirect().getHttpRedirectCode().intValue(), 302);
        rr = result.getRoutingRules().get(1);
        Assert.assertEquals(rr.getNumber().intValue(), 5);
        Assert.assertEquals(rr.getCondition().getHttpErrorCodeReturnedEquals().intValue(), 403);
        Assert.assertEquals(rr.getRedirect().getRedirectType(), RoutingRule.RedirectType.External);
        Assert.assertEquals(rr.getRedirect().getHostName(), "oss.aliyuncs.com");
        Assert.assertEquals(rr.getRedirect().getProtocol(), RoutingRule.Protocol.Http);
        Assert.assertEquals(rr.getRedirect().getReplaceKeyPrefixWith(), "~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        Assert.assertEquals(rr.getRedirect().getHttpRedirectCode().intValue(), 303);
        ossClient.deleteBucketWebsite(bucketName);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : SetBucketWebsiteRequest(com.aliyun.oss.model.SetBucketWebsiteRequest) BucketWebsiteResult(com.aliyun.oss.model.BucketWebsiteResult) RoutingRule(com.aliyun.oss.model.RoutingRule) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 3 with SetBucketWebsiteRequest

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

the class BucketWebsiteTest method testUnormalSetBucketWebsiteWithRedirect.

@Test
public void testUnormalSetBucketWebsiteWithRedirect() {
    final String bucketName = "unormal-set-bucket-website-redirect";
    final String indexDocument = "index.html";
    try {
        ossClient.createBucket(bucketName);
        SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(bucketName);
        RoutingRule rule = new RoutingRule();
        rule.setNumber(-1);
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.External);
        rule.getRedirect().setMirrorURL("http://oss-test.aliyun-inc.com/mirror-test-source/");
        request.setIndexDocument(indexDocument);
        // rule Number invalid
        try {
            request.AddRoutingRule(rule);
            Assert.fail("Add routing rule should not be successful");
        } catch (Exception e) {
            Assert.assertTrue(e instanceof IllegalArgumentException);
        }
        request = new SetBucketWebsiteRequest(bucketName);
        rule = new RoutingRule();
        rule.setNumber(1);
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.External);
        request.setIndexDocument(indexDocument);
        // rule value invalid
        try {
            request.AddRoutingRule(rule);
            Assert.fail("Add routing rule should not be successful");
        } catch (Exception e) {
            Assert.assertTrue(e instanceof IllegalArgumentException);
        }
        // rule ReplaceKeyPrefixWith&ReplaceKeyWith invalid
        request = new SetBucketWebsiteRequest(bucketName);
        rule = new RoutingRule();
        rule.setNumber(1);
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.External);
        rule.getRedirect().setReplaceKeyWith("");
        rule.getRedirect().setReplaceKeyPrefixWith("");
        request.setIndexDocument(indexDocument);
        // rule value invalid
        try {
            request.AddRoutingRule(rule);
            Assert.fail("Add routing rule should not be successful");
        } catch (Exception e) {
            Assert.assertTrue(e instanceof IllegalArgumentException);
        }
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : SetBucketWebsiteRequest(com.aliyun.oss.model.SetBucketWebsiteRequest) RoutingRule(com.aliyun.oss.model.RoutingRule) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 4 with SetBucketWebsiteRequest

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

the class BucketWebsiteTest method testNormalSetBucketWebsiteWithRedirect.

@Test
public void testNormalSetBucketWebsiteWithRedirect() {
    final String bucketName = "normal-set-bucket-website-redirect";
    final String indexDocument = "index.html";
    try {
        ossClient.createBucket(bucketName);
        // Set RoutingRule
        SetBucketWebsiteRequest request = new SetBucketWebsiteRequest(bucketName);
        RoutingRule rule = new RoutingRule();
        rule.setNumber(1);
        rule.getCondition().setKeyPrefixEquals("~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.External);
        rule.getRedirect().setHostName("oss.aliyuncs.com");
        rule.getRedirect().setProtocol(RoutingRule.Protocol.Https);
        rule.getRedirect().setReplaceKeyWith("${key}.jpg");
        rule.getRedirect().setHttpRedirectCode(302);
        request.setIndexDocument(indexDocument);
        request.AddRoutingRule(rule);
        ossClient.setBucketWebsite(request);
        waitForCacheExpiration(5);
        BucketWebsiteResult result = ossClient.getBucketWebsite(bucketName);
        Assert.assertEquals(indexDocument, result.getIndexDocument());
        Assert.assertEquals(result.getRoutingRules().size(), 1);
        RoutingRule rr = result.getRoutingRules().get(0);
        Assert.assertEquals(rr.getNumber().intValue(), 1);
        Assert.assertEquals(rr.getCondition().getKeyPrefixEquals(), "~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        Assert.assertEquals(rr.getCondition().getHttpErrorCodeReturnedEquals().intValue(), 404);
        Assert.assertEquals(rr.getRedirect().getRedirectType(), RoutingRule.RedirectType.External);
        Assert.assertEquals(rr.getRedirect().getHostName(), "oss.aliyuncs.com");
        Assert.assertEquals(rr.getRedirect().getProtocol(), RoutingRule.Protocol.Https);
        Assert.assertEquals(rr.getRedirect().getReplaceKeyWith(), "${key}.jpg");
        Assert.assertEquals(rr.getRedirect().getHttpRedirectCode().intValue(), 302);
        Assert.assertEquals(result.getRequestId().length(), REQUEST_ID_LEN);
        ossClient.deleteBucketWebsite(bucketName);
        // Set RoutingRule
        request = new SetBucketWebsiteRequest(bucketName);
        rule = new RoutingRule();
        rule.setNumber(2);
        rule.getCondition().setHttpErrorCodeReturnedEquals(404);
        rule.getRedirect().setRedirectType(RoutingRule.RedirectType.External);
        rule.getRedirect().setHostName("oss.aliyuncs.com");
        rule.getRedirect().setProtocol(RoutingRule.Protocol.Https);
        rule.getRedirect().setReplaceKeyWith("${key}.jpg");
        rule.getRedirect().setHttpRedirectCode(302);
        request.AddRoutingRule(rule);
        rule = new RoutingRule();
        rule.setNumber(5);
        rule.getCondition().setHttpErrorCodeReturnedEquals(403);
        rule.getRedirect().setHostName("oss.aliyuncs.com");
        rule.getRedirect().setProtocol(RoutingRule.Protocol.Http);
        rule.getRedirect().setReplaceKeyPrefixWith("~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        rule.getRedirect().setHttpRedirectCode(303);
        request.AddRoutingRule(rule);
        request.setIndexDocument(indexDocument);
        ossClient.setBucketWebsite(request);
        waitForCacheExpiration(5);
        result = ossClient.getBucketWebsite(bucketName);
        Assert.assertEquals(indexDocument, result.getIndexDocument());
        Assert.assertEquals(result.getRoutingRules().size(), 2);
        Assert.assertEquals(result.getRequestId().length(), REQUEST_ID_LEN);
        rr = result.getRoutingRules().get(0);
        Assert.assertEquals(rr.getNumber().intValue(), 2);
        Assert.assertEquals(rr.getCondition().getHttpErrorCodeReturnedEquals().intValue(), 404);
        Assert.assertEquals(rr.getRedirect().getRedirectType(), RoutingRule.RedirectType.External);
        Assert.assertEquals(rr.getRedirect().getHostName(), "oss.aliyuncs.com");
        Assert.assertEquals(rr.getRedirect().getProtocol(), RoutingRule.Protocol.Https);
        Assert.assertEquals(rr.getRedirect().getReplaceKeyWith(), "${key}.jpg");
        Assert.assertEquals(rr.getRedirect().getHttpRedirectCode().intValue(), 302);
        rr = result.getRoutingRules().get(1);
        Assert.assertEquals(rr.getNumber().intValue(), 5);
        Assert.assertEquals(rr.getCondition().getHttpErrorCodeReturnedEquals().intValue(), 403);
        Assert.assertEquals(rr.getRedirect().getRedirectType(), RoutingRule.RedirectType.External);
        Assert.assertEquals(rr.getRedirect().getHostName(), "oss.aliyuncs.com");
        Assert.assertEquals(rr.getRedirect().getProtocol(), RoutingRule.Protocol.Http);
        Assert.assertEquals(rr.getRedirect().getReplaceKeyPrefixWith(), "~!@#$%^&*()-_=+|\\[]{}<>,./?`~");
        Assert.assertEquals(rr.getRedirect().getHttpRedirectCode().intValue(), 303);
        ossClient.deleteBucketWebsite(bucketName);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    } finally {
        ossClient.deleteBucket(bucketName);
    }
}
Also used : SetBucketWebsiteRequest(com.aliyun.oss.model.SetBucketWebsiteRequest) BucketWebsiteResult(com.aliyun.oss.model.BucketWebsiteResult) RoutingRule(com.aliyun.oss.model.RoutingRule) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 5 with SetBucketWebsiteRequest

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

Aggregations

SetBucketWebsiteRequest (com.aliyun.oss.model.SetBucketWebsiteRequest)9 OSSException (com.aliyun.oss.OSSException)8 Test (org.junit.Test)8 BucketWebsiteResult (com.aliyun.oss.model.BucketWebsiteResult)5 RoutingRule (com.aliyun.oss.model.RoutingRule)5 OSSClient (com.aliyun.oss.OSSClient)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 LifecycleRule (com.aliyun.oss.model.LifecycleRule)1 ObjectListing (com.aliyun.oss.model.ObjectListing)1 SetBucketCORSRequest (com.aliyun.oss.model.SetBucketCORSRequest)1 CORSRule (com.aliyun.oss.model.SetBucketCORSRequest.CORSRule)1 SetBucketLifecycleRequest (com.aliyun.oss.model.SetBucketLifecycleRequest)1 SetBucketLoggingRequest (com.aliyun.oss.model.SetBucketLoggingRequest)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 JSONException (org.codehaus.jettison.json.JSONException)1