Search in sources :

Example 16 with OSSClientBuilder

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

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

the class ClientBuilderTest method testClientBuilderWithCredentialProvider.

@Test
public void testClientBuilderWithCredentialProvider() {
    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), null);
        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 : 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 18 with OSSClientBuilder

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

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

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

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