Search in sources :

Example 1 with DownloadFileResult

use of com.aliyun.oss.model.DownloadFileResult in project aliyun-oss-java-sdk by aliyun.

the class OSSDownloadOperation method downloadFileWithCheckpoint.

private DownloadFileResult downloadFileWithCheckpoint(DownloadFileRequest downloadFileRequest) throws Throwable {
    DownloadFileResult downloadFileResult = new DownloadFileResult();
    DownloadCheckPoint downloadCheckPoint = new DownloadCheckPoint();
    // checkpoint file.
    if (downloadFileRequest.isEnableCheckpoint()) {
        // re-download again.
        try {
            downloadCheckPoint.load(downloadFileRequest.getCheckpointFile());
        } catch (Exception e) {
            remove(downloadFileRequest.getCheckpointFile());
        }
        // The download checkpoint is corrupted, download again.
        if (!downloadCheckPoint.isValid(objectOperation)) {
            prepare(downloadCheckPoint, downloadFileRequest);
            remove(downloadFileRequest.getCheckpointFile());
        }
    } else {
        // The checkpoint is not enabled, download the file again.
        prepare(downloadCheckPoint, downloadFileRequest);
    }
    // Progress listen starts tracking the progress.
    ProgressListener listener = downloadFileRequest.getProgressListener();
    ProgressPublisher.publishProgress(listener, ProgressEventType.TRANSFER_STARTED_EVENT);
    // Concurrently download parts.
    DownloadResult downloadResult = download(downloadCheckPoint, downloadFileRequest);
    for (PartResult partResult : downloadResult.getPartResults()) {
        if (partResult.isFailed()) {
            ProgressPublisher.publishProgress(listener, ProgressEventType.TRANSFER_PART_FAILED_EVENT);
            throw partResult.getException();
        }
    }
    // Publish the complete status.
    ProgressPublisher.publishProgress(listener, ProgressEventType.TRANSFER_COMPLETED_EVENT);
    // rename the temp file.
    renameTo(downloadFileRequest.getTempDownloadFile(), downloadFileRequest.getDownloadFile());
    // successful download.
    if (downloadFileRequest.isEnableCheckpoint()) {
        remove(downloadFileRequest.getCheckpointFile());
    }
    downloadFileResult.setObjectMetadata(downloadResult.getObjectMetadata());
    return downloadFileResult;
}
Also used : DownloadFileResult(com.aliyun.oss.model.DownloadFileResult) ProgressListener(com.aliyun.oss.event.ProgressListener) LogUtils.logException(com.aliyun.oss.common.utils.LogUtils.logException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with DownloadFileResult

use of com.aliyun.oss.model.DownloadFileResult 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();
    }
}
Also used : DownloadFileResult(com.aliyun.oss.model.DownloadFileResult) OSSException(com.aliyun.oss.OSSException) DownloadFileRequest(com.aliyun.oss.model.DownloadFileRequest) ClientException(com.aliyun.oss.ClientException) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder)

Example 3 with DownloadFileResult

use of com.aliyun.oss.model.DownloadFileResult 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);
    }
}
Also used : UploadFileRequest(com.aliyun.oss.model.UploadFileRequest) DownloadFileResult(com.aliyun.oss.model.DownloadFileResult) UploadFileResult(com.aliyun.oss.model.UploadFileResult) DownloadFileRequest(com.aliyun.oss.model.DownloadFileRequest) File(java.io.File) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) Test(org.junit.Test)

Example 4 with DownloadFileResult

use of com.aliyun.oss.model.DownloadFileResult 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);
    }
}
Also used : UploadFileRequest(com.aliyun.oss.model.UploadFileRequest) DownloadFileResult(com.aliyun.oss.model.DownloadFileResult) UploadFileResult(com.aliyun.oss.model.UploadFileResult) DownloadFileRequest(com.aliyun.oss.model.DownloadFileRequest) File(java.io.File) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) Test(org.junit.Test)

Example 5 with DownloadFileResult

use of com.aliyun.oss.model.DownloadFileResult in project aliyun-oss-java-sdk by aliyun.

the class DownloadFileTest method testUploadFileWithCheckpointFile.

@Test
public void testUploadFileWithCheckpointFile() {
    final String key = "obj-download-file-cpf";
    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);
        uploadFileRequest.setCheckpointFile("BingWallpaper.ucp");
        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);
        downloadFileRequest.setCheckpointFile("BingWallpaper.dcp");
        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);
    }
}
Also used : UploadFileRequest(com.aliyun.oss.model.UploadFileRequest) DownloadFileResult(com.aliyun.oss.model.DownloadFileResult) UploadFileResult(com.aliyun.oss.model.UploadFileResult) DownloadFileRequest(com.aliyun.oss.model.DownloadFileRequest) File(java.io.File) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) Test(org.junit.Test)

Aggregations

DownloadFileResult (com.aliyun.oss.model.DownloadFileResult)5 DownloadFileRequest (com.aliyun.oss.model.DownloadFileRequest)4 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)4 UploadFileRequest (com.aliyun.oss.model.UploadFileRequest)3 UploadFileResult (com.aliyun.oss.model.UploadFileResult)3 File (java.io.File)3 Test (org.junit.Test)3 ClientException (com.aliyun.oss.ClientException)1 OSS (com.aliyun.oss.OSS)1 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)1 OSSException (com.aliyun.oss.OSSException)1 LogUtils.logException (com.aliyun.oss.common.utils.LogUtils.logException)1 ProgressListener (com.aliyun.oss.event.ProgressListener)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1