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);
}
}
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);
}
}
}
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());
}
}
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());
}
}
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());
}
}
Aggregations