Search in sources :

Example 36 with PutObjectResult

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

the class UploadPartCopyTest method testNormalUploadPartCopy.

@Test
public void testNormalUploadPartCopy() {
    final String sourceBucket = super.bucketName + "-" + "normal-upload-part-copy-source";
    final String targetBucket = super.bucketName + "-" + "normal-upload-part-copy-target";
    final String sourceKey = "normal-upload-part-copy-object-source";
    final String targetKey = "normal-upload-part-copy-object-target";
    // 128KB
    final long partSize = 128 * 1024;
    try {
        ossClient.createBucket(sourceBucket);
        ossClient.createBucket(targetBucket);
        waitForCacheExpiration(5);
        // Put object into source bucket
        String eTag = null;
        try {
            InputStream instream = genFixedLengthInputStream(partSize);
            PutObjectResult result = ossClient.putObject(sourceBucket, sourceKey, instream, null);
            eTag = result.getETag();
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
        // Claim upload id for target bucket
        String uploadId = claimUploadId(ossClient, targetBucket, targetKey);
        // Upload part copy
        final int partNumber = 1;
        List<PartETag> partETags = new ArrayList<PartETag>();
        UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(sourceBucket, sourceKey, targetBucket, targetKey);
        uploadPartCopyRequest.setPartNumber(partNumber);
        uploadPartCopyRequest.setUploadId(uploadId);
        UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
        partETags.add(uploadPartCopyResult.getPartETag());
        Assert.assertEquals(eTag, uploadPartCopyResult.getETag());
        Assert.assertEquals(partNumber, uploadPartCopyResult.getPartNumber());
        Assert.assertEquals(uploadPartCopyResult.getRequestId().length(), REQUEST_ID_LEN);
        ListPartsRequest listPartsRequest = new ListPartsRequest(targetBucket, targetKey, uploadId);
        PartListing partListing = ossClient.listParts(listPartsRequest);
        Assert.assertEquals(1, partListing.getParts().size());
        Assert.assertEquals(targetBucket, partListing.getBucketName());
        Assert.assertEquals(targetKey, partListing.getKey());
        Assert.assertEquals(uploadId, partListing.getUploadId());
        Assert.assertEquals(LIST_PART_MAX_RETURNS, partListing.getMaxParts().intValue());
        Assert.assertNotNull(partListing.getNextPartNumberMarker());
        Assert.assertFalse(partListing.isTruncated());
        Assert.assertEquals(partListing.getRequestId().length(), REQUEST_ID_LEN);
        // Complete multipart upload
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(targetBucket, targetKey, uploadId, partETags);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        Assert.assertEquals(composeLocation(ossClient, OSS_TEST_ENDPOINT, targetBucket, targetKey), completeMultipartUploadResult.getLocation());
        Assert.assertEquals(targetBucket, completeMultipartUploadResult.getBucketName());
        Assert.assertEquals(targetKey, completeMultipartUploadResult.getKey());
        Assert.assertEquals(calcMultipartsETag(partETags), completeMultipartUploadResult.getETag());
        Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
        // Get uploaded object
        OSSObject o = ossClient.getObject(targetBucket, targetKey);
        final long objectSize = 1 * partSize;
        Assert.assertEquals(objectSize, o.getObjectMetadata().getContentLength());
        Assert.assertEquals(calcMultipartsETag(partETags), o.getObjectMetadata().getETag());
        Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        deleteBucketWithObjects(ossClient, sourceBucket);
        deleteBucketWithObjects(ossClient, targetBucket);
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) OSSException(com.aliyun.oss.OSSException) PartETag(com.aliyun.oss.model.PartETag) PartListing(com.aliyun.oss.model.PartListing) ListPartsRequest(com.aliyun.oss.model.ListPartsRequest) UploadPartCopyResult(com.aliyun.oss.model.UploadPartCopyResult) UploadPartCopyRequest(com.aliyun.oss.model.UploadPartCopyRequest) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 37 with PutObjectResult

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

the class ObjectEncryptionTest method testNormalObject.

private void testNormalObject(SSEAlgorithm algorithm, DataEncryptionAlgorithm dataEncryptionAlgorithm) throws Exception {
    for (int i = 0; i < ITERATIONS; ++i) {
        final String keyPrefix = "normal-file-encryption-";
        // 1KB
        final String filePath = genFixedLengthFile(i * 1024 * 100 + 1024);
        try {
            // 1. put
            ObjectMetadata metadata = new ObjectMetadata();
            if (algorithm != null) {
                metadata.setServerSideEncryption(algorithm.toString());
            }
            if (dataEncryptionAlgorithm != null) {
                metadata.setServerSideDataEncryption(dataEncryptionAlgorithm.toString());
            }
            PutObjectRequest request = new PutObjectRequest(bucketName, buildObjectKey(keyPrefix, i), new File(filePath), metadata);
            request.setProcess("");
            PutObjectResult result = ossClient.putObject(request);
            Map<String, String> headers = result.getResponse().getHeaders();
            Assert.assertEquals(algorithm != null, headers.containsKey(OSSHeaders.OSS_SERVER_SIDE_ENCRYPTION));
            if (algorithm != null) {
                Assert.assertEquals(headers.get(OSSHeaders.OSS_SERVER_SIDE_ENCRYPTION), algorithm.toString());
            }
            Assert.assertEquals(dataEncryptionAlgorithm != null, headers.containsKey(OSSHeaders.OSS_SERVER_SIDE_DATA_ENCRYPTION));
            if (dataEncryptionAlgorithm != null) {
                Assert.assertEquals(dataEncryptionAlgorithm.toString(), headers.get(OSSHeaders.OSS_SERVER_SIDE_DATA_ENCRYPTION));
            }
            // 2. get
            byte[] target = InputStream2ByteArray(filePath);
            checkObjectContent(target, buildObjectKey(keyPrefix, i), algorithm, dataEncryptionAlgorithm);
            // 3. copy without encryption
            copyObject(target, buildObjectKey(keyPrefix, i), buildObjectKey(keyPrefix + "copy-", i), null, null);
            // 4. copy with the same encryption
            copyObject(target, buildObjectKey(keyPrefix, i), buildObjectKey(keyPrefix + "copy-", i), algorithm, dataEncryptionAlgorithm);
            // 5. copy with different encryption
            if (dataEncryptionAlgorithm == null) {
                copyObject(target, buildObjectKey(keyPrefix, i), buildObjectKey(keyPrefix + "copy-", i), SSEAlgorithm.KMS, DataEncryptionAlgorithm.SM4);
            } else {
                copyObject(target, buildObjectKey(keyPrefix, i), buildObjectKey(keyPrefix + "copy-", i), SSEAlgorithm.KMS, null);
                copyObject(target, buildObjectKey(keyPrefix, i), buildObjectKey(keyPrefix + "copy-", i), SSEAlgorithm.SM4, null);
                copyObject(target, buildObjectKey(keyPrefix, i), buildObjectKey(keyPrefix + "copy-", i), SSEAlgorithm.AES256, null);
            }
        } catch (Exception ex) {
            Assert.fail(ex.getMessage());
        } finally {
            removeFile(filePath);
        }
    }
}
Also used : PutObjectResult(com.aliyun.oss.model.PutObjectResult) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) File(java.io.File) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest)

Example 38 with PutObjectResult

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

the class ObjectVersionTest method testCopyObject.

@Test
public void testCopyObject() {
    String srcKey = "version-test-src";
    String destKey = "version-test-dest";
    long inputStreamLength = 1024;
    try {
        InputStream instream = genFixedLengthInputStream(inputStreamLength);
        ObjectMetadata metadata = new ObjectMetadata();
        PutObjectResult puObjectResult = ossClient.putObject(bucketName, srcKey, instream, metadata);
        CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucketName, srcKey, bucketName, destKey);
        copyObjectRequest.setSourceVersionId(puObjectResult.getVersionId());
        CopyObjectResult copyObjectResult = ossClient.copyObject(copyObjectRequest);
        Assert.assertTrue(copyObjectResult.getVersionId() != null);
        Assert.assertTrue(copyObjectResult.getVersionId().length() == 64);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : CopyObjectRequest(com.aliyun.oss.model.CopyObjectRequest) PutObjectResult(com.aliyun.oss.model.PutObjectResult) InputStream(java.io.InputStream) CopyObjectResult(com.aliyun.oss.model.CopyObjectResult) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 39 with PutObjectResult

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

the class ObjectVersionTest method testDownloadFile.

@Test
public void testDownloadFile() throws Throwable {
    String key = "version-test-download-file-object";
    int inputStreamLength1 = 2567;
    int inputStreamLength2 = 5678;
    String downFileName = key + "-down.txt";
    try {
        // Put object version1
        InputStream instream = genFixedLengthInputStream(inputStreamLength1);
        PutObjectResult putObjectResult = ossClient.putObject(bucketName, key, instream);
        String versionId1 = putObjectResult.getVersionId();
        // Put object version2
        instream = genFixedLengthInputStream(inputStreamLength2);
        putObjectResult = ossClient.putObject(bucketName, key, instream);
        String versionId2 = putObjectResult.getVersionId();
        // Download object verison1
        DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, key);
        downloadFileRequest.setVersionId(versionId1);
        downloadFileRequest.setTaskNum(2);
        downloadFileRequest.setPartSize(512 * 1024);
        downloadFileRequest.setDownloadFile(downFileName);
        ossClient.downloadFile(downloadFileRequest);
        File downFile = new File(downFileName);
        long length1 = downFile.length();
        downFile.delete();
        // Check download object version1
        Assert.assertEquals(length1, inputStreamLength1);
        // Download object verison2
        downloadFileRequest = new DownloadFileRequest(bucketName, key);
        downloadFileRequest.setVersionId(versionId2);
        downloadFileRequest.setDownloadFile(downFileName);
        ossClient.downloadFile(downloadFileRequest);
        downFile = new File(downFileName);
        long length2 = downFile.length();
        downFile.delete();
        // Check download object version2
        Assert.assertEquals(length2, inputStreamLength2);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : PutObjectResult(com.aliyun.oss.model.PutObjectResult) InputStream(java.io.InputStream) DownloadFileRequest(com.aliyun.oss.model.DownloadFileRequest) File(java.io.File) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 40 with PutObjectResult

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

the class ObjectVersionTest method tesDeleteVersion.

@Test
public void tesDeleteVersion() {
    String key = "version-test-del-version";
    long inputStreamLength = 1024;
    try {
        InputStream instream = genFixedLengthInputStream(inputStreamLength);
        ObjectMetadata metadata = new ObjectMetadata();
        PutObjectResult putResult = ossClient.putObject(bucketName, key, instream, metadata);
        Assert.assertTrue(putResult.getVersionId() != null);
        Assert.assertTrue(putResult.getVersionId().length() == 64);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key, putResult.getVersionId());
        getObjectRequest.setVersionId(putResult.getVersionId());
        OSSObject ossObject = ossClient.getObject(getObjectRequest);
        ossObject.close();
        Assert.assertEquals(inputStreamLength, ossObject.getObjectMetadata().getContentLength());
        Assert.assertEquals(putResult.getVersionId(), ossObject.getObjectMetadata().getVersionId());
        ossClient.deleteVersion(bucketName, key, putResult.getVersionId());
        try {
            ossClient.getObject(getObjectRequest);
        } catch (OSSException e) {
            Assert.assertEquals(OSSErrorCode.NO_SUCH_VERSION, e.getErrorCode());
        }
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) InputStream(java.io.InputStream) OSSException(com.aliyun.oss.OSSException) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Aggregations

PutObjectResult (com.aliyun.oss.model.PutObjectResult)60 Test (org.junit.Test)31 OSSException (com.aliyun.oss.OSSException)30 InputStream (java.io.InputStream)24 OSSObject (com.aliyun.oss.model.OSSObject)20 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)19 File (java.io.File)15 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)13 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)10 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ArrayList (java.util.ArrayList)10 FileInputStream (java.io.FileInputStream)9 Callback (com.aliyun.oss.model.Callback)8 IOException (java.io.IOException)8 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)7 Date (java.util.Date)7 OSS (com.aliyun.oss.OSS)6 CopyObjectResult (com.aliyun.oss.model.CopyObjectResult)6 Ignore (org.junit.Ignore)6 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)5