Search in sources :

Example 21 with OSSClientBuilder

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

the class DoesObjectExistTest method testUnormalDoesObjectExist.

@Test
public void testUnormalDoesObjectExist() {
    final String nonexistentKey = "test-unormal-does-object-exist";
    // SignatureDoesNotMatch
    OSS client = new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT, TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET + " ");
    try {
        client.doesObjectExist(bucketName, nonexistentKey);
        Assert.fail("Does object exist 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 22 with OSSClientBuilder

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

the class MultiInstanceClientTest method keepUsingAfterClose.

@Test
public void keepUsingAfterClose() {
    final String key = "key0";
    OSS client = new OSSClientBuilder().build(OSS_TEST_ENDPOINT, OSS_TEST_ACCESS_KEY_ID, OSS_TEST_ACCESS_KEY_SECRET);
    InputStream content = TestUtils.genFixedLengthInputStream(128 * 1024);
    client.putObject(bucketName, key, content, null);
    client.shutdown();
    try {
        content = TestUtils.genFixedLengthInputStream(128 * 1024);
        client.putObject(bucketName, key, content, null);
    } catch (ClientException ce) {
        System.out.println(ce.getMessage());
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}
Also used : InputStream(java.io.InputStream) ClientException(com.aliyun.oss.ClientException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) ClientException(com.aliyun.oss.ClientException) Test(org.junit.Test)

Example 23 with OSSClientBuilder

use of com.aliyun.oss.OSSClientBuilder in project alluxio by Alluxio.

the class OSSUnderFileSystem method createInstance.

/**
 * Constructs a new instance of {@link OSSUnderFileSystem}.
 *
 * @param uri the {@link AlluxioURI} for this UFS
 * @param conf the configuration for this UFS
 * @return the created {@link OSSUnderFileSystem} instance
 */
public static OSSUnderFileSystem createInstance(AlluxioURI uri, UnderFileSystemConfiguration conf) throws Exception {
    String bucketName = UnderFileSystemUtils.getBucketName(uri);
    Preconditions.checkArgument(conf.isSet(PropertyKey.OSS_ACCESS_KEY), "Property %s is required to connect to OSS", PropertyKey.OSS_ACCESS_KEY);
    Preconditions.checkArgument(conf.isSet(PropertyKey.OSS_SECRET_KEY), "Property %s is required to connect to OSS", PropertyKey.OSS_SECRET_KEY);
    Preconditions.checkArgument(conf.isSet(PropertyKey.OSS_ENDPOINT_KEY), "Property %s is required to connect to OSS", PropertyKey.OSS_ENDPOINT_KEY);
    String accessId = conf.getString(PropertyKey.OSS_ACCESS_KEY);
    String accessKey = conf.getString(PropertyKey.OSS_SECRET_KEY);
    String endPoint = conf.getString(PropertyKey.OSS_ENDPOINT_KEY);
    ClientBuilderConfiguration ossClientConf = initializeOSSClientConfig(conf);
    OSS ossClient = new OSSClientBuilder().build(endPoint, accessId, accessKey, ossClientConf);
    return new OSSUnderFileSystem(uri, ossClient, bucketName, conf);
}
Also used : ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder)

Example 24 with OSSClientBuilder

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

the class ProxyTest method testProxy.

@Ignore
public void testProxy() {
    String key = "test/test.txt";
    String content = "Hello OSS.";
    try {
        ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
        conf.setProxyHost(TestConfig.PROXY_HOST);
        conf.setProxyPort(TestConfig.PROXY_PORT);
        conf.setProxyUsername(TestConfig.PROXY_USER);
        conf.setProxyPassword(TestConfig.PROXY_PASSWORD);
        OSS ossClient = new OSSClientBuilder().build(TestConfig.OSS_TEST_ENDPOINT, TestConfig.OSS_TEST_ACCESS_KEY_ID, TestConfig.OSS_TEST_ACCESS_KEY_SECRET, conf);
        BucketInfo info = ossClient.getBucketInfo(bucketName);
        Assert.assertEquals(info.getBucket().getName(), bucketName);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(content.getBytes().length);
        ossClient.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()), metadata);
        OSSObject ossObject = ossClient.getObject(bucketName, key);
        InputStream inputStream = ossObject.getObjectContent();
        inputStream.close();
        ossClient.deleteObject(bucketName, 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) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BucketInfo(com.aliyun.oss.model.BucketInfo) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Ignore(org.junit.Ignore)

Example 25 with OSSClientBuilder

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

the class RequestTimeoutTest method testClientConfigIndependent.

/**
 * Testing connection timeout.
 */
@Test
public void testClientConfigIndependent() throws Exception {
    String key = "test-client-config-independent";
    ClientBuilderConfiguration config = new ClientBuilderConfiguration();
    config.setRequestTimeout(requestTimeout);
    config.setRequestTimeoutEnabled(true);
    config.setConnectionTimeout(1);
    OSS client = new OSSClientBuilder().build(endpoint, accessId, accessKey, config);
    try {
        client.putObject(bucketName, key, TestUtils.genFixedLengthInputStream(1024));
        Assert.fail("Put object should not be successful");
    } catch (ClientException e) {
        Assert.assertEquals(ClientErrorCode.CONNECTION_TIMEOUT, e.getErrorCode());
    } finally {
        client.shutdown();
    }
}
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)

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