Search in sources :

Example 1 with TransferProgressUpdatingListener

use of com.qcloud.cos.event.TransferProgressUpdatingListener in project cos-java-sdk-v5 by tencentyun.

the class TransferManager method copy.

/**
 * <p>
 * Schedules a new transfer to copy data from one object to another . This method is
 * non-blocking and returns immediately (i.e. before the copy has finished).
 * </p>
 * <p>
 * Note: You need to use this method if the {@link TransferManager} is created with a regional
 * COS client and the source & destination buckets are in different regions.
 * </p>
 * <p>
 * <code>TransferManager</code> doesn't support copying of encrypted objects whose encryption
 * materials are stored in an instruction file.
 * </p>
 * <p>
 * Use the returned <code>Copy</code> object to check if the copy is complete.
 * </p>
 * <p>
 * If resources are available, the copy request will begin immediately. Otherwise, the copy is
 * scheduled and started as soon as resources become available.
 * </p>
 *
 * <p>
 * <b>Note:</b> If the {@link TransferManager} is created with a regional COS client and the
 * source & destination buckets are in different regions, use the
 * {@link #copy(CopyObjectRequest, COS, TransferStateChangeListener)} method.
 * </p>
 *
 * @param copyObjectRequest The request containing all the parameters for the copy.
 * @param srcCOS An COS client constructed for the region in which the source object's bucket is
 *        located.
 * @param stateChangeListener The transfer state change listener to monitor the copy request
 * @return A new <code>Copy</code> object to use to check the state of the copy request being
 *         processed.
 * @throws CosClientException If any errors are encountered in the client while making the
 *         request or handling the response.
 * @throws CosServiceException If any errors occurred in Qcloud COS while processing the
 *         request.
 */
public Copy copy(final CopyObjectRequest copyObjectRequest, final COS srcCOS, final TransferStateChangeListener stateChangeListener) throws CosServiceException, CosClientException {
    appendSingleObjectUserAgent(copyObjectRequest);
    assertParameterNotNull(copyObjectRequest.getSourceBucketName(), "The source bucket name must be specified when a copy request is initiated.");
    assertParameterNotNull(copyObjectRequest.getSourceKey(), "The source object key must be specified when a copy request is initiated.");
    assertParameterNotNull(copyObjectRequest.getDestinationBucketName(), "The destination bucket name must be specified when a copy request is initiated.");
    assertParameterNotNull(copyObjectRequest.getDestinationKey(), "The destination object key must be specified when a copy request is initiated.");
    assertParameterNotNull(srcCOS, "The srcCOS parameter is mandatory");
    String description = "Copying object from " + copyObjectRequest.getSourceBucketName() + "/" + copyObjectRequest.getSourceKey() + " to " + copyObjectRequest.getDestinationBucketName() + "/" + copyObjectRequest.getDestinationKey();
    GetObjectMetadataRequest getObjectMetadataRequest = new GetObjectMetadataRequest(copyObjectRequest.getSourceBucketName(), copyObjectRequest.getSourceKey()).withVersionId(copyObjectRequest.getSourceVersionId());
    ObjectMetadata metadata = srcCOS.getObjectMetadata(getObjectMetadataRequest);
    TransferProgress transferProgress = new TransferProgress();
    transferProgress.setTotalBytesToTransfer(metadata.getContentLength());
    ProgressListenerChain listenerChain = new ProgressListenerChain(new TransferProgressUpdatingListener(transferProgress));
    CopyImpl copy = new CopyImpl(description, transferProgress, listenerChain, stateChangeListener);
    CopyCallable copyCallable = new CopyCallable(this, threadPool, copy, copyObjectRequest, metadata, listenerChain);
    CopyMonitor watcher = CopyMonitor.create(this, copy, threadPool, copyCallable, copyObjectRequest, listenerChain);
    copy.setMonitor(watcher);
    return copy;
}
Also used : GetObjectMetadataRequest(com.qcloud.cos.model.GetObjectMetadataRequest) CopyImpl(com.qcloud.cos.internal.CopyImpl) COSProgressListenerChain(com.qcloud.cos.event.COSProgressListenerChain) ProgressListenerChain(com.qcloud.cos.event.ProgressListenerChain) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) TransferProgressUpdatingListener(com.qcloud.cos.event.TransferProgressUpdatingListener) MultipleFileTransferProgressUpdatingListener(com.qcloud.cos.event.MultipleFileTransferProgressUpdatingListener)

Example 2 with TransferProgressUpdatingListener

use of com.qcloud.cos.event.TransferProgressUpdatingListener in project cos-java-sdk-v5 by tencentyun.

the class TransferManager method doDownload.

/**
 * Same as public interface, but adds a state listener so that callers can be notified of state
 * changes to the download.
 *
 * @see TransferManager#download(GetObjectRequest, File)
 */
private Download doDownload(final GetObjectRequest getObjectRequest, final File file, final TransferStateChangeListener stateListener, final COSProgressListener cosProgressListener, final boolean resumeExistingDownload) {
    appendSingleObjectUserAgent(getObjectRequest);
    String description = "Downloading from " + getObjectRequest.getBucketName() + "/" + getObjectRequest.getKey();
    TransferProgress transferProgress = new TransferProgress();
    // COS progress listener to capture the persistable transfer when available
    COSProgressListenerChain listenerChain = new COSProgressListenerChain(// The listener for updating transfer progress
    new TransferProgressUpdatingListener(transferProgress), getObjectRequest.getGeneralProgressListener(), // Listeners
    cosProgressListener);
    // included in
    // the original
    // request
    // The listener chain used by the low-level GetObject request.
    // This listener chain ignores any COMPLETE event, so that we could
    // delay firing the signal until the high-level download fully finishes.
    getObjectRequest.setGeneralProgressListener(new ProgressListenerChain(new TransferCompletionFilter(), listenerChain));
    long startingByte = 0;
    long lastByte;
    long[] range = getObjectRequest.getRange();
    if (range != null && range.length == 2) {
        startingByte = range[0];
        lastByte = range[1];
    } else {
        GetObjectMetadataRequest getObjectMetadataRequest = new GetObjectMetadataRequest(getObjectRequest.getBucketName(), getObjectRequest.getKey());
        if (getObjectRequest.getSSECustomerKey() != null)
            getObjectMetadataRequest.setSSECustomerKey(getObjectRequest.getSSECustomerKey());
        if (getObjectRequest.getVersionId() != null)
            getObjectMetadataRequest.setVersionId(getObjectRequest.getVersionId());
        final ObjectMetadata objectMetadata = cos.getObjectMetadata(getObjectMetadataRequest);
        lastByte = objectMetadata.getContentLength() - 1;
    }
    final long origStartingByte = startingByte;
    // We still pass the unfiltered listener chain into DownloadImpl
    final DownloadImpl download = new DownloadImpl(description, transferProgress, listenerChain, null, stateListener, getObjectRequest, file);
    long totalBytesToDownload = lastByte - startingByte + 1;
    transferProgress.setTotalBytesToTransfer(totalBytesToDownload);
    long fileLength = -1;
    if (resumeExistingDownload) {
        if (!FileLocks.lock(file)) {
            throw new FileLockException("Fail to lock " + file + " for resume download");
        }
        try {
            if (file.exists()) {
                fileLength = file.length();
                startingByte = startingByte + fileLength;
                getObjectRequest.setRange(startingByte, lastByte);
                transferProgress.updateProgress(Math.min(fileLength, totalBytesToDownload));
                totalBytesToDownload = lastByte - startingByte + 1;
                if (log.isDebugEnabled()) {
                    log.debug("Resume download: totalBytesToDownload=" + totalBytesToDownload + ", origStartingByte=" + origStartingByte + ", startingByte=" + startingByte + ", lastByte=" + lastByte + ", numberOfBytesRead=" + fileLength + ", file: " + file);
                }
            }
        } finally {
            FileLocks.unlock(file);
        }
    }
    if (totalBytesToDownload < 0) {
        throw new IllegalArgumentException("Unable to determine the range for download operation.");
    }
    final CountDownLatch latch = new CountDownLatch(1);
    Future<?> future = threadPool.submit(new DownloadCallable(cos, latch, getObjectRequest, resumeExistingDownload, download, file, origStartingByte, fileLength));
    download.setMonitor(new DownloadMonitor(download, future));
    latch.countDown();
    return download;
}
Also used : FileLockException(com.qcloud.cos.exception.FileLockException) TransferCompletionFilter(com.qcloud.cos.event.TransferCompletionFilter) CountDownLatch(java.util.concurrent.CountDownLatch) TransferProgressUpdatingListener(com.qcloud.cos.event.TransferProgressUpdatingListener) MultipleFileTransferProgressUpdatingListener(com.qcloud.cos.event.MultipleFileTransferProgressUpdatingListener) GetObjectMetadataRequest(com.qcloud.cos.model.GetObjectMetadataRequest) COSProgressListenerChain(com.qcloud.cos.event.COSProgressListenerChain) COSProgressListenerChain(com.qcloud.cos.event.COSProgressListenerChain) ProgressListenerChain(com.qcloud.cos.event.ProgressListenerChain) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 3 with TransferProgressUpdatingListener

use of com.qcloud.cos.event.TransferProgressUpdatingListener in project cos-java-sdk-v5 by tencentyun.

the class TransferManager method doResumableDownload.

private Download doResumableDownload(final GetObjectRequest getObjectRequest, final File destFile, final TransferStateChangeListener stateListener, final COSProgressListener cosProgressListener, String resumableTaskFilePath, int multiThreadThreshold, int partSize) {
    PersistableResumeDownload downloadRecord = getPersistableResumeRecord(getObjectRequest, destFile, resumableTaskFilePath);
    long bytesToDownload = Long.parseLong(downloadRecord.getContentLength());
    if (bytesToDownload < multiThreadThreshold) {
        downloadRecord.getDumpFile().delete();
        return doDownload(getObjectRequest, destFile, stateListener, cosProgressListener, OVERWRITE_MODE);
    }
    appendSingleObjectUserAgent(getObjectRequest);
    String description = "Resumable downloading from " + getObjectRequest.getBucketName() + "/" + getObjectRequest.getKey();
    TransferProgress transferProgress = new TransferProgress();
    // COS progress listener to capture the persistable transfer when available
    COSProgressListenerChain listenerChain = new COSProgressListenerChain(// The listener for updating transfer progress
    new TransferProgressUpdatingListener(transferProgress), getObjectRequest.getGeneralProgressListener(), cosProgressListener);
    getObjectRequest.setGeneralProgressListener(new ProgressListenerChain(new TransferCompletionFilter(), listenerChain));
    RandomAccessFile destRandomAccessFile;
    FileChannel destFileChannel;
    try {
        destRandomAccessFile = new RandomAccessFile(destFile, "rw");
        destRandomAccessFile.setLength(bytesToDownload);
        destFileChannel = destRandomAccessFile.getChannel();
    } catch (Exception e) {
        throw new CosClientException("resumable download got exception:" + e.getCause().getMessage() + e.getMessage());
    }
    transferProgress.setTotalBytesToTransfer(bytesToDownload);
    DownloadImpl download = new DownloadImpl(description, transferProgress, listenerChain, null, stateListener, getObjectRequest, destFile);
    ResumableDownloadSubmitter submitter = new ResumableDownloadSubmitter(cos, threadPool, getObjectRequest, download, destFile, destRandomAccessFile, destFileChannel, downloadRecord, partSize, multiThreadThreshold, transferProgress, listenerChain);
    ResumableDownloadMonitor monitor = ResumableDownloadMonitor.create(listenerChain, submitter, download, threadPool, downloadRecord, destFile, destFileChannel);
    download.setMonitor(monitor);
    return download;
}
Also used : TransferCompletionFilter(com.qcloud.cos.event.TransferCompletionFilter) FileChannel(java.nio.channels.FileChannel) CosClientException(com.qcloud.cos.exception.CosClientException) AbortedException(com.qcloud.cos.exception.AbortedException) FileLockException(com.qcloud.cos.exception.FileLockException) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) IOException(java.io.IOException) TransferProgressUpdatingListener(com.qcloud.cos.event.TransferProgressUpdatingListener) MultipleFileTransferProgressUpdatingListener(com.qcloud.cos.event.MultipleFileTransferProgressUpdatingListener) RandomAccessFile(java.io.RandomAccessFile) COSProgressListenerChain(com.qcloud.cos.event.COSProgressListenerChain) COSProgressListenerChain(com.qcloud.cos.event.COSProgressListenerChain) ProgressListenerChain(com.qcloud.cos.event.ProgressListenerChain)

Example 4 with TransferProgressUpdatingListener

use of com.qcloud.cos.event.TransferProgressUpdatingListener in project cos-java-sdk-v5 by tencentyun.

the class TransferManager method doUpload.

/**
 * <p>
 * Schedules a new transfer to upload data to Qcloud COS. This method is non-blocking and
 * returns immediately (i.e. before the upload has finished).
 * </p>
 * <p>
 * Use the returned <code>Upload</code> object to query the progress of the transfer, add
 * listeners for progress events, and wait for the upload to complete.
 * </p>
 * <p>
 * If resources are available, the upload will begin immediately. Otherwise, the upload is
 * scheduled and started as soon as resources become available.
 * </p>
 *
 * @param putObjectRequest The request containing all the parameters for the upload.
 * @param stateListener The transfer state change listener to monitor the upload.
 * @param progressListener An optional callback listener to receive the progress of the upload.
 *
 * @return A new <code>Upload</code> object to use to check the state of the upload, listen for
 *         progress notifications, and otherwise manage the upload.
 *
 * @throws CosClientException If any errors are encountered in the client while making the
 *         request or handling the response.
 * @throws CosServiceException If any errors occurred in Qcloud COS while processing the
 *         request.
 */
private Upload doUpload(final PutObjectRequest putObjectRequest, final TransferStateChangeListener stateListener, final COSProgressListener progressListener, final PersistableUpload persistableUpload) throws CosServiceException, CosClientException {
    appendSingleObjectUserAgent(putObjectRequest);
    String multipartUploadId = persistableUpload != null ? persistableUpload.getMultipartUploadId() : null;
    if (putObjectRequest.getMetadata() == null)
        putObjectRequest.setMetadata(new ObjectMetadata());
    ObjectMetadata metadata = putObjectRequest.getMetadata();
    File file = TransferManagerUtils.getRequestFile(putObjectRequest);
    if (file != null) {
        // Always set the content length, even if it's already set
        metadata.setContentLength(file.length());
    } else {
        if (multipartUploadId != null) {
            throw new IllegalArgumentException("Unable to resume the upload. No file specified.");
        }
    }
    String description = "Uploading to " + putObjectRequest.getBucketName() + "/" + putObjectRequest.getKey();
    TransferProgress transferProgress = new TransferProgress();
    transferProgress.setTotalBytesToTransfer(TransferManagerUtils.getContentLength(putObjectRequest));
    COSProgressListenerChain listenerChain = new COSProgressListenerChain(new TransferProgressUpdatingListener(transferProgress), putObjectRequest.getGeneralProgressListener(), progressListener);
    putObjectRequest.setGeneralProgressListener(listenerChain);
    UploadImpl upload = new UploadImpl(description, transferProgress, listenerChain, stateListener);
    /**
     * Since we use the same thread pool for uploading individual parts and complete multi part
     * upload, there is a possibility that the tasks for complete multi-part upload will be
     * added to end of queue in case of multiple parallel uploads submitted. This may result in
     * a delay for processing the complete multi part upload request.
     */
    UploadCallable uploadCallable = new UploadCallable(this, threadPool, upload, putObjectRequest, listenerChain, multipartUploadId, transferProgress);
    UploadMonitor watcher = UploadMonitor.create(this, upload, threadPool, uploadCallable, putObjectRequest, listenerChain);
    upload.setMonitor(watcher);
    return upload;
}
Also used : COSProgressListenerChain(com.qcloud.cos.event.COSProgressListenerChain) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) TransferProgressUpdatingListener(com.qcloud.cos.event.TransferProgressUpdatingListener) MultipleFileTransferProgressUpdatingListener(com.qcloud.cos.event.MultipleFileTransferProgressUpdatingListener)

Example 5 with TransferProgressUpdatingListener

use of com.qcloud.cos.event.TransferProgressUpdatingListener in project cos-java-sdk-v5 by tencentyun.

the class TransferManager method doImageAuditing.

private ImageAuditingImpl doImageAuditing(ImageAuditingRequest request, MultipleFileTransferStateChangeListener transferListener) {
    appendImageAuditingUserAgent(request);
    String description = "send image auditing job ";
    TransferProgress transferProgress = new TransferProgress();
    COSProgressListenerChain listenerChain = new COSProgressListenerChain(new TransferProgressUpdatingListener(transferProgress), request.getGeneralProgressListener());
    ImageAuditingImpl imageAuditing = new ImageAuditingImpl(description, transferProgress, listenerChain, transferListener, request);
    final CountDownLatch latch = new CountDownLatch(1);
    Future<?> future = threadPool.submit(new ImageAuditingCallable(cos, latch, request, imageAuditing));
    imageAuditing.setMonitor(new ImageAuditingMonitor(imageAuditing, future));
    latch.countDown();
    return imageAuditing;
}
Also used : COSProgressListenerChain(com.qcloud.cos.event.COSProgressListenerChain) CountDownLatch(java.util.concurrent.CountDownLatch) TransferProgressUpdatingListener(com.qcloud.cos.event.TransferProgressUpdatingListener) MultipleFileTransferProgressUpdatingListener(com.qcloud.cos.event.MultipleFileTransferProgressUpdatingListener)

Aggregations

COSProgressListenerChain (com.qcloud.cos.event.COSProgressListenerChain)5 MultipleFileTransferProgressUpdatingListener (com.qcloud.cos.event.MultipleFileTransferProgressUpdatingListener)5 TransferProgressUpdatingListener (com.qcloud.cos.event.TransferProgressUpdatingListener)5 ProgressListenerChain (com.qcloud.cos.event.ProgressListenerChain)3 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)3 TransferCompletionFilter (com.qcloud.cos.event.TransferCompletionFilter)2 FileLockException (com.qcloud.cos.exception.FileLockException)2 GetObjectMetadataRequest (com.qcloud.cos.model.GetObjectMetadataRequest)2 RandomAccessFile (java.io.RandomAccessFile)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AbortedException (com.qcloud.cos.exception.AbortedException)1 CosClientException (com.qcloud.cos.exception.CosClientException)1 CosServiceException (com.qcloud.cos.exception.CosServiceException)1 CopyImpl (com.qcloud.cos.internal.CopyImpl)1 File (java.io.File)1 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1