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