Search in sources :

Example 1 with ClientBuilderConfiguration

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

the class RequestTimeoutTest method testMultiOssClientIndependent.

/**
 * Multiple OSSClient instances test.
 * Each instance should work independently without being impacted by other instances.
 * So one timeout instance should not make other instances timeout.
 */
@Test
public void testMultiOssClientIndependent() throws Exception {
    String key = "test-multi-client-independent";
    try {
        ClientBuilderConfiguration config = new ClientBuilderConfiguration();
        config.setRequestTimeout(1);
        config.setRequestTimeoutEnabled(true);
        config.setMaxConnections(1);
        OSS client = new OSSClientBuilder().build(endpoint, accessId, accessKey, config);
        Thread[] threads = new Thread[10];
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                threads[i] = new TimeoutOperationThread(client, key + i);
            } else {
                threads[i] = new OperationThread(key + i);
            }
        }
        for (int i = 0; i < 10; i++) {
            threads[i].start();
        }
        for (int i = 0; i < 10; i++) {
            threads[i].join();
        }
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) ClientException(com.aliyun.oss.ClientException) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 2 with ClientBuilderConfiguration

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

the class RequestTimeoutTest method testExitNormalAfterTimeout.

/**
 * Testing grace exist after connection timeout
 */
@Test
public void testExitNormalAfterTimeout() throws Exception {
    String key = "test-exit-after-timeout";
    ClientBuilderConfiguration config = new ClientBuilderConfiguration();
    config.setRequestTimeout(requestTimeout);
    config.setRequestTimeoutEnabled(true);
    config.setMaxConnections(1);
    OSS client = new OSSClientBuilder().build(endpoint, accessId, accessKey, config);
    try {
        client.putObject(bucketName, key, TestUtils.genFixedLengthInputStream(1024 * 10));
        Assert.fail("Put object should not be successful");
    } catch (ClientException e) {
        Assert.assertEquals(OSSErrorCode.REQUEST_TIMEOUT, e.getErrorCode());
    }
}
Also used : ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) ClientException(com.aliyun.oss.ClientException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Example 3 with ClientBuilderConfiguration

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

the class ClientBuilderTest method testClientBuilderWithSTS.

@Test
public void testClientBuilderWithSTS() {
    try {
        ClientBuilderConfiguration config = new ClientBuilderConfiguration();
        config.setSupportCname(true);
        config.setConnectionTimeout(10000);
        OSSClient ossClient = (OSSClient) new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT, new DefaultCredentialProvider(TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET, "TOKEN"), config);
        Assert.assertTrue(ossClient.getClientConfiguration().isSupportCname());
        Assert.assertEquals(ossClient.getClientConfiguration().getConnectionTimeout(), 10000);
        Credentials cred = ossClient.getCredentialsProvider().getCredentials();
        Assert.assertEquals(cred.getAccessKeyId(), TestConfig.OSS_TEST_ACCESS_KEY_ID);
        Assert.assertEquals(cred.getSecretAccessKey(), TestConfig.OSS_TEST_ACCESS_KEY_SECRET);
        Assert.assertEquals(cred.getSecurityToken(), "TOKEN");
        Assert.assertTrue(cred.useSecurityToken());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) OSSClient(com.aliyun.oss.OSSClient) DefaultCredentialProvider(com.aliyun.oss.common.auth.DefaultCredentialProvider) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Credentials(com.aliyun.oss.common.auth.Credentials) Test(org.junit.Test)

Example 4 with ClientBuilderConfiguration

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

the class ClientBuilderTest method testClientBuilderWithBuilderConfiguration.

@Test
public void testClientBuilderWithBuilderConfiguration() {
    try {
        OSSClient ossClient = (OSSClient) new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT, new DefaultCredentialProvider(TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET), new ClientBuilderConfiguration());
        Assert.assertFalse(ossClient.getClientConfiguration().isSupportCname());
        BucketInfo info = ossClient.getBucketInfo(bucketName);
        Assert.assertEquals(info.getBucket().getName(), bucketName);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(TEST_CONTENT.getBytes().length);
        ossClient.putObject(bucketName, TEST_KEY, new ByteArrayInputStream(TEST_CONTENT.getBytes()), metadata);
        OSSObject ossObject = ossClient.getObject(bucketName, TEST_KEY);
        InputStream inputStream = ossObject.getObjectContent();
        inputStream.close();
        ossClient.deleteObject(bucketName, TEST_KEY);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) OSSObject(com.aliyun.oss.model.OSSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) OSSClient(com.aliyun.oss.OSSClient) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DefaultCredentialProvider(com.aliyun.oss.common.auth.DefaultCredentialProvider) BucketInfo(com.aliyun.oss.model.BucketInfo) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Example 5 with ClientBuilderConfiguration

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

the class CnameTest method testCnameExcludeList.

@Test
@SuppressWarnings("unused")
public void testCnameExcludeList() {
    ClientBuilderConfiguration cc = new ClientBuilderConfiguration();
    // Defalut CNAME Exclude List: [aliyuncs.com, aliyun-inc.com, aliyun.com]
    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("aliyun.com"));
    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("aliyun.com"));
    OSS client = new OSSClientBuilder().build(OSS_TEST_ENDPOINT, OSS_TEST_ACCESS_KEY_ID, OSS_TEST_ACCESS_KEY_SECRET, 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) Test(org.junit.Test)

Aggregations

ClientBuilderConfiguration (com.aliyun.oss.ClientBuilderConfiguration)14 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)13 OSS (com.aliyun.oss.OSS)9 Test (org.junit.Test)8 ClientException (com.aliyun.oss.ClientException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 OSSClient (com.aliyun.oss.OSSClient)3 DefaultCredentialProvider (com.aliyun.oss.common.auth.DefaultCredentialProvider)3 BucketInfo (com.aliyun.oss.model.BucketInfo)3 OSSObject (com.aliyun.oss.model.OSSObject)3 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)3 InputStream (java.io.InputStream)3 Ignore (org.junit.Ignore)3 OSSException (com.aliyun.oss.OSSException)2 Credentials (com.aliyun.oss.common.auth.Credentials)2 ArrayList (java.util.ArrayList)2 RequestSigner (com.aliyun.oss.common.auth.RequestSigner)1 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)1 File (java.io.File)1 LinkedHashMap (java.util.LinkedHashMap)1