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());
}
}
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());
}
}
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");
}
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;
}
}
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();
}
}
Aggregations