Search in sources :

Example 6 with DownloadFileRequest

use of com.aliyun.oss.model.DownloadFileRequest 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

DownloadFileRequest (com.aliyun.oss.model.DownloadFileRequest)6 UploadFileRequest (com.aliyun.oss.model.UploadFileRequest)5 Test (org.junit.Test)5 DownloadFileResult (com.aliyun.oss.model.DownloadFileResult)4 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)4 File (java.io.File)4 UploadFileResult (com.aliyun.oss.model.UploadFileResult)3 ClientException (com.aliyun.oss.ClientException)2 OSSException (com.aliyun.oss.OSSException)2 OSS (com.aliyun.oss.OSS)1 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)1 OSSObject (com.aliyun.oss.model.OSSObject)1 SimplifiedObjectMeta (com.aliyun.oss.model.SimplifiedObjectMeta)1 IOException (java.io.IOException)1