Search in sources :

Example 91 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class TransferManager method getPersistableResumeRecord.

private PersistableResumeDownload getPersistableResumeRecord(GetObjectRequest getObjectRequest, File destFile, String resumableTaskFilePath) {
    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);
    long cosContentLength = objectMetadata.getContentLength();
    long cosLastModified = objectMetadata.getLastModified().getTime();
    String cosEtag = objectMetadata.getETag();
    String cosCrc64 = objectMetadata.getCrc64Ecma();
    File resumableTaskFile;
    if (resumableTaskFilePath == null || resumableTaskFilePath == "") {
        resumableTaskFile = new File(destFile.getAbsolutePath() + ".cosresumabletask");
    } else {
        resumableTaskFile = new File(resumableTaskFilePath);
    }
    PersistableResumeDownload downloadRecord = null;
    FileInputStream is = null;
    try {
        // attempt to create the parent if it doesn't exist
        File parentDirectory = resumableTaskFile.getParentFile();
        if (parentDirectory != null && !parentDirectory.exists()) {
            if (!(parentDirectory.mkdirs())) {
                throw new CosClientException("Unable to create directory in the path" + parentDirectory.getAbsolutePath());
            }
        }
        if (!resumableTaskFile.exists()) {
            resumableTaskFile.createNewFile();
        }
        is = new FileInputStream(resumableTaskFile);
        downloadRecord = PersistableResumeDownload.deserializeFrom(is);
        log.info("deserialize download record from " + resumableTaskFile.getAbsolutePath() + "record: " + downloadRecord.serialize());
    } catch (IOException e) {
        throw new CosClientException("can not create file" + resumableTaskFile.getAbsolutePath() + e);
    } catch (IllegalArgumentException e) {
        log.warn("resumedownload task file cannot deserialize" + e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                throw new CosClientException("can not close input stream " + resumableTaskFile.getAbsolutePath() + e);
            }
        }
    }
    if (downloadRecord == null || downloadRecord.getLastModified() != cosLastModified || !downloadRecord.getContentLength().equals(Long.toString(cosContentLength)) || !downloadRecord.getEtag().equals(cosEtag) || !downloadRecord.getCrc64ecma().equals(cosCrc64)) {
        HashMap<String, Integer> downloadedBlocks = new HashMap<String, Integer>();
        downloadRecord = new PersistableResumeDownload(cosLastModified, Long.toString(cosContentLength), cosEtag, cosCrc64, downloadedBlocks);
    }
    downloadRecord.setDumpFile(resumableTaskFile);
    return downloadRecord;
}
Also used : HashMap(java.util.HashMap) CosClientException(com.qcloud.cos.exception.CosClientException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GetObjectMetadataRequest(com.qcloud.cos.model.GetObjectMetadataRequest) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 92 with CosClientException

use of com.qcloud.cos.exception.CosClientException 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 93 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class RangeDownloadCallable method call.

public DownloadPart call() throws Exception {
    COSObject object = cos.getObject(request);
    InputStream input = object.getObjectContent();
    ObjectMetadata meta = object.getObjectMetadata();
    long[] range = request.getRange();
    long start = range[0];
    long end = range[1];
    long position = start;
    ByteBuffer tmpBuf = ByteBuffer.allocateDirect(1024 * 1024);
    byte[] buffer = new byte[1024 * 10];
    int bytesRead;
    CRC64 crc64 = new CRC64();
    while ((bytesRead = input.read(buffer)) > -1) {
        start += bytesRead;
        crc64.update(buffer, bytesRead);
        if (tmpBuf.remaining() < bytesRead) {
            tmpBuf.flip();
            position += destFileChannel.write(tmpBuf, position);
            tmpBuf.clear();
        }
        tmpBuf.put(buffer, 0, bytesRead);
    }
    tmpBuf.flip();
    destFileChannel.write(tmpBuf, position);
    if (start != end + 1) {
        destFileChannel.close();
        destFile.delete();
        String msg = String.format("get object want %d bytes, but got %d bytes, reqeust_id: %s", end + 1, start, meta.getRequestId());
        throw new CosClientException(msg);
    }
    String block = String.format("%d-%d", range[0], range[1]);
    downloadRecord.putDownloadedBlocks(block);
    downloadRecord.dump();
    return new DownloadPart(range[0], range[1], crc64.getValue());
}
Also used : CRC64(com.qcloud.cos.utils.CRC64) COSObject(com.qcloud.cos.model.COSObject) InputStream(java.io.InputStream) CosClientException(com.qcloud.cos.exception.CosClientException) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) ByteBuffer(java.nio.ByteBuffer)

Example 94 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class ResumableDownloadMonitor method checkCRC.

void checkCRC(List<DownloadPart> downloadParts) throws IOException {
    Collections.sort(downloadParts, new Comparator<DownloadPart>() {

        @Override
        public int compare(DownloadPart part1, DownloadPart part2) {
            return (int) (part1.start - part2.start);
        }
    });
    CRC64 crc64 = new CRC64();
    for (DownloadPart part : downloadParts) {
        crc64 = CRC64.combine(crc64, new CRC64(part.crc64), part.getContentLength());
    }
    long crc64Download = crc64.getValue();
    long crc64Cos = crc64ToLong(downloadRecord.getCrc64ecma());
    log.debug("download crc " + crc64Download + " cos crc " + crc64Cos);
    if (crc64Download != crc64Cos) {
        destFile.delete();
        throw new CosClientException("download file has diff crc64 with cos file, cos: " + crc64Cos + " downloaded: " + crc64Download);
    }
}
Also used : CRC64(com.qcloud.cos.utils.CRC64) CosClientException(com.qcloud.cos.exception.CosClientException)

Example 95 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class ServiceUtils method retryableDownloadCOSObjectToFile.

/**
 * Gets an object stored in COS and downloads it into the specified file.
 * This method includes the one-time retry mechanism after integrity check failure
 * on the downloaded file. It will also return immediately after getting null valued
 * COSObject (when getObject request does not meet the specified constraints).
 *
 * @param file
 *             The file to store the object's data in.
 * @param retryableCOSDownloadTask
 *             The implementation of SafeCOSDownloadTask interface which allows user to
 *             get access to all the visible variables at the calling site of this method.
 */
public static COSObject retryableDownloadCOSObjectToFile(File file, RetryableCOSDownloadTask retryableCOSDownloadTask, boolean appendData) {
    boolean hasRetried = false;
    boolean needRetry;
    COSObject cosObject;
    do {
        needRetry = false;
        cosObject = retryableCOSDownloadTask.getCOSObjectStream();
        if (cosObject == null)
            return null;
        try {
            ServiceUtils.downloadObjectToFile(cosObject, file, retryableCOSDownloadTask.needIntegrityCheck(), appendData);
        } catch (CosClientException cse) {
            if (!cse.isRetryable()) {
                cosObject.getObjectContent().abort();
                throw cse;
            }
            // The current code will retry the download only when case 2) or 3) happens.
            if (cse.getCause() instanceof SocketException || cse.getCause() instanceof SSLProtocolException) {
                throw cse;
            } else {
                needRetry = true;
                if (hasRetried) {
                    cosObject.getObjectContent().abort();
                    throw cse;
                } else {
                    log.info("Retry the download of object " + cosObject.getKey() + " (bucket " + cosObject.getBucketName() + ")", cse);
                    hasRetried = true;
                }
            }
        }
    } while (needRetry);
    return cosObject;
}
Also used : SSLProtocolException(javax.net.ssl.SSLProtocolException) SocketException(java.net.SocketException) COSObject(com.qcloud.cos.model.COSObject) CosClientException(com.qcloud.cos.exception.CosClientException)

Aggregations

CosClientException (com.qcloud.cos.exception.CosClientException)111 CosServiceException (com.qcloud.cos.exception.CosServiceException)64 COSCredentials (com.qcloud.cos.auth.COSCredentials)41 ClientConfig (com.qcloud.cos.ClientConfig)39 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)39 Region (com.qcloud.cos.region.Region)39 COSClient (com.qcloud.cos.COSClient)37 IOException (java.io.IOException)31 File (java.io.File)28 ByteArrayInputStream (java.io.ByteArrayInputStream)18 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)15 TransferManager (com.qcloud.cos.transfer.TransferManager)14 ExecutorService (java.util.concurrent.ExecutorService)14 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)13 URISyntaxException (java.net.URISyntaxException)13 MultiObjectDeleteException (com.qcloud.cos.exception.MultiObjectDeleteException)12 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)12 SecretKey (javax.crypto.SecretKey)12 MalformedURLException (java.net.MalformedURLException)11 PutObjectResult (com.qcloud.cos.model.PutObjectResult)10