All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.branch.cos.transfer.CompleteMultipartCopy Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2010-2019 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.
 
 * According to cos feature, we modify some class,comment, field name, etc.
 */


package com.branch.cos.transfer;

import static com.branch.cos.event.SDKProgressPublisher.publishProgress;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import com.branch.cos.COS;
import com.branch.cos.event.ProgressEventType;
import com.branch.cos.event.ProgressListenerChain;
import com.branch.cos.exception.CosClientException;
import com.branch.cos.model.CompleteMultipartUploadRequest;
import com.branch.cos.model.CompleteMultipartUploadResult;
import com.branch.cos.model.CopyObjectRequest;
import com.branch.cos.model.CopyResult;
import com.branch.cos.model.PartETag;


/**
 * Initiates a complete multi-part upload request for a TransferManager multi-part parallel copy.
 */
public class CompleteMultipartCopy implements Callable {

    /** The upload id associated with the multi-part copy. */
    private final String uploadId;

    /**
     * The reference to underlying COS client to be used for initiating requests to COS.
     */
    private final COS cos;

    /** The reference to the request initiated by the user. */
    private final CopyObjectRequest origReq;

    /** The futures of threads that copies individual parts. */
    private final List> futures;

    /** The monitor to which the copy progress has to be communicated. */
    private final CopyMonitor monitor;

    /** The listener where progress of the copy needs to be published. */
    private final ProgressListenerChain listener;

    public CompleteMultipartCopy(String uploadId, COS cos, CopyObjectRequest copyObjectRequest,
            List> futures, ProgressListenerChain progressListenerChain,
            CopyMonitor monitor) {
        this.uploadId = uploadId;
        this.cos = cos;
        this.origReq = copyObjectRequest;
        this.futures = futures;
        this.listener = progressListenerChain;
        this.monitor = monitor;
    }

    @Override
    public CopyResult call() throws Exception {
        CompleteMultipartUploadResult res;

        try {
            CompleteMultipartUploadRequest req =
                    new CompleteMultipartUploadRequest(origReq.getDestinationBucketName(),
                            origReq.getDestinationKey(), uploadId, collectPartETags())
                                    .withGeneralProgressListener(
                                            origReq.getGeneralProgressListener());
            res = cos.completeMultipartUpload(req);
        } catch (Exception e) {
            publishProgress(listener, ProgressEventType.TRANSFER_FAILED_EVENT);
            throw e;
        }

        CopyResult copyResult = new CopyResult();
        copyResult.setSourceBucketName(origReq.getSourceBucketName());
        copyResult.setSourceKey(origReq.getSourceKey());
        copyResult.setDestinationBucketName(res.getBucketName());
        copyResult.setDestinationKey(res.getKey());
        copyResult.setETag(res.getETag());
        copyResult.setVersionId(res.getVersionId());
        copyResult.setRequestId(res.getRequestId());
        copyResult.setDateStr(res.getDateStr());

        monitor.copyComplete();

        return copyResult;
    }

    /**
     * Collects the Part ETags for initiating the complete multi-part copy request. This is blocking
     * as it waits until all the upload part threads complete.
     */
    private List collectPartETags() {

        final List partETags = new ArrayList();
        for (Future future : futures) {
            try {
                partETags.add(future.get());
            } catch (Exception e) {
                throw new CosClientException("Unable to copy part: " + e.getCause().getMessage(),
                        e.getCause());
            }
        }
        return partETags;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy