Search in sources :

Example 6 with OSSClient

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

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

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

use of com.aliyun.oss.OSSClient 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
   * @return the created {@link OSSUnderFileSystem} instance
   * @throws Exception when a connection to GCS could not be created
   */
public static OSSUnderFileSystem createInstance(AlluxioURI uri) throws Exception {
    String bucketName = uri.getHost();
    Preconditions.checkArgument(Configuration.containsKey(PropertyKey.OSS_ACCESS_KEY), "Property " + PropertyKey.OSS_ACCESS_KEY + " is required to connect to OSS");
    Preconditions.checkArgument(Configuration.containsKey(PropertyKey.OSS_SECRET_KEY), "Property " + PropertyKey.OSS_SECRET_KEY + " is required to connect to OSS");
    Preconditions.checkArgument(Configuration.containsKey(PropertyKey.OSS_ENDPOINT_KEY), "Property " + PropertyKey.OSS_ENDPOINT_KEY + " is required to connect to OSS");
    String accessId = Configuration.get(PropertyKey.OSS_ACCESS_KEY);
    String accessKey = Configuration.get(PropertyKey.OSS_SECRET_KEY);
    String endPoint = Configuration.get(PropertyKey.OSS_ENDPOINT_KEY);
    ClientConfiguration ossClientConf = initializeOSSClientConfig();
    OSSClient ossClient = new OSSClient(endPoint, accessId, accessKey, ossClientConf);
    return new OSSUnderFileSystem(uri, ossClient, bucketName);
}
Also used : OSSClient(com.aliyun.oss.OSSClient) ClientConfiguration(com.aliyun.oss.ClientConfiguration)

Example 10 with OSSClient

use of com.aliyun.oss.OSSClient in project free-framework by a601942905git.

the class AliyunOssBucketUtils method createDefaultBucketName.

/**
 * 创建bucket
 * @param bucketName    bucket名称
 */
public static Bucket createDefaultBucketName(String bucketName) {
    OSSClient ossClient = AliyunOssUtils.createOssClient();
    Bucket bucket = ossClient.createBucket(bucketName);
    log.info("创建的bucket:{}", bucket);
    AliyunOssUtils.shutDownClient(ossClient);
    return bucket;
}
Also used : Bucket(com.aliyun.oss.model.Bucket) OSSClient(com.aliyun.oss.OSSClient)

Aggregations

OSSClient (com.aliyun.oss.OSSClient)23 Test (org.junit.Test)8 OSSObject (com.aliyun.oss.model.OSSObject)7 ClientConfiguration (com.aliyun.oss.ClientConfiguration)6 DefaultCredentialProvider (com.aliyun.oss.common.auth.DefaultCredentialProvider)6 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)5 BucketInfo (com.aliyun.oss.model.BucketInfo)5 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 OSSException (com.aliyun.oss.OSSException)4 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ClientBuilderConfiguration (com.aliyun.oss.ClientBuilderConfiguration)3 Credentials (com.aliyun.oss.common.auth.Credentials)3 ObjectListing (com.aliyun.oss.model.ObjectListing)3 DefaultCredentials (com.aliyun.oss.common.auth.DefaultCredentials)2 Bucket (com.aliyun.oss.model.Bucket)2 CannedAccessControlList (com.aliyun.oss.model.CannedAccessControlList)2 OSSObjectSummary (com.aliyun.oss.model.OSSObjectSummary)2 IOException (java.io.IOException)2