use of com.aliyun.oss.OSS in project aliyun-oss-java-sdk by aliyun.
the class DownloadSample method main.
public static void main(String[] args) throws IOException {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, key);
// Sets the local file to download to
downloadFileRequest.setDownloadFile(downloadFile);
// Sets the concurrent task thread count 5. By default it's 1.
downloadFileRequest.setTaskNum(5);
// Sets the part size, by default it's 100K.
downloadFileRequest.setPartSize(1024 * 1024 * 1);
// Enable checkpoint. By default it's false.
downloadFileRequest.setEnableCheckpoint(true);
DownloadFileResult downloadResult = ossClient.downloadFile(downloadFileRequest);
ObjectMetadata objectMetadata = downloadResult.getObjectMetadata();
System.out.println(objectMetadata.getETag());
System.out.println(objectMetadata.getLastModified());
System.out.println(objectMetadata.getUserMetadata().get("meta"));
} 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());
} catch (Throwable e) {
e.printStackTrace();
} finally {
ossClient.shutdown();
}
}
use of com.aliyun.oss.OSS in project aliyun-oss-java-sdk by aliyun.
the class ListObjectsSample method main.
public static void main(String[] args) throws IOException {
OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
final String content = "Hello OSS";
final String keyPrefix = "MyObjectKey";
if (!client.doesBucketExist(bucketName)) {
client.createBucket(bucketName);
}
// Prepare the environment---inserting 100 test files.
List<String> keys = new ArrayList<String>();
for (int i = 0; i < 100; i++) {
String key = keyPrefix + i;
InputStream instream = new ByteArrayInputStream(content.getBytes());
client.putObject(bucketName, key, instream);
keys.add(key);
}
System.out.println("Put " + keys.size() + " objects completed.");
ObjectListing objectListing = null;
String nextMarker = null;
final int maxKeys = 30;
List<OSSObjectSummary> sums = null;
// use default parameter to list the files. By default up to 100 entries could be listed.
System.out.println("Default paramters:");
objectListing = client.listObjects(bucketName);
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
// Sets the max keys with 200 (the max value could be 1000).
System.out.println("With max keys:");
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMaxKeys(200));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
// Gets the object with specified prefix. By default it returns up to 100 entries.
System.out.println("With prefix:");
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withPrefix(keyPrefix));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
// Gets the object with specified marker. By default it returns up to 100 entries.
System.out.println("With marker: ");
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMarker(keyPrefix + "11"));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
// Gets all object by paging. Each page will have up to 100 entries.
System.out.println("List all objects:");
nextMarker = null;
do {
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMarker(nextMarker).withMaxKeys(maxKeys));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
nextMarker = objectListing.getNextMarker();
} while (objectListing.isTruncated());
// Gets all object with specified prefix by paging. Each page will have up to 100 entries.
System.out.println("List all objects after marker:");
nextMarker = keyPrefix + "11";
do {
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMarker(nextMarker).withMaxKeys(maxKeys));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
nextMarker = objectListing.getNextMarker();
} while (objectListing.isTruncated());
// Gets all object with specified marker by paging. Each page will have up to 100 entries.
System.out.println("List all objects with prefix:");
nextMarker = null;
do {
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withPrefix(keyPrefix + "1").withMarker(nextMarker).withMaxKeys(maxKeys));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
nextMarker = objectListing.getNextMarker();
} while (objectListing.isTruncated());
// Clean up the environment----deleting the test files.
System.out.println("Deleting all objects:");
DeleteObjectsResult deleteObjectsResult = client.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys));
List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
for (String object : deletedObjects) {
System.out.println("\t" + object);
}
} 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.
*/
client.shutdown();
}
}
use of com.aliyun.oss.OSS in project aliyun-oss-java-sdk by aliyun.
the class RequestTimeoutTest method testMultiOssClientIndependent.
/**
* Multiple OSSClient instances test.
* Each instance should work independently without being impacted by other instances.
* So one timeout instance should not make other instances timeout.
*/
@Test
public void testMultiOssClientIndependent() throws Exception {
String key = "test-multi-client-independent";
try {
ClientBuilderConfiguration config = new ClientBuilderConfiguration();
config.setRequestTimeout(1);
config.setRequestTimeoutEnabled(true);
config.setMaxConnections(1);
OSS client = new OSSClientBuilder().build(endpoint, accessId, accessKey, config);
Thread[] threads = new Thread[10];
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
threads[i] = new TimeoutOperationThread(client, key + i);
} else {
threads[i] = new OperationThread(key + i);
}
}
for (int i = 0; i < 10; i++) {
threads[i].start();
}
for (int i = 0; i < 10; i++) {
threads[i].join();
}
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.OSS 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.OSS in project aliyun-oss-java-sdk by aliyun.
the class CnameTest method testCnameExcludeList.
@Test
@SuppressWarnings("unused")
public void testCnameExcludeList() {
ClientBuilderConfiguration cc = new ClientBuilderConfiguration();
// Defalut CNAME Exclude List: [aliyuncs.com, aliyun-inc.com, aliyun.com]
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("aliyun.com"));
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("aliyun.com"));
OSS client = new OSSClientBuilder().build(OSS_TEST_ENDPOINT, OSS_TEST_ACCESS_KEY_ID, OSS_TEST_ACCESS_KEY_SECRET, cc);
// Do some operations with client here...
}
Aggregations