Search in sources :

Example 31 with OSS

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

Example 32 with OSS

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

the class PostPolicyTest method testGenPostPolicy.

@Ignore
public void testGenPostPolicy() {
    OSS client = new OSSClientBuilder().build(endpoint, accessId, accessKey);
    try {
        Date expiration = DateUtil.parseIso8601Date("2015-03-19T03:44:06.476Z");
        PolicyConditions policyConds = new PolicyConditions();
        policyConds.addConditionItem("bucket", bucketName);
        // $ must be escaped with backslash.
        policyConds.addConditionItem(MatchMode.Exact, PolicyConditions.COND_KEY, "user/eric/\\${filename}");
        policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, "user/eric");
        policyConds.addConditionItem(MatchMode.StartWith, "x-oss-meta-tag", "dummy_etag");
        policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 1, 1024);
        String actualPostPolicy = client.generatePostPolicy(expiration, policyConds);
        String expectedPostPolicy = "{\"expiration\":\"2015-03-19T03:44:06.476Z\",\"conditions\":[{\"bucket\":\"oss-test2\"},[\"eq\",\"$key\",\"user/eric/\\${filename}\"],[\"starts-with\",\"$key\",\"user/eric\"],[\"starts-with\",\"$x-oss-meta-tag\",\"dummy_etag\"],[\"content-length-range\",1,1024]]}";
        Assert.assertEquals(expectedPostPolicy, actualPostPolicy);
        byte[] binaryData = actualPostPolicy.getBytes("utf-8");
        String actualEncodedPolicy = BinaryUtil.toBase64String(binaryData);
        String expectedEncodedPolicy = "eyJleHBpcmF0aW9uIjoiMjAxNS0wMy0xOVQwMzo0NDowNi40NzZaIiwiY29uZGl0aW9ucyI6W3siYnVja2V0Ijoib3NzLXRlc3QyIn0sWyJlcSIsIiRrZXkiLCJ1c2VyL2VyaWMvXCR7ZmlsZW5hbWV9Il0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCJ1c2VyL2VyaWMiXSxbInN0YXJ0cy13aXRoIiwiJHgtb3NzLW1ldGEtdGFnIiwiZHVtbXlfZXRhZyJdLFsiY29udGVudC1sZW5ndGgtcmFuZ2UiLDEsMTAyNF1dfQ==";
        Assert.assertEquals(expectedEncodedPolicy, actualEncodedPolicy);
        String actualPostSignature = client.calculatePostSignature(actualPostPolicy);
        String expectedPostSignature = "+fOC13qQyIUqF+T/mSA/So2qEBw=";
        Assert.assertEquals(expectedPostSignature, actualPostSignature);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : PolicyConditions(com.aliyun.oss.model.PolicyConditions) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Date(java.util.Date) Ignore(org.junit.Ignore)

Example 33 with OSS

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

the class OSSClientTest method testProxyHost.

@Test
public void testProxyHost() {
    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    String accessKeyId = "accessKeyId";
    String accessKeySecret = "accessKeySecret";
    ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
    conf.setProxyHost(endpoint);
    conf.setProxyPort(80);
    conf.setProxyUsername("user");
    conf.setProxyPassword("passwd");
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, conf);
    ossClient.shutdown();
}
Also used : ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Example 34 with OSS

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

the class CustomSessionCredentialsProviderTest method testGetCredentialsFromAuthInOss.

@Test
public void testGetCredentialsFromAuthInOss() {
    try {
        CustomSessionCredentialsProvider credentialsProvider = new CustomSessionCredentialsProvider(TestConfig.OSS_AUTH_SERVER_HOST);
        Credentials credentials = credentialsProvider.getCredentials();
        Assert.assertEquals(credentials.getAccessKeyId().length(), 29);
        Assert.assertEquals(credentials.getSecretAccessKey().length(), 44);
        Assert.assertEquals(credentials.getSecurityToken().length(), 516);
        Assert.assertTrue(credentials.useSecurityToken());
        String key = "test.txt";
        String content = "HelloOSS";
        OSS ossClient = new OSSClientBuilder().build(TestConfig.OSS_ENDPOINT, credentials.getAccessKeyId(), credentials.getSecretAccessKey(), credentials.getSecurityToken());
        ossClient.putObject(TestConfig.OSS_BUCKET, key, new ByteArrayInputStream(content.getBytes()));
        ossClient.shutdown();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : CustomSessionCredentialsProvider(com.aliyun.oss.common.auth.CustomSessionCredentialsProvider) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicCredentials(com.aliyun.oss.common.auth.BasicCredentials) Credentials(com.aliyun.oss.common.auth.Credentials) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Example 35 with OSS

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

the class CustomSessionCredentialsProviderTest method testGetCredentialsUseInOss.

@Test
public void testGetCredentialsUseInOss() {
    try {
        CustomSessionCredentialsProvider credentialsProvider = CredentialsProviderFactory.newCustomSessionCredentialsProvider(TestConfig.OSS_AUTH_SERVER_HOST);
        String key = "test.txt";
        String content = "HelloOSS";
        OSS ossClient = new OSSClientBuilder().build(TestConfig.OSS_ENDPOINT, credentialsProvider);
        ossClient.putObject(TestConfig.OSS_BUCKET, key, new ByteArrayInputStream(content.getBytes()));
        Thread.sleep(1000 * 10);
        ossClient.putObject(TestConfig.OSS_BUCKET, key, new ByteArrayInputStream(content.getBytes()));
        ossClient.shutdown();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : CustomSessionCredentialsProvider(com.aliyun.oss.common.auth.CustomSessionCredentialsProvider) ByteArrayInputStream(java.io.ByteArrayInputStream) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Aggregations

OSS (com.aliyun.oss.OSS)50 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)40 Test (org.junit.Test)24 OSSException (com.aliyun.oss.OSSException)19 ByteArrayInputStream (java.io.ByteArrayInputStream)19 ClientException (com.aliyun.oss.ClientException)17 ObjectListing (com.aliyun.oss.model.ObjectListing)10 Date (java.util.Date)10 ClientBuilderConfiguration (com.aliyun.oss.ClientBuilderConfiguration)9 OSSObjectSummary (com.aliyun.oss.model.OSSObjectSummary)9 OSSObject (com.aliyun.oss.model.OSSObject)8 Ignore (org.junit.Ignore)8 File (java.io.File)6 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 CredentialsProvider (com.aliyun.oss.common.auth.CredentialsProvider)5 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)5 Credentials (com.aliyun.oss.common.auth.Credentials)4 URI (java.net.URI)4 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)3