Search in sources :

Example 6 with CopyObjectRequest

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);
    }
}
Also used : CopyObjectRequest(com.aliyun.oss.model.CopyObjectRequest) OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) ByteArrayInputStream(java.io.ByteArrayInputStream) CopyObjectResult(com.aliyun.oss.model.CopyObjectResult) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSSException(com.aliyun.oss.OSSException) Ignore(org.junit.Ignore)

Example 7 with CopyObjectRequest

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));
}
Also used : OSSObjectOperation(com.aliyun.oss.internal.OSSObjectOperation) HashMap(java.util.HashMap) DefaultCredentialProvider(com.aliyun.oss.common.auth.DefaultCredentialProvider) Method(java.lang.reflect.Method) DefaultCredentials(com.aliyun.oss.common.auth.DefaultCredentials) CopyObjectRequest(com.aliyun.oss.model.CopyObjectRequest) DefaultServiceClient(com.aliyun.oss.common.comm.DefaultServiceClient) Test(org.junit.Test)

Example 8 with CopyObjectRequest

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);
        }
    }
}
Also used : ListObjectsRequest(com.aliyun.oss.model.ListObjectsRequest) OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) CopyObjectRequest(com.aliyun.oss.model.CopyObjectRequest) SegmentLoadingException(org.apache.druid.segment.loading.SegmentLoadingException) ObjectListing(com.aliyun.oss.model.ObjectListing) OSSException(com.aliyun.oss.OSSException) ISE(org.apache.druid.java.util.common.ISE) OSS(com.aliyun.oss.OSS) IOE(org.apache.druid.java.util.common.IOE)

Aggregations

CopyObjectRequest (com.aliyun.oss.model.CopyObjectRequest)8 OSSException (com.aliyun.oss.OSSException)6 CopyObjectResult (com.aliyun.oss.model.CopyObjectResult)4 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)4 PutObjectResult (com.aliyun.oss.model.PutObjectResult)4 Test (org.junit.Test)4 OSSObject (com.aliyun.oss.model.OSSObject)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Ignore (org.junit.Ignore)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2 OSS (com.aliyun.oss.OSS)1 DefaultCredentialProvider (com.aliyun.oss.common.auth.DefaultCredentialProvider)1 DefaultCredentials (com.aliyun.oss.common.auth.DefaultCredentials)1 DefaultServiceClient (com.aliyun.oss.common.comm.DefaultServiceClient)1 OSSObjectOperation (com.aliyun.oss.internal.OSSObjectOperation)1 ListObjectsRequest (com.aliyun.oss.model.ListObjectsRequest)1 OSSObjectSummary (com.aliyun.oss.model.OSSObjectSummary)1 ObjectAcl (com.aliyun.oss.model.ObjectAcl)1 ObjectListing (com.aliyun.oss.model.ObjectListing)1