Search in sources :

Example 6 with ClientBuilderConfiguration

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

the class OSSUnderFileSystem method initializeOSSClientConfig.

/**
 * Creates an OSS {@code ClientConfiguration} using an Alluxio Configuration.
 *
 * @return the OSS {@link ClientBuilderConfiguration}
 */
private static ClientBuilderConfiguration initializeOSSClientConfig(AlluxioConfiguration alluxioConf) {
    ClientBuilderConfiguration ossClientConf = new ClientBuilderConfiguration();
    ossClientConf.setConnectionTimeout((int) alluxioConf.getMs(PropertyKey.UNDERFS_OSS_CONNECT_TIMEOUT));
    ossClientConf.setSocketTimeout((int) alluxioConf.getMs(PropertyKey.UNDERFS_OSS_SOCKET_TIMEOUT));
    ossClientConf.setConnectionTTL(alluxioConf.getLong(PropertyKey.UNDERFS_OSS_CONNECT_TTL));
    ossClientConf.setMaxConnections(alluxioConf.getInt(PropertyKey.UNDERFS_OSS_CONNECT_MAX));
    return ossClientConf;
}
Also used : ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration)

Example 7 with ClientBuilderConfiguration

use of com.aliyun.oss.ClientBuilderConfiguration 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 8 with ClientBuilderConfiguration

use of com.aliyun.oss.ClientBuilderConfiguration 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 9 with ClientBuilderConfiguration

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

Example 10 with ClientBuilderConfiguration

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

the class ProxySignTest method testProxyAuth.

@Ignore
public void testProxyAuth() {
    String bucketName = "sdk-test-md-1";
    String key = "mingdi/test.txt";
    String content = "Hello OSS.";
    String proxyHost = "";
    String endpoint = "";
    String accessKeyId = "";
    String secretAccessKey = "";
    try {
        ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
        conf.setProxyHost(proxyHost);
        conf.setProxyPort(8080);
        Credentials credentials = new ProxyCredentials("mingditest");
        ProxyRequestSigner proxySigner = new ProxyRequestSigner(credentials);
        List<RequestSigner> signerHandlers = new LinkedList<RequestSigner>();
        signerHandlers.add(proxySigner);
        conf.setSignerHandlers(signerHandlers);
        Map<String, String> proxyHeaders = new LinkedHashMap<String, String>();
        proxyHeaders.put(HEADER_PROXY_TYPE, "default");
        proxyHeaders.put(HEADER_PROXY_USER, "diff_bucket_sync_test_user_1");
        proxyHeaders.put(HEADER_PROXY_DEST, "cn-qingdao");
        proxyHeaders.put(HEADER_PROXY_DEST_REGION, "cn-qingdao");
        proxyHeaders.put(HEADER_PROXY_REAL_HOST, endpoint);
        conf.setDefaultHeaders(proxyHeaders);
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, secretAccessKey, conf);
        ossClient.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
        ossClient.getObject(bucketName, key);
        ossClient.deleteObject(bucketName, key);
    } catch (Throwable e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : LinkedList(java.util.LinkedList) OSS(com.aliyun.oss.OSS) LinkedHashMap(java.util.LinkedHashMap) ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) ByteArrayInputStream(java.io.ByteArrayInputStream) Credentials(com.aliyun.oss.common.auth.Credentials) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) RequestSigner(com.aliyun.oss.common.auth.RequestSigner) Ignore(org.junit.Ignore)

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