Search in sources :

Example 21 with ClientException

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

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

use of com.aliyun.oss.ClientException in project FredaBlog by yangjinlong86.

the class OssUtils method test.

public static void test() {
    String firstKey = "jason_test_object";
    OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    try {
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_bucket.html?spm=5176.docoss/sdk/java-sdk/init
        if (ossClient.doesBucketExist(bucketName)) {
            System.out.println("您已经创建Bucket:" + bucketName + "。");
        } else {
            System.out.println("您的Bucket不存在,创建Bucket:" + bucketName + "。");
            // 创建Bucket。详细请参看“SDK手册 > Java-SDK > 管理Bucket”。
            // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_bucket.html?spm=5176.docoss/sdk/java-sdk/init
            ossClient.createBucket(bucketName);
        }
        // 查看Bucket信息。详细请参看“SDK手册 > Java-SDK > 管理Bucket”。
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_bucket.html?spm=5176.docoss/sdk/java-sdk/init
        BucketInfo info = ossClient.getBucketInfo(bucketName);
        System.out.println("Bucket " + bucketName + "的信息如下:");
        System.out.println("\t数据中心:" + info.getBucket().getLocation());
        System.out.println("\t创建时间:" + info.getBucket().getCreationDate());
        System.out.println("\t用户标志:" + info.getBucket().getOwner());
        // 把字符串存入OSS,Object的名称为firstKey。详细请参看“SDK手册 > Java-SDK > 上传文件”。
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/upload_object.html?spm=5176.docoss/user_guide/upload_object
        InputStream is = new ByteArrayInputStream("Hello OSS".getBytes());
        ossClient.putObject(bucketName, firstKey, is);
        System.out.println("Object:" + firstKey + "存入OSS成功。");
        // 下载文件。详细请参看“SDK手册 > Java-SDK > 下载文件”。
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/download_object.html?spm=5176.docoss/sdk/java-sdk/manage_object
        OSSObject ossObject = ossClient.getObject(bucketName, firstKey);
        InputStream inputStream = ossObject.getObjectContent();
        StringBuilder objectContent = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        while (true) {
            String line = reader.readLine();
            if (line == null) {
                break;
            }
            objectContent.append(line);
        }
        inputStream.close();
        System.out.println("Object:" + firstKey + "的内容是:" + objectContent);
        // 文件存储入OSS,Object的名称为fileKey。详细请参看“SDK手册 > Java-SDK > 上传文件”。
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/upload_object.html?spm=5176.docoss/user_guide/upload_object
        String fileKey = "README.md";
        ossClient.putObject(bucketName, fileKey, new File("README.md"));
        System.out.println("Object:" + fileKey + "存入OSS成功。");
        // 查看Bucket中的Object。详细请参看“SDK手册 > Java-SDK > 管理文件”。
        // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_object.html?spm=5176.docoss/sdk/java-sdk/manage_bucket
        ObjectListing objectListing = ossClient.listObjects(bucketName);
        List<OSSObjectSummary> objectSummary = objectListing.getObjectSummaries();
        System.out.println("您有以下Object:");
        for (OSSObjectSummary object : objectSummary) {
            System.out.println("\t" + object.getKey());
        }
    // 删除Object。详细请参看“SDK手册 > Java-SDK > 管理文件”。
    // 链接地址是:https://help.aliyun.com/document_detail/oss/sdk/java-sdk/manage_object.html?spm=5176.docoss/sdk/java-sdk/manage_bucket
    } catch (OSSException oe) {
        oe.printStackTrace();
    } catch (ClientException ce) {
        ce.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        ossClient.shutdown();
    }
    logger.info("Completed");
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) OSSClient(com.aliyun.oss.OSSClient) ObjectListing(com.aliyun.oss.model.ObjectListing) OSSException(com.aliyun.oss.OSSException) ClientException(com.aliyun.oss.ClientException) OSSException(com.aliyun.oss.OSSException) OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) BucketInfo(com.aliyun.oss.model.BucketInfo) ClientException(com.aliyun.oss.ClientException)

Example 24 with ClientException

use of com.aliyun.oss.ClientException in project hadoop by apache.

the class AliyunOSSFileSystemStore method retrieve.

/**
   * Retrieve a part of an object.
   *
   * @param key the object name that is being retrieved from the Aliyun OSS.
   * @param byteStart start position.
   * @param byteEnd end position.
   * @return This method returns null if the key is not found.
   */
public InputStream retrieve(String key, long byteStart, long byteEnd) {
    try {
        GetObjectRequest request = new GetObjectRequest(bucketName, key);
        request.setRange(byteStart, byteEnd);
        return ossClient.getObject(request).getObjectContent();
    } catch (OSSException | ClientException e) {
        return null;
    }
}
Also used : OSSException(com.aliyun.oss.OSSException) ClientException(com.aliyun.oss.ClientException) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest)

Example 25 with ClientException

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

ClientException (com.aliyun.oss.ClientException)48 OSSException (com.aliyun.oss.OSSException)27 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 OSS (com.aliyun.oss.OSS)16 IOException (java.io.IOException)12 InputStream (java.io.InputStream)12 Test (org.junit.Test)11 RequestMessage (com.aliyun.oss.common.comm.RequestMessage)9 OSSObject (com.aliyun.oss.model.OSSObject)9 ArrayList (java.util.ArrayList)9 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)8 File (java.io.File)7 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)6 ObjectListing (com.aliyun.oss.model.ObjectListing)5 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)5 ClientConfiguration (com.aliyun.oss.ClientConfiguration)4 ServiceException (com.aliyun.oss.ServiceException)4 ExecutionContext (com.aliyun.oss.common.comm.ExecutionContext)4 ResponseHandler (com.aliyun.oss.common.comm.ResponseHandler)4