Search in sources :

Example 11 with ClientBuilderConfiguration

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

the class ClientBuilderTest method testClientBuilderWithAll.

@Test
public void testClientBuilderWithAll() {
    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), config);
        Assert.assertTrue(ossClient.getClientConfiguration().isSupportCname());
        Assert.assertEquals(ossClient.getClientConfiguration().getConnectionTimeout(), 10000);
        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 12 with ClientBuilderConfiguration

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

the class CnameTest method testCnameExcludeList.

@Ignore
@SuppressWarnings("unused")
public void testCnameExcludeList() {
    ClientBuilderConfiguration cc = new ClientBuilderConfiguration();
    // Defalut CNAME Exclude List: [.aliyuncs.com, .aliyun-inc.com, localhost]
    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("localhost"));
    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("localhost"));
    OSS client = new OSSClientBuilder().build("<input your customized host name>", "<input your access id>", "<input your access key>", 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) Ignore(org.junit.Ignore)

Example 13 with ClientBuilderConfiguration

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

the class OSSClientTest method testProxyHost.

@Test
public void testProxyHost() {
    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    String accessKeyId = "accessKeyId";
    String accessKeySecret = "accessKeySecret";
    ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
    conf.setProxyHost(endpoint);
    conf.setProxyPort(80);
    conf.setProxyUsername("user");
    conf.setProxyPassword("passwd");
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, conf);
    ossClient.shutdown();
}
Also used : ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Test(org.junit.Test)

Example 14 with ClientBuilderConfiguration

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

the class MultipartUploadSample method main.

public static void main(String[] args) throws IOException {
    /*
         * Constructs a client instance with your account for accessing OSS
         */
    ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
    conf.setIdleConnectionTime(1000);
    client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, conf);
    try {
        /*
             * Claim a upload id firstly
             */
        String uploadId = claimUploadId();
        System.out.println("Claiming a new upload id " + uploadId + "\n");
        /*
             * Calculate how many parts to be divided
             */
        // 5MB
        final long partSize = 5 * 1024 * 1024L;
        final File sampleFile = createSampleFile();
        long fileLength = sampleFile.length();
        int partCount = (int) (fileLength / partSize);
        if (fileLength % partSize != 0) {
            partCount++;
        }
        if (partCount > 10000) {
            throw new RuntimeException("Total parts count should not exceed 10000");
        } else {
            System.out.println("Total parts count " + partCount + "\n");
        }
        /*
             * Upload multiparts to your bucket
             */
        System.out.println("Begin to upload multiparts to OSS from a file\n");
        for (int i = 0; i < partCount; i++) {
            long startPos = i * partSize;
            long curPartSize = (i + 1 == partCount) ? (fileLength - startPos) : partSize;
            executorService.execute(new PartUploader(sampleFile, startPos, curPartSize, i + 1, uploadId));
        }
        /*
             * Waiting for all parts finished
             */
        executorService.shutdown();
        while (!executorService.isTerminated()) {
            try {
                executorService.awaitTermination(5, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        /*
             * Verify whether all parts are finished
             */
        if (partETags.size() != partCount) {
            throw new IllegalStateException("Upload multiparts fail due to some parts are not finished yet");
        } else {
            System.out.println("Succeed to complete multiparts into an object named " + key + "\n");
        }
        /*
             * View all parts uploaded recently
             */
        listAllParts(uploadId);
        /*
             * Complete to upload multiparts
             */
        completeMultipartUpload(uploadId);
        /*
             * Fetch the object that newly created at the step below.
             */
        System.out.println("Fetching an object");
        client.getObject(new GetObjectRequest(bucketName, key), new File(localFilePath));
    } catch (OSSException oe) {
        System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason.");
        System.out.println("Error Message: " + oe.getErrorCode());
        System.out.println("Error Code:       " + oe.getErrorCode());
        System.out.println("Request ID:      " + oe.getRequestId());
        System.out.println("Host ID:           " + oe.getHostId());
    } catch (ClientException ce) {
        System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network.");
        System.out.println("Error Message: " + ce.getMessage());
    } finally {
        /*
             * Do not forget to shut down the client finally to release all allocated resources.
             */
        if (client != null) {
            client.shutdown();
        }
    }
}
Also used : OSSException(com.aliyun.oss.OSSException) ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) ClientException(com.aliyun.oss.ClientException) File(java.io.File) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder)

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