use of com.aliyun.oss.model.DownloadFileRequest 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.model.DownloadFileRequest in project aliyun-oss-java-sdk by aliyun.
the class RequestTimeoutTest method testObjectOperationsNormal.
/**
* Testing normal request.
*/
@Test
public void testObjectOperationsNormal() throws Exception {
String key = "test-object-operation-normal";
try {
// get
ossClient.putObject(bucketName, key, TestUtils.genFixedLengthInputStream(64));
// put
OSSObject ossObject = ossClient.getObject(bucketName, key);
ossObject.getObjectContent().close();
// delete
ossClient.deleteObject(bucketName, key);
// upload
File file = createSampleFile(key, 1024 * 500);
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
uploadFileRequest.setUploadFile(file.getAbsolutePath());
uploadFileRequest.setPartSize(1024 * 100);
uploadFileRequest.setTaskNum(10);
uploadFileRequest.setEnableCheckpoint(true);
ossClient.uploadFile(uploadFileRequest);
// download
DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, key);
downloadFileRequest.setDownloadFile(file.getAbsolutePath());
downloadFileRequest.setTaskNum(10);
downloadFileRequest.setEnableCheckpoint(true);
ossClient.downloadFile(downloadFileRequest);
ossClient.deleteObject(bucketName, key);
file.delete();
} catch (Exception e) {
Assert.fail(e.getMessage());
} catch (Throwable t) {
Assert.fail(t.getMessage());
}
}
use of com.aliyun.oss.model.DownloadFileRequest in project aliyun-oss-java-sdk by aliyun.
the class DownloadFileTest method testUploadFileWithCheckpoint.
@Test
public void testUploadFileWithCheckpoint() {
final String key = "obj-download-file-cp";
try {
File file = createSampleFile(key, 1024 * 500);
// upload file
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
uploadFileRequest.setUploadFile(file.getAbsolutePath());
uploadFileRequest.setTaskNum(10);
uploadFileRequest.setEnableCheckpoint(true);
ObjectMetadata objMetadata = new ObjectMetadata();
objMetadata.addUserMetadata("prop", "propval");
uploadFileRequest.setObjectMetadata(objMetadata);
UploadFileResult uploadRes = ossClient.uploadFile(uploadFileRequest);
Assert.assertEquals(uploadRes.getMultipartUploadResult().getBucketName(), bucketName);
Assert.assertEquals(uploadRes.getMultipartUploadResult().getKey(), key);
// download file
String filePathNew = key + "-new.txt";
DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, key);
downloadFileRequest.setDownloadFile(filePathNew);
downloadFileRequest.setTaskNum(10);
downloadFileRequest.setEnableCheckpoint(true);
DownloadFileResult downloadRes = ossClient.downloadFile(downloadFileRequest);
ObjectMetadata objMeta = downloadRes.getObjectMetadata();
Assert.assertEquals(objMeta.getContentLength(), 102400);
Assert.assertEquals(objMeta.getObjectType(), "Multipart");
Assert.assertEquals(objMeta.getUserMetadata().get("prop"), "propval");
File fileNew = new File(filePathNew);
Assert.assertTrue("comparte file", compareFile(file.getAbsolutePath(), fileNew.getAbsolutePath()));
ossClient.deleteObject(bucketName, key);
fileNew.delete();
} catch (Throwable e) {
e.printStackTrace();
Assert.fail(e.getMessage());
ossClient.deleteBucket(bucketName);
}
}
use of com.aliyun.oss.model.DownloadFileRequest in project aliyun-oss-java-sdk by aliyun.
the class DownloadFileTest method testUploadFileWithoutCheckpoint.
@Test
public void testUploadFileWithoutCheckpoint() {
final String key = "obj-download-file-wcp";
try {
File file = createSampleFile(key, 1024 * 500);
// upload file
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
uploadFileRequest.setUploadFile(file.getAbsolutePath());
uploadFileRequest.setTaskNum(10);
ObjectMetadata objMetadata = new ObjectMetadata();
objMetadata.addUserMetadata("prop", "propval");
uploadFileRequest.setObjectMetadata(objMetadata);
UploadFileResult uploadRes = ossClient.uploadFile(uploadFileRequest);
Assert.assertEquals(uploadRes.getMultipartUploadResult().getBucketName(), bucketName);
Assert.assertEquals(uploadRes.getMultipartUploadResult().getKey(), key);
// download file
String filePathNew = key + "-new.txt";
DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, key);
downloadFileRequest.setDownloadFile(filePathNew);
downloadFileRequest.setTaskNum(10);
DownloadFileResult downloadRes = ossClient.downloadFile(downloadFileRequest);
ObjectMetadata objMeta = downloadRes.getObjectMetadata();
Assert.assertEquals(objMeta.getContentLength(), 102400);
Assert.assertEquals(objMeta.getObjectType(), "Multipart");
Assert.assertEquals(objMeta.getUserMetadata().get("prop"), "propval");
File fileNew = new File(filePathNew);
Assert.assertTrue("comparte file", compareFile(file.getAbsolutePath(), fileNew.getAbsolutePath()));
ossClient.deleteObject(bucketName, key);
fileNew.delete();
} catch (Throwable e) {
e.printStackTrace();
Assert.fail(e.getMessage());
ossClient.deleteBucket(bucketName);
}
}
use of com.aliyun.oss.model.DownloadFileRequest in project aliyun-oss-java-sdk by aliyun.
the class ProgressBarTest method testDownloadFileProgressBar.
@Test
public void testDownloadFileProgressBar() throws Throwable {
final String key = "download-file-progress-bar";
final int instreamLength = 4 * 1024 * 1024;
final String localFile = genFixedLengthFile(instreamLength);
try {
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
uploadFileRequest.setUploadFile(localFile);
uploadFileRequest.setPartSize(1 * 1024 * 1024);
uploadFileRequest.setTaskNum(3);
uploadFileRequest.setProgressListener(new PutObjectProgressListener());
ossClient.uploadFile(uploadFileRequest);
SimplifiedObjectMeta metadata = ossClient.getSimplifiedObjectMeta(bucketName, key);
Assert.assertEquals(instreamLength, metadata.getSize());
DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, key);
downloadFileRequest.setDownloadFile(localFile + "-df.tmp");
downloadFileRequest.setPartSize(1 * 1024 * 1024);
downloadFileRequest.setTaskNum(3);
downloadFileRequest.setProgressListener(new GetObjectProgressListener());
ossClient.downloadFile(downloadFileRequest);
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail(ex.getMessage());
}
}
Aggregations