Search in sources :

Example 31 with OSSClientBuilder

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

the class GetSimplifiedObjectMetaTest method testUnormalGetSimplifiedObjectMeta.

@Test
public void testUnormalGetSimplifiedObjectMeta() throws Exception {
    // Try to get simplified object meta under nonexistent bucket
    final String key = "unormal-get-simplified-object-meta";
    final String nonexistentBucket = "nonexistent-bukcet";
    try {
        ossClient.getSimplifiedObjectMeta(nonexistentBucket, key);
        Assert.fail("Get simplified object meta should not be successful");
    } catch (OSSException ex) {
        Assert.assertEquals(OSSErrorCode.NO_SUCH_BUCKET, ex.getErrorCode());
        Assert.assertTrue(ex.getMessage().startsWith(NO_SUCH_BUCKET_ERR));
    }
    // Try to get nonexistent object
    final String nonexistentKey = "nonexistent-object";
    try {
        ossClient.getSimplifiedObjectMeta(bucketName, nonexistentKey);
        Assert.fail("Get simplified object meta should not be successful");
    } catch (OSSException ex) {
        Assert.assertEquals(OSSErrorCode.NO_SUCH_KEY, ex.getErrorCode());
        Assert.assertTrue(ex.getMessage().startsWith(NO_SUCH_KEY_ERR));
    }
    // SignatureDoesNotMatch
    OSS client = new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT, TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET + " ");
    try {
        client.getSimplifiedObjectMeta(bucketName, nonexistentKey);
        Assert.fail("Get simplified object meta should not be successful");
    } catch (OSSException ex) {
        Assert.assertEquals(OSSErrorCode.SIGNATURE_DOES_NOT_MATCH, ex.getErrorCode());
    } finally {
        client.shutdown();
    }
}
Also used : OSSException(com.aliyun.oss.OSSException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Example 32 with OSSClientBuilder

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

the class CnameTest method testCnameExcludeList.

@Ignore
@SuppressWarnings("unused")
public void testCnameExcludeList() {
    ClientBuilderConfiguration cc = new ClientBuilderConfiguration();
    // Defalut CNAME Exclude List: [.aliyuncs.com, .aliyun-inc.com, localhost]
    List<String> currentExcludeList = cc.getCnameExcludeList();
    Assert.assertEquals(currentExcludeList.size(), 3);
    Assert.assertTrue(currentExcludeList.contains(".aliyuncs.com"));
    Assert.assertTrue(currentExcludeList.contains(".aliyun-inc.com"));
    Assert.assertTrue(currentExcludeList.contains("localhost"));
    List<String> cnameExcludeList = new ArrayList<String>();
    String excludeItem = "http://oss-cn-hangzhou.aliyuncs.gd";
    // Add your customized host name here
    cnameExcludeList.add(excludeItem);
    cc.setCnameExcludeList(cnameExcludeList);
    currentExcludeList = cc.getCnameExcludeList();
    Assert.assertEquals(currentExcludeList.size(), 4);
    Assert.assertTrue(currentExcludeList.contains(excludeItem));
    Assert.assertTrue(currentExcludeList.contains(".aliyuncs.com"));
    Assert.assertTrue(currentExcludeList.contains(".aliyun-inc.com"));
    Assert.assertTrue(currentExcludeList.contains("localhost"));
    OSS client = new OSSClientBuilder().build("<input your customized host name>", "<input your access id>", "<input your access key>", cc);
// Do some operations with client here...
}
Also used : ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) ArrayList(java.util.ArrayList) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Ignore(org.junit.Ignore)

Example 33 with OSSClientBuilder

use of com.aliyun.oss.OSSClientBuilder 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 34 with OSSClientBuilder

use of com.aliyun.oss.OSSClientBuilder 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 35 with OSSClientBuilder

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

Aggregations

OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)48 OSS (com.aliyun.oss.OSS)39 ByteArrayInputStream (java.io.ByteArrayInputStream)23 Test (org.junit.Test)22 ClientException (com.aliyun.oss.ClientException)19 OSSException (com.aliyun.oss.OSSException)19 ClientBuilderConfiguration (com.aliyun.oss.ClientBuilderConfiguration)13 OSSObject (com.aliyun.oss.model.OSSObject)10 InputStream (java.io.InputStream)10 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)9 Ignore (org.junit.Ignore)8 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)7 ArrayList (java.util.ArrayList)7 OSSClient (com.aliyun.oss.OSSClient)5 Credentials (com.aliyun.oss.common.auth.Credentials)5 CredentialsProvider (com.aliyun.oss.common.auth.CredentialsProvider)5 BucketInfo (com.aliyun.oss.model.BucketInfo)5 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)5 File (java.io.File)5 DefaultCredentialProvider (com.aliyun.oss.common.auth.DefaultCredentialProvider)4