use of com.aliyun.oss.model.CopyObjectRequest in project aliyun-oss-java-sdk by aliyun.
the class CopyObjectTest method testCopyObjectWithSpecialChars.
@Ignore
public void testCopyObjectWithSpecialChars() {
final String sourceBucket = "copy-existing-object-source-bucket";
final String targetBucket = "copy-existing-object-target-bucket";
final String sourceKey = "测\\r试-中.~,+\"'*&¥#@%!(文)+字符|?/.zip";
final String targetKey = "测\\r试-中.~,+\"'*&¥#@%!(文)+字符|?/-2.zip";
final String userMetaKey0 = "user";
final String userMetaValue0 = "阿里人";
// TODO: With chinese characters will be failed.
final String userMetaKey1 = "tag";
final String userMetaValue1 = "标签1";
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);
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));
// 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));
} 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 OSSObjectOperationTest method testPopulateCopyObjectHeaders.
@Test
public void testPopulateCopyObjectHeaders() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, URISyntaxException {
CopyObjectRequest request = new CopyObjectRequest("srcbucket", "srckey", "destbucket", "destkey");
request.setServerSideEncryption(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
Method[] testMethods = OSSObjectOperation.class.getDeclaredMethods();
Method testMethod = null;
for (Method m : testMethods) {
if (m.getName().equals("populateCopyObjectHeaders")) {
testMethod = m;
}
}
testMethod.setAccessible(true);
OSSObjectOperation operations = new OSSObjectOperation(new DefaultServiceClient(new ClientConfiguration()), new DefaultCredentialProvider(new DefaultCredentials("id", "key")));
Map<String, String> headers = new HashMap<String, String>();
Object[] params = new Object[2];
params[0] = request;
params[1] = headers;
testMethod.invoke(operations, params);
assertEquals("/srcbucket/srckey", headers.get(OSSHeaders.COPY_OBJECT_SOURCE));
assertEquals(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION, headers.get(OSSHeaders.OSS_SERVER_SIDE_ENCRYPTION));
assertEquals(null, headers.get(OSSHeaders.COPY_OBJECT_METADATA_DIRECTIVE));
}
use of com.aliyun.oss.model.CopyObjectRequest in project druid by druid-io.
the class OssDataSegmentMover method selfCheckingMove.
/**
* Copies an object and after that checks that the object is present at the target location, via a separate API call.
* If it is not, an exception is thrown, and the object is not deleted at the old location. This "paranoic" check
* is added after it was observed that oss may report a successful move, and the object is not found at the target
* location.
*/
private void selfCheckingMove(String srcBucket, String dstBucket, String srcPath, String dstPath, String copyMsg) throws IOException, SegmentLoadingException {
if (srcBucket.equals(dstBucket) && srcPath.equals(dstPath)) {
log.info("No need to move file[%s://%s/%s] onto itself", OssStorageDruidModule.SCHEME, srcBucket, srcPath);
return;
}
final OSS client = this.clientSupplier.get();
if (client.doesObjectExist(srcBucket, srcPath)) {
final ObjectListing listResult = client.listObjects(new ListObjectsRequest(srcBucket, srcPath, null, null, 1));
// keyCount is still zero.
if (listResult.getObjectSummaries().size() == 0) {
// should never happen
throw new ISE("Unable to list object [%s://%s/%s]", OssStorageDruidModule.SCHEME, srcBucket, srcPath);
}
final OSSObjectSummary objectSummary = listResult.getObjectSummaries().get(0);
if (objectSummary.getStorageClass() != null && objectSummary.getStorageClass().equals(StorageClass.IA.name())) {
throw new OSSException(StringUtils.format("Cannot move file[%s://%s/%s] of storage class glacier, skipping.", OssStorageDruidModule.SCHEME, srcBucket, srcPath));
} else {
log.info("Moving file %s", copyMsg);
final CopyObjectRequest copyRequest = new CopyObjectRequest(srcBucket, srcPath, dstBucket, dstPath);
client.copyObject(copyRequest);
if (!client.doesObjectExist(dstBucket, dstPath)) {
throw new IOE("After copy was reported as successful the file doesn't exist in the target location [%s]", copyMsg);
}
deleteWithRetriesSilent(srcBucket, srcPath);
log.debug("Finished moving file %s", copyMsg);
}
} else {
// ensure object exists in target location
if (client.doesObjectExist(dstBucket, dstPath)) {
log.info("Not moving file [%s://%s/%s], already present in target location [%s://%s/%s]", OssStorageDruidModule.SCHEME, srcBucket, srcPath, OssStorageDruidModule.SCHEME, dstBucket, dstPath);
} else {
throw new SegmentLoadingException("Unable to move file %s, not present in either source or target location", copyMsg);
}
}
}
Aggregations