use of com.aliyun.oss.model.CopyObjectRequest in project aliyun-oss-java-sdk by aliyun.
the class OSSClientRequestTest method testCopyObjectRequest.
@SuppressWarnings("serial")
@Test
public void testCopyObjectRequest() {
final String sourceBucketName = "src_bucket";
final String sourceKey = "src_key";
final CopyObjectRequest request = new CopyObjectRequest(sourceBucketName, sourceKey, bucketName, objectKey);
TestAction test1 = new TestAction() {
public void run() throws Exception {
objectOp.copyObject(request);
}
};
executeTest(test1, HttpMethod.PUT, bucketName + "." + endpoint.getHost(), objectKey, new HashMap<String, String>() {
{
put("x-oss-copy-source", "/" + sourceBucketName + "/" + sourceKey);
}
}, null, 0);
final List<String> matchingETags = new LinkedList<String>();
matchingETags.add("matching");
matchingETags.add("m2");
request.setMatchingETagConstraints(matchingETags);
final List<String> unmatchingETags = new LinkedList<String>();
unmatchingETags.add("unmatching");
unmatchingETags.add("u2");
request.setNonmatchingETagConstraints(unmatchingETags);
request.setModifiedSinceConstraint(new Date());
request.setUnmodifiedSinceConstraint(new Date());
final ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("text/plain");
metadata.setContentEncoding(OSSConstants.DEFAULT_CHARSET_NAME);
metadata.setCacheControl("no-cache");
metadata.setServerSideEncryption(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
metadata.setUserMetadata(new HashMap<String, String>() {
{
put("my", "my");
}
});
request.setNewObjectMetadata(metadata);
final Map<String, String> expectedHeaders = new HashMap<String, String>() {
{
put("x-oss-copy-source-if-match", matchingETags.get(0) + ", " + matchingETags.get(1));
put("x-oss-copy-source-if-none-match", unmatchingETags.get(0) + ", " + unmatchingETags.get(1));
put("x-oss-copy-source-if-modified-since", DateUtil.formatRfc822Date(request.getModifiedSinceConstraint()));
put("x-oss-copy-source-if-unmodified-since", DateUtil.formatRfc822Date(request.getUnmodifiedSinceConstraint()));
put("x-oss-metadata-directive", "REPLACE");
put("Content-Type", metadata.getContentType());
put("Content-Encoding", metadata.getContentEncoding());
put("Cache-Control", metadata.getCacheControl());
put("x-oss-server-side-encryption", metadata.getServerSideEncryption());
put("x-oss-meta-my", "my");
}
};
TestAction test2 = new TestAction() {
public void run() throws Exception {
objectOp.copyObject(request);
}
};
executeTest(test2, HttpMethod.PUT, bucketName + "." + endpoint.getHost(), objectKey, expectedHeaders, null, 0);
}
use of com.aliyun.oss.model.CopyObjectRequest in project aliyun-oss-java-sdk by aliyun.
the class CopyObjectTest method testCopyExistingObject.
@Test
public void testCopyExistingObject() {
final String sourceBucket = "copy-existing-object-source-bucket";
final String targetBucket = "copy-existing-object-target-bucket";
final String sourceKey = "copy-existing-object-source-object";
final String targetKey = "copy-existing-object-target-object";
final String userMetaKey0 = "user";
final String userMetaValue0 = "aliy";
final String userMetaKey1 = "tag";
final String userMetaValue1 = "copy-object";
final String contentType = "application/txt";
try {
ossClient.createBucket(sourceBucket);
ossClient.createBucket(targetBucket);
// Set source object different with target object and copy source bucket orignal metadata(default behavior).
byte[] content = { 'A', 'l', 'i', 'y', 'u', 'n' };
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(content.length);
metadata.setContentType(DEFAULT_OBJECT_CONTENT_TYPE);
metadata.addUserMetadata(userMetaKey0, userMetaValue0);
PutObjectResult putObjectResult = ossClient.putObject(sourceBucket, sourceKey, new ByteArrayInputStream(content), metadata);
CopyObjectResult copyObjectResult = ossClient.copyObject(sourceBucket, sourceKey, targetBucket, targetKey);
String sourceETag = putObjectResult.getETag();
String targetETag = copyObjectResult.getETag();
Assert.assertEquals(sourceETag, targetETag);
Assert.assertEquals(putObjectResult.getRequestId().length(), REQUEST_ID_LEN);
OSSObject ossObject = ossClient.getObject(targetBucket, targetKey);
ObjectMetadata newObjectMetadata = ossObject.getObjectMetadata();
Assert.assertEquals(DEFAULT_OBJECT_CONTENT_TYPE, newObjectMetadata.getContentType());
Assert.assertEquals(userMetaValue0, newObjectMetadata.getUserMetadata().get(userMetaKey0));
Assert.assertEquals(ossObject.getRequestId().length(), REQUEST_ID_LEN);
// Set source object same as target object and replace source bucket orignal metadata.
final String sourceBucketAsTarget = sourceBucket;
final String sourceKeyAsTarget = sourceKey;
newObjectMetadata = new ObjectMetadata();
newObjectMetadata.setContentLength(content.length);
newObjectMetadata.setContentType(contentType);
newObjectMetadata.addUserMetadata(userMetaKey1, userMetaValue1);
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceBucket, sourceKey, sourceBucketAsTarget, sourceKeyAsTarget);
copyObjectRequest.setNewObjectMetadata(newObjectMetadata);
copyObjectResult = ossClient.copyObject(copyObjectRequest);
Assert.assertEquals(sourceETag, copyObjectResult.getETag());
ossObject = ossClient.getObject(sourceBucketAsTarget, sourceKeyAsTarget);
newObjectMetadata = ossObject.getObjectMetadata();
Assert.assertEquals(contentType, newObjectMetadata.getContentType());
Assert.assertEquals(userMetaValue1, newObjectMetadata.getUserMetadata().get(userMetaKey1));
Assert.assertEquals(putObjectResult.getRequestId().length(), REQUEST_ID_LEN);
Assert.assertEquals(copyObjectResult.getRequestId().length(), REQUEST_ID_LEN);
Assert.assertEquals(ossObject.getRequestId().length(), REQUEST_ID_LEN);
} catch (Exception e) {
Assert.fail(e.getMessage());
} finally {
waitForCacheExpiration(5);
deleteBucketWithObjects(ossClient, sourceBucket);
deleteBucketWithObjects(ossClient, targetBucket);
}
}
use of com.aliyun.oss.model.CopyObjectRequest in project aliyun-oss-java-sdk by aliyun.
the class CopyObjectTest method testCopyObjectWithMiscConstraints.
@Ignore
public void testCopyObjectWithMiscConstraints() throws Exception {
final String sourceBucket = "copy-object-with-misc-constraints-source-bucket";
final String targetBucket = "copy-object-with-misc-constraints-target-bucket";
final String sourceKey = "copy-object-with-misc-constraints-source-key";
final String targetKey = "copy-object-with-misc-constraints-target-key";
try {
ossClient.createBucket(sourceBucket);
ossClient.createBucket(targetBucket);
final Date beforeModifiedTime = new Date();
Thread.sleep(1000);
String eTag = null;
try {
PutObjectResult result = ossClient.putObject(sourceBucket, sourceKey, TestUtils.genFixedLengthInputStream(1024), null);
eTag = result.getETag();
} catch (Exception e) {
Assert.fail(e.getMessage());
}
// Matching ETag Constraints
List<String> matchingETagConstraints = new ArrayList<String>();
matchingETagConstraints.add(eTag);
CopyObjectRequest request = new CopyObjectRequest(sourceBucket, sourceKey, targetBucket, targetKey);
request.setMatchingETagConstraints(matchingETagConstraints);
CopyObjectResult result = null;
try {
result = ossClient.copyObject(request);
Assert.assertEquals(eTag, result.getETag());
} catch (Exception e) {
Assert.fail(e.getMessage());
} finally {
request.clearMatchingETagConstraints();
}
matchingETagConstraints.clear();
matchingETagConstraints.add("nonmatching-etag");
request.setMatchingETagConstraints(matchingETagConstraints);
try {
result = ossClient.copyObject(request);
Assert.fail("Copy object should not be successful.");
} catch (OSSException e) {
Assert.assertEquals(OSSErrorCode.PRECONDITION_FAILED, e.getErrorCode());
// Assert.assertTrue(e.getMessage().startsWith(PRECONDITION_FAILED_ERR));
} finally {
request.clearMatchingETagConstraints();
}
// Non-Matching ETag Constraints
List<String> nonmatchingETagConstraints = new ArrayList<String>();
nonmatchingETagConstraints.add("nonmatching-etag");
request.setNonmatchingETagConstraints(nonmatchingETagConstraints);
try {
result = ossClient.copyObject(request);
Assert.assertEquals(eTag, result.getETag());
} catch (Exception e) {
Assert.fail(e.getMessage());
} finally {
request.clearNonmatchingETagConstraints();
}
nonmatchingETagConstraints.clear();
nonmatchingETagConstraints.add(eTag);
request.setNonmatchingETagConstraints(nonmatchingETagConstraints);
try {
result = ossClient.copyObject(request);
Assert.fail("Copy object should not be successful.");
} catch (OSSException e) {
Assert.assertEquals(OSSErrorCode.NOT_MODIFIED, e.getErrorCode());
Assert.assertTrue(e.getMessage().startsWith(NOT_MODIFIED_ERR));
} finally {
request.clearNonmatchingETagConstraints();
}
// Unmodified Since Constraint
Date unmodifiedSinceConstraint = new Date();
request.setUnmodifiedSinceConstraint(unmodifiedSinceConstraint);
try {
result = ossClient.copyObject(request);
Assert.assertEquals(eTag, result.getETag());
} catch (OSSException e) {
Assert.fail(e.getMessage());
} finally {
request.setUnmodifiedSinceConstraint(null);
}
unmodifiedSinceConstraint = beforeModifiedTime;
request.setUnmodifiedSinceConstraint(unmodifiedSinceConstraint);
try {
result = ossClient.copyObject(request);
Assert.fail("Copy object should not be successful.");
} catch (OSSException e) {
Assert.assertEquals(OSSErrorCode.PRECONDITION_FAILED, e.getErrorCode());
// Assert.assertTrue(e.getMessage().startsWith(PRECONDITION_FAILED_ERR));
} finally {
request.setUnmodifiedSinceConstraint(null);
}
// Modified Since Constraint
Date modifiedSinceConstraint = beforeModifiedTime;
request.setModifiedSinceConstraint(modifiedSinceConstraint);
try {
result = ossClient.copyObject(request);
Assert.assertEquals(eTag, result.getETag());
} catch (OSSException e) {
Assert.fail(e.getMessage());
} finally {
request.setModifiedSinceConstraint(null);
}
modifiedSinceConstraint = new Date();
request.setModifiedSinceConstraint(modifiedSinceConstraint);
try {
result = ossClient.copyObject(request);
Assert.fail("Copy object should not be successful.");
} catch (OSSException e) {
Assert.assertEquals(OSSErrorCode.NOT_MODIFIED, e.getErrorCode());
Assert.assertTrue(e.getMessage().startsWith(NOT_MODIFIED_ERR));
} finally {
request.setModifiedSinceConstraint(null);
}
} catch (Exception e) {
Assert.fail(e.getMessage());
} finally {
deleteBucketWithObjects(ossClient, sourceBucket);
deleteBucketWithObjects(ossClient, targetBucket);
}
}
use of com.aliyun.oss.model.CopyObjectRequest in project aliyun-oss-java-sdk by aliyun.
the class ObjectAclTest method testCopyObjectWithACLHeader.
@Test
public void testCopyObjectWithACLHeader() throws IOException {
final String sourceBucket = "copy-existing-object-source-bucket";
final String targetBucket = "copy-existing-object-target-bucket";
final String sourceKey = "copy-existing-object-source-object";
final String targetKey = "copy-existing-object-target-object";
final String userMetaKey0 = "user";
final String userMetaValue0 = "aliy";
final String userMetaKey1 = "tag";
final String userMetaValue1 = "copy-object";
final String contentType = "application/txt";
try {
ossClient.createBucket(sourceBucket);
ossClient.createBucket(targetBucket);
byte[] content = { 'A', 'l', 'i', 'y', 'u', 'n' };
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(content.length);
metadata.setContentType(DEFAULT_OBJECT_CONTENT_TYPE);
metadata.addUserMetadata(userMetaKey0, userMetaValue0);
PutObjectResult putObjectResult = ossClient.putObject(sourceBucket, sourceKey, new ByteArrayInputStream(content), metadata);
ObjectMetadata newObjectMetadata = new ObjectMetadata();
newObjectMetadata.setContentLength(content.length);
newObjectMetadata.setContentType(contentType);
newObjectMetadata.addUserMetadata(userMetaKey1, userMetaValue1);
newObjectMetadata.setObjectAcl(CannedAccessControlList.PublicRead);
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceBucket, sourceKey, targetBucket, targetKey);
copyObjectRequest.setNewObjectMetadata(newObjectMetadata);
CopyObjectResult copyObjectResult = ossClient.copyObject(copyObjectRequest);
String sourceETag = putObjectResult.getETag();
String targetETag = copyObjectResult.getETag();
Assert.assertEquals(sourceETag, targetETag);
Assert.assertEquals(putObjectResult.getRequestId().length(), REQUEST_ID_LEN);
OSSObject ossObject = ossClient.getObject(targetBucket, targetKey);
newObjectMetadata = ossObject.getObjectMetadata();
Assert.assertEquals(contentType, newObjectMetadata.getContentType());
Assert.assertEquals(userMetaValue1, newObjectMetadata.getUserMetadata().get(userMetaKey1));
// Verify uploaded objects acl
ObjectAcl returnedACL = ossClient.getObjectAcl(targetBucket, targetKey);
Assert.assertEquals(ObjectPermission.PublicRead, returnedACL.getPermission());
Assert.assertEquals(returnedACL.getRequestId().length(), REQUEST_ID_LEN);
Assert.assertEquals(ossObject.getRequestId().length(), REQUEST_ID_LEN);
} catch (Exception e) {
Assert.fail(e.getMessage());
} finally {
waitForCacheExpiration(5);
deleteBucketWithObjects(ossClient, sourceBucket);
deleteBucketWithObjects(ossClient, targetBucket);
}
}
use of com.aliyun.oss.model.CopyObjectRequest in project aliyun-oss-java-sdk by aliyun.
the class CopyObjectTest method testCopyObjectWithInvalidEncryptionAlgo.
@Ignore
public void testCopyObjectWithInvalidEncryptionAlgo() {
final String sourceBucket = "copy-object-with-invalid-encryption-algo-source-bucket";
final String targetBucket = "copy-object-with-invalid-encryption-algo-target-bucket";
final String sourceKey = "copy-object-with-invalid-encryption-algo-source-key";
final String targetKey = "copy-object-with-invalid-encryption-algo-target-key";
try {
ossClient.createBucket(sourceBucket);
ossClient.createBucket(targetBucket);
final String invalidEncryptionAlgo = "Invalid-Encryption-Algo";
try {
CopyObjectRequest request = new CopyObjectRequest(sourceBucket, sourceKey, targetBucket, targetKey);
request.setServerSideEncryption(invalidEncryptionAlgo);
ossClient.copyObject(request);
Assert.fail("Copy object should not be successful");
} catch (OSSException e) {
Assert.assertEquals(OSSErrorCode.INVALID_ENCRYPTION_ALGORITHM_ERROR, e.getErrorCode());
Assert.assertTrue(e.getMessage().startsWith(INVALID_ENCRYPTION_ALGO_ERR));
}
} catch (Exception e) {
Assert.fail(e.getMessage());
} finally {
deleteBucketWithObjects(ossClient, sourceBucket);
deleteBucketWithObjects(ossClient, targetBucket);
}
}
Aggregations