Search in sources :

Example 1 with COSProgressListenerChain

use of com.qcloud.cos.event.COSProgressListenerChain 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 2 with COSProgressListenerChain

use of com.qcloud.cos.event.COSProgressListenerChain 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 3 with COSProgressListenerChain

use of com.qcloud.cos.event.COSProgressListenerChain 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 4 with COSProgressListenerChain

use of com.qcloud.cos.event.COSProgressListenerChain 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)4 MultipleFileTransferProgressUpdatingListener (com.qcloud.cos.event.MultipleFileTransferProgressUpdatingListener)4 TransferProgressUpdatingListener (com.qcloud.cos.event.TransferProgressUpdatingListener)4 ProgressListenerChain (com.qcloud.cos.event.ProgressListenerChain)2 TransferCompletionFilter (com.qcloud.cos.event.TransferCompletionFilter)2 FileLockException (com.qcloud.cos.exception.FileLockException)2 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)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 GetObjectMetadataRequest (com.qcloud.cos.model.GetObjectMetadataRequest)1 File (java.io.File)1 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1