Search in sources :

Example 6 with UploadFileRequest

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

use of com.aliyun.oss.model.UploadFileRequest 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 8 with UploadFileRequest

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

the class BucketStatTest method testGetBucketStat.

@Test
public void testGetBucketStat() {
    String key = "obj-upload-file-stat.txt";
    String uploadId = null;
    try {
        File file = createSampleFile(key, 1024 * 500);
        // upload a file
        UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
        uploadFileRequest.setUploadFile(file.getAbsolutePath());
        uploadFileRequest.setTaskNum(10);
        UploadFileResult uploadRes = ossClient.uploadFile(uploadFileRequest);
        Assert.assertEquals(uploadRes.getMultipartUploadResult().getBucketName(), bucketName);
        Assert.assertEquals(uploadRes.getMultipartUploadResult().getKey(), key);
        // init upload
        InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
        Assert.assertEquals(initiateMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
        uploadId = initiateMultipartUploadResult.getUploadId();
        BucketStat stat = ossClient.getBucketStat(bucketName);
        System.out.println(stat.getStorageSize() + "," + stat.getObjectCount() + "," + stat.getMultipartUploadCount());
        Assert.assertTrue(stat.getStorageSize() >= 1024 * 300);
        Assert.assertTrue(stat.getObjectCount() >= 1);
        Assert.assertTrue(stat.getMultipartUploadCount() >= 1);
        Assert.assertEquals(stat.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    } catch (Throwable e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    } finally {
        if (uploadId != null) {
            AbortMultipartUploadRequest AbortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, key, uploadId);
            ossClient.abortMultipartUpload(AbortMultipartUploadRequest);
        }
    }
}
Also used : UploadFileRequest(com.aliyun.oss.model.UploadFileRequest) InitiateMultipartUploadResult(com.aliyun.oss.model.InitiateMultipartUploadResult) BucketStat(com.aliyun.oss.model.BucketStat) InitiateMultipartUploadRequest(com.aliyun.oss.model.InitiateMultipartUploadRequest) UploadFileResult(com.aliyun.oss.model.UploadFileResult) AbortMultipartUploadRequest(com.aliyun.oss.model.AbortMultipartUploadRequest) File(java.io.File) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 9 with UploadFileRequest

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

Example 10 with UploadFileRequest

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

the class UploadFileTest method testUploadFileWithoutCheckpoint.

@Test
public void testUploadFileWithoutCheckpoint() {
    final String key = "obj-upload-file-wcp";
    try {
        File file = createSampleFile(key, 1024 * 500);
        UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
        uploadFileRequest.setUploadFile(file.getAbsolutePath());
        uploadFileRequest.setTaskNum(10);
        UploadFileResult uploadRes = ossClient.uploadFile(uploadFileRequest);
        Assert.assertEquals(uploadRes.getMultipartUploadResult().getBucketName(), bucketName);
        Assert.assertEquals(uploadRes.getMultipartUploadResult().getKey(), key);
        ObjectListing objects = ossClient.listObjects(bucketName, key);
        Assert.assertEquals(objects.getObjectSummaries().size(), 1);
        Assert.assertEquals(objects.getObjectSummaries().get(0).getKey(), key);
        Assert.assertEquals(objects.getObjectSummaries().get(0).getSize(), file.length());
        Assert.assertEquals(objects.getRequestId().length(), REQUEST_ID_LEN);
        ObjectMetadata meta = ossClient.getObjectMetadata(bucketName, key);
        Assert.assertEquals(meta.getContentLength(), file.length());
        Assert.assertEquals(meta.getContentType(), "text/plain");
        File fileNew = new File(key + "-new.txt");
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        ossClient.getObject(getObjectRequest, fileNew);
        Assert.assertEquals(file.length(), fileNew.length());
        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) ObjectListing(com.aliyun.oss.model.ObjectListing) UploadFileResult(com.aliyun.oss.model.UploadFileResult) File(java.io.File) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) Test(org.junit.Test)

Aggregations

UploadFileRequest (com.aliyun.oss.model.UploadFileRequest)11 Test (org.junit.Test)9 UploadFileResult (com.aliyun.oss.model.UploadFileResult)8 File (java.io.File)8 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)6 DownloadFileRequest (com.aliyun.oss.model.DownloadFileRequest)5 OSSException (com.aliyun.oss.OSSException)4 ClientException (com.aliyun.oss.ClientException)3 DownloadFileResult (com.aliyun.oss.model.DownloadFileResult)3 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)3 ObjectListing (com.aliyun.oss.model.ObjectListing)3 OSS (com.aliyun.oss.OSS)2 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)2 OSSObject (com.aliyun.oss.model.OSSObject)2 InconsistentException (com.aliyun.oss.InconsistentException)1 AbortMultipartUploadRequest (com.aliyun.oss.model.AbortMultipartUploadRequest)1 AppendObjectRequest (com.aliyun.oss.model.AppendObjectRequest)1 AppendObjectResult (com.aliyun.oss.model.AppendObjectResult)1 BucketStat (com.aliyun.oss.model.BucketStat)1 CompleteMultipartUploadResult (com.aliyun.oss.model.CompleteMultipartUploadResult)1