Search in sources :

Example 11 with InitiateMultipartUploadRequest

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

the class ChunkedEncodingTest method testUploadPartChunked.

@Ignore
public void testUploadPartChunked() {
    try {
        InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult initiateMultipartUploadResult = client.initiateMultipartUpload(initiateMultipartUploadRequest);
        String uploadId = initiateMultipartUploadResult.getUploadId();
        File file = new File(filePath);
        final int partSize = 5 * 1024 * 1024;
        int fileSize = (int) file.length();
        final int partCount = (file.length() % partSize != 0) ? (fileSize / partSize + 1) : (fileSize / partSize);
        List<PartETag> partETags = new ArrayList<PartETag>();
        for (int i = 0; i < partCount; i++) {
            InputStream fin = new BufferedInputStream(new FileInputStream(file));
            fin.skip(i * partSize);
            int size = (i + 1 == partCount) ? (fileSize - i * partSize) : partSize;
            UploadPartRequest req = new UploadPartRequest();
            req.setBucketName(bucketName);
            req.setKey(key);
            req.setPartNumber(i + 1);
            req.setPartSize(size);
            req.setUploadId(uploadId);
            req.setInputStream(fin);
            req.setUseChunkEncoding(true);
            UploadPartResult result = client.uploadPart(req);
            partETags.add(result.getPartETag());
        }
        String expectedETag = calcMultipartETag(partETags);
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        CompleteMultipartUploadResult completeMultipartUploadResult = client.completeMultipartUpload(completeMultipartUploadRequest);
        String actualETag = completeMultipartUploadResult.getETag();
        Assert.assertEquals(expectedETag, actualETag);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : InitiateMultipartUploadResult(com.aliyun.oss.model.InitiateMultipartUploadResult) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InitiateMultipartUploadRequest(com.aliyun.oss.model.InitiateMultipartUploadRequest) ArrayList(java.util.ArrayList) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) FileInputStream(java.io.FileInputStream) UploadPartResult(com.aliyun.oss.model.UploadPartResult) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Ignore(org.junit.Ignore)

Example 12 with InitiateMultipartUploadRequest

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

the class MultipartUploadSample method claimUploadId.

private static String claimUploadId() {
    InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, key);
    InitiateMultipartUploadResult result = client.initiateMultipartUpload(request);
    return result.getUploadId();
}
Also used : InitiateMultipartUploadResult(com.aliyun.oss.model.InitiateMultipartUploadResult) InitiateMultipartUploadRequest(com.aliyun.oss.model.InitiateMultipartUploadRequest)

Aggregations

InitiateMultipartUploadRequest (com.aliyun.oss.model.InitiateMultipartUploadRequest)12 InitiateMultipartUploadResult (com.aliyun.oss.model.InitiateMultipartUploadResult)11 PartETag (com.aliyun.oss.model.PartETag)8 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)7 ArrayList (java.util.ArrayList)7 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)6 OSSException (com.aliyun.oss.OSSException)5 CompleteMultipartUploadResult (com.aliyun.oss.model.CompleteMultipartUploadResult)5 UploadPartRequest (com.aliyun.oss.model.UploadPartRequest)5 UploadPartResult (com.aliyun.oss.model.UploadPartResult)5 File (java.io.File)5 Test (org.junit.Test)5 AbortMultipartUploadRequest (com.aliyun.oss.model.AbortMultipartUploadRequest)4 FileInputStream (java.io.FileInputStream)4 ClientException (com.aliyun.oss.ClientException)3 UploadPartCopyRequest (com.aliyun.oss.model.UploadPartCopyRequest)3 UploadPartCopyResult (com.aliyun.oss.model.UploadPartCopyResult)3 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)2 OSSObject (com.aliyun.oss.model.OSSObject)2 InputStream (java.io.InputStream)2