Search in sources :

Example 6 with InitiateMultipartUploadRequest

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

the class SecurityTokenTest method testObjectOperationsWithToken.

@Test
public void testObjectOperationsWithToken() throws JSONException {
    List<String> actions = new ArrayList<String>();
    List<String> resources = new ArrayList<String>();
    // Put bucket with valid security token
    final String bucketName = "test-object-operations-with-token-bucket0";
    actions.add("oss:PutBucket");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    OSSClient sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.createBucket(bucketName);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Put object
    final String key = "test-object-operations-with-token-key0";
    final long instreamLength = 1024;
    InputStream instream = null;
    actions.add("oss:PutObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        instream = genFixedLengthInputStream(instreamLength);
        sessionClient.putObject(bucketName, key, instream, null);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        if (instream != null) {
            try {
                instream.close();
            } catch (IOException e) {
            }
        }
        sessionClient.shutdown();
    }
    // Get object
    actions.add("oss:GetObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        OSSObject o = sessionClient.getObject(bucketName, key);
        Assert.assertEquals(instreamLength, o.getObjectMetadata().getContentLength());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Copy object
    actions.add("oss:GetObject");
    actions.add("oss:PutObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.copyObject(bucketName, key, bucketName, key + DUMMY_SUFFIX);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Initiate multipart upload and upload single part
    // 128KB
    final int partSize = 128 * 1024;
    String uploadId = null;
    List<PartETag> partETags = new ArrayList<PartETag>();
    actions.add("oss:PutObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult result = sessionClient.initiateMultipartUpload(request);
        instream = genFixedLengthInputStream(partSize);
        uploadId = result.getUploadId();
        // Upload single part
        UploadPartRequest uploadPartRequest = new UploadPartRequest();
        uploadPartRequest.setBucketName(bucketName);
        uploadPartRequest.setKey(key);
        uploadPartRequest.setInputStream(instream);
        uploadPartRequest.setPartNumber(1);
        uploadPartRequest.setPartSize(partSize);
        uploadPartRequest.setUploadId(uploadId);
        UploadPartResult uploadPartResult = sessionClient.uploadPart(uploadPartRequest);
        partETags.add(uploadPartResult.getPartETag());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // List parts
    actions.add("oss:ListParts");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
        PartListing partListing = sessionClient.listParts(listPartsRequest);
        Assert.assertEquals(1, partListing.getParts().size());
        Assert.assertEquals(bucketName, partListing.getBucketName());
        Assert.assertEquals(key, partListing.getKey());
        Assert.assertEquals(uploadId, partListing.getUploadId());
        Assert.assertEquals(1000, partListing.getMaxParts().intValue());
        Assert.assertEquals(1, partListing.getNextPartNumberMarker().intValue());
        Assert.assertFalse(partListing.isTruncated());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Complete multipart
    actions.add("oss:PutObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        CompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        sessionClient.completeMultipartUpload(request);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Cleanup objects and bucket
    actions.add("oss:DeleteObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.deleteObject(bucketName, key);
        sessionClient.deleteObject(bucketName, key + DUMMY_SUFFIX);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    actions.add("oss:DeleteBucket");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.deleteBucket(bucketName);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
}
Also used : InitiateMultipartUploadResult(com.aliyun.oss.model.InitiateMultipartUploadResult) OSSObject(com.aliyun.oss.model.OSSObject) OSSClient(com.aliyun.oss.OSSClient) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) InitiateMultipartUploadRequest(com.aliyun.oss.model.InitiateMultipartUploadRequest) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) IOException(java.io.IOException) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) PartETag(com.aliyun.oss.model.PartETag) PartListing(com.aliyun.oss.model.PartListing) ListPartsRequest(com.aliyun.oss.model.ListPartsRequest) UploadPartResult(com.aliyun.oss.model.UploadPartResult) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 7 with InitiateMultipartUploadRequest

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

the class GetRequestIdTest method testNormalGetRequestId.

@Test
public void testNormalGetRequestId() {
    final String key = "normal-get-request-id";
    final long inputStreamLength = 1024;
    final int requestIdLength = "572BF2F2207FB3397648E9F1".length();
    try {
        // put object
        PutObjectResult putObjectResult = ossClient.putObject(bucketName, key, genFixedLengthInputStream(inputStreamLength));
        Assert.assertEquals(putObjectResult.getRequestId().length(), requestIdLength);
        // get object
        OSSObject ossObject = ossClient.getObject(bucketName, key);
        ossObject.getObjectContent().close();
        Assert.assertEquals(ossObject.getRequestId().length(), requestIdLength);
        File file = new File("tmp");
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        ObjectMetadata objectMeta = ossClient.getObject(getObjectRequest, file);
        Assert.assertEquals(objectMeta.getRequestId().length(), requestIdLength);
        // delete object
        ossClient.deleteObject(bucketName, key);
        // append object
        AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, file);
        appendObjectRequest.setPosition(0L);
        AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
        Assert.assertEquals(appendObjectResult.getRequestId().length(), requestIdLength);
        // getSimplifiedObjectMeta
        SimplifiedObjectMeta simplifiedObjectMeta = ossClient.getSimplifiedObjectMeta(bucketName, key);
        Assert.assertEquals(simplifiedObjectMeta.getRequestId().length(), requestIdLength);
        // getObjectMetadata
        ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, key);
        Assert.assertEquals(objectMetadata.getRequestId().length(), requestIdLength);
        // delete objects
        DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);
        ArrayList<String> keys = new ArrayList<String>();
        keys.add(key);
        deleteObjectsRequest.setKeys(keys);
        DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(deleteObjectsRequest);
        Assert.assertEquals(deleteObjectsResult.getRequestId().length(), requestIdLength);
        // initiate multipart upload
        InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
        Assert.assertEquals(initiateMultipartUploadResult.getRequestId().length(), requestIdLength);
        // upload part
        UploadPartRequest uploadPartRequest = new UploadPartRequest(bucketName, key, initiateMultipartUploadResult.getUploadId(), 1, new FileInputStream(file), inputStreamLength);
        UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
        Assert.assertEquals(uploadPartResult.getRequestId().length(), requestIdLength);
        // complete multipart upload
        List<PartETag> partETags = new ArrayList<PartETag>();
        partETags.add(new PartETag(1, uploadPartResult.getETag()));
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, initiateMultipartUploadResult.getUploadId(), partETags);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), requestIdLength);
        // copy object
        CopyObjectResult CopyObjectResult = ossClient.copyObject(bucketName, key, bucketName, key);
        Assert.assertEquals(CopyObjectResult.getRequestId().length(), requestIdLength);
        // initiate multipart copy
        InitiateMultipartUploadRequest initiateMultipartCopyRequest = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult initiateMultipartCopyResult = ossClient.initiateMultipartUpload(initiateMultipartCopyRequest);
        Assert.assertEquals(initiateMultipartCopyResult.getRequestId().length(), requestIdLength);
        // upload part copy
        UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(bucketName, key, bucketName, key, initiateMultipartCopyResult.getUploadId(), 1, 0L, inputStreamLength);
        UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
        Assert.assertEquals(uploadPartCopyResult.getRequestId().length(), requestIdLength);
        // abort multipart upload
        AbortMultipartUploadRequest AbortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, key, initiateMultipartCopyResult.getUploadId());
        ossClient.abortMultipartUpload(AbortMultipartUploadRequest);
        ossClient.deleteObject(bucketName, key);
        file.delete();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : SimplifiedObjectMeta(com.aliyun.oss.model.SimplifiedObjectMeta) InitiateMultipartUploadResult(com.aliyun.oss.model.InitiateMultipartUploadResult) ArrayList(java.util.ArrayList) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) AppendObjectResult(com.aliyun.oss.model.AppendObjectResult) UploadPartResult(com.aliyun.oss.model.UploadPartResult) UploadPartCopyResult(com.aliyun.oss.model.UploadPartCopyResult) CopyObjectResult(com.aliyun.oss.model.CopyObjectResult) UploadPartCopyRequest(com.aliyun.oss.model.UploadPartCopyRequest) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) InitiateMultipartUploadRequest(com.aliyun.oss.model.InitiateMultipartUploadRequest) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) AbortMultipartUploadRequest(com.aliyun.oss.model.AbortMultipartUploadRequest) DeleteObjectsResult(com.aliyun.oss.model.DeleteObjectsResult) FileInputStream(java.io.FileInputStream) DeleteObjectsRequest(com.aliyun.oss.model.DeleteObjectsRequest) AppendObjectRequest(com.aliyun.oss.model.AppendObjectRequest) File(java.io.File) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 8 with InitiateMultipartUploadRequest

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

the class CRCChecksumTest method testMutilUploadCRC.

@Test
public void testMutilUploadCRC() {
    String key = "mutil-upload-object-crc";
    try {
        List<PartETag> partETags = new ArrayList<PartETag>();
        // 初始化上传任务
        InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
        Assert.assertEquals(initiateMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
        String uploadId = initiateMultipartUploadResult.getUploadId();
        // 上传分片
        for (int i = 0; i < 5; i++) {
            long fileLen = 1024 * 100 * (i + 1);
            String filePath = TestUtils.genFixedLengthFile(fileLen);
            UploadPartRequest request = new UploadPartRequest(bucketName, key, uploadId, i + 1, new FileInputStream(filePath), fileLen);
            UploadPartResult uploadPartResult = ossClient.uploadPart(request);
            Assert.assertEquals(uploadPartResult.getRequestId().length(), REQUEST_ID_LEN);
            partETags.add(uploadPartResult.getPartETag());
            checkCRC(uploadPartResult);
        }
        // 提交上传任务,服务器返回整个文件的CRC,客户端没有计算
        CompleteMultipartUploadRequest CompleteMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(CompleteMultipartUploadRequest);
        Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
        checkCRC(completeMultipartUploadResult);
        ossClient.deleteObject(bucketName, key);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : InitiateMultipartUploadResult(com.aliyun.oss.model.InitiateMultipartUploadResult) ArrayList(java.util.ArrayList) InitiateMultipartUploadRequest(com.aliyun.oss.model.InitiateMultipartUploadRequest) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) FileInputStream(java.io.FileInputStream) InconsistentException(com.aliyun.oss.InconsistentException) UploadPartResult(com.aliyun.oss.model.UploadPartResult) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 9 with InitiateMultipartUploadRequest

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

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

the class TestUtils method claimUploadId.

public static String claimUploadId(OSSClient client, String bucketName, String key) {
    InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, key);
    InitiateMultipartUploadResult result = client.initiateMultipartUpload(request);
    Assert.assertEquals(bucketName, result.getBucketName());
    Assert.assertEquals(key, result.getKey());
    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