Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. This file is distributed
  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
  */
 package com.amazonaws.services.s3.transfer.internal;
 
 import static com.amazonaws.event.SDKProgressPublisher.publishProgress;
 
 import java.util.List;
 import java.util.Map;
 
 
An implementation of the Callable interface that helps TransferManager in carrying out the copy requests. The call method checks if multi-part copy request can be carried out on the Amazon S3 object. This is done by checking the size of the Amazon S3 object being copied.

If the size of the object is greater than the preferred size limit, then copy operation is carried out as multi part copy requests to the server.

If the size of the object is less than the preferred size limit, the copy operation is carried out in a single request where the Amazon S3 object is transferred as one chunk from the source bucket to the destination bucket.

 
 public class CopyCallable implements Callable<CopyResult> {

    
A reference to the Amazon S3 client using which copy or copy part requests are initiated.
 
     private final AmazonS3 s3;
    
Thread pool used during multi-part copy is performed.
 
     private final ExecutorService threadPool;
    
A reference to the original copy request received.
 
     private final CopyObjectRequest copyObjectRequest;
    
Upload id to be used when sending copy part requests.
 
     private String multipartUploadId;
    
Metadata of the object in the source bucket to be copied.
 
     private final ObjectMetadata metadata;
     private final CopyImpl copy;
 
     private static final Log log = LogFactory.getLog(CopyCallable.class);
    
TransferManager configuration that provides details on when to use multi-part copy, part size etc.,
 
     private final TransferManagerConfiguration configuration;
    
A list of future objects to be returned when multi-part copy is initiated.
 
     private final List<Future<PartETag>> futures = new ArrayList<Future<PartETag>>();
 
     private final ProgressListenerChain listenerChain;
 
     public CopyCallable(TransferManager transferManager,
             ExecutorService threadPoolCopyImpl copy,
             CopyObjectRequest copyObjectRequestObjectMetadata metadata,
             ProgressListenerChain progressListenerChain) {
        this. = transferManager.getAmazonS3Client();
        this. = transferManager.getConfiguration();
        this. = threadPool;
        this. = copyObjectRequest;
        this. = metadata;
        this. = progressListenerChain;
        this. = copy;
    }
        return ;
    }
        return ;
    }

    
Returns true if this CopyCallable is processing a multi-part copy.

Returns:
True if this CopyCallable is processing a multi-part copy.
    public boolean isMultipartCopy() {
        return (.getContentLength() > 
                .getMultipartCopyThreshold());
    }
    public CopyResult call() throws Exception {
        if (isMultipartCopy()) {
            copyInParts();
            return null;
        } else {
            return copyInOneChunk();
        }
    }

    
Performs the copy of the Amazon S3 object from source bucket to destination bucket. The Amazon S3 object is copied to destination in one single request.

Returns:
CopyResult response information from the server.
    private CopyResult copyInOneChunk() {
        CopyObjectResult copyObjectResult = .copyObject();
        CopyResult copyResult = new CopyResult();
        copyResult.setSourceKey(.getSourceKey());
                .getDestinationBucketName());
        copyResult.setETag(copyObjectResult.getETag());
        copyResult.setVersionId(copyObjectResult.getVersionId());
        return copyResult;
    }

    
Performs the copy of an Amazon S3 object from source bucket to destination bucket as multiple copy part requests. The information about the part to be copied is specified in the request as a byte range (first-last)

Throws:
java.lang.Exception Any Exception that occurs while carrying out the request.
    private void copyInParts() throws Exception {
        final String bucketName = .getDestinationBucketName();
        final String key = .getDestinationKey();
        long optimalPartSize = getOptimalPartSize(.getContentLength());
        try {
            CopyPartRequestFactory requestFactory = new CopyPartRequestFactory(
                    optimalPartSize,
                    .getContentLength());
            copyPartsInParallel(requestFactory);
        } catch (Exception e) {
            try {
                .abortMultipartUpload(new AbortMultipartUploadRequest(
                        bucketNamekey));
            } catch (Exception e2) {
                .info(
                        "Unable to abort multipart upload, you may need to manually remove uploaded parts: "
                                + e2.getMessage(), e2);
            }
            throw e;
        }
    }

    
Computes and returns the optimal part size for the copy operation.
    private long getOptimalPartSize(long contentLengthOfSource) {
        long optimalPartSize = TransferManagerUtils
                .calculateOptimalPartSizeForCopy(,
                        contentLengthOfSource);
        .debug("Calculated optimal part size: " + optimalPartSize);
        return optimalPartSize;
    }

    
Submits a callable for each part to be copied to our thread pool and records its corresponding Future.
    private void copyPartsInParallel(CopyPartRequestFactory requestFactory) {
        while (requestFactory.hasMoreRequests()) {
            if (.isShutdown())
                throw new CancellationException(
                        "TransferManager has been shutdown");
            CopyPartRequest request = requestFactory.getNextCopyPartRequest();
            .add(.submit(new CopyPartCallable(request)));
        }
    }

    
Initiates a multipart upload and returns the upload id
    private String initiateMultipartUpload(CopyObjectRequest origReq) {
                origReq.getDestinationBucketName(),
                origReq.getDestinationKey()).withCannedACL(
                origReq.getCannedAccessControlList());
        req.withAccessControlList(origReq.getAccessControlList())
           .withStorageClass(origReq.getStorageClass())
           .withSSECustomerKey(origReq.getDestinationSSECustomerKey())
           ;
        ObjectMetadata newObjectMetadata = origReq.getNewObjectMetadata();
        if (newObjectMetadata == null){
            newObjectMetadata = new ObjectMetadata();
        }
        if (newObjectMetadata.getContentType() == null){
            newObjectMetadata.setContentType(.getContentType());
        }
        req.setObjectMetadata(newObjectMetadata);
        populateMetadataWithEncryptionParams(,newObjectMetadata);
        String uploadId = .initiateMultipartUpload(req).getUploadId();
        .debug("Initiated new multipart upload: " + uploadId);
        return uploadId;
    }
    private void populateMetadataWithEncryptionParams(ObjectMetadata sourceObjectMetadata destination) {
        Map<StringStringuserMetadataSource = source.getUserMetadata();
        Map<StringStringuserMetadataDestination = destination.getUserMetadata();
        String[] headersToCopy = { .,
                ...,
                .,
                .,
                . };
        if (userMetadataSource != null) {
            if(userMetadataDestination == null){
                userMetadataDestinationnew HashMap<String,String>();
                destination.setUserMetadata(userMetadataDestination);
            }
            String headerValue;
            for(String header : headersToCopy){
                headerValue = userMetadataSource.get(header);
                if(headerValue != null){
                    userMetadataDestination.put(headerheaderValue);
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X