Search in sources :

Example 21 with ObjectMetadata

use of com.qcloud.cos.model.ObjectMetadata in project cos-java-sdk-v5 by tencentyun.

the class UploadCallable method uploadPartsInSeries.

/**
 * Uploads all parts in the request in serial in this thread, then completes the upload and
 * returns the result.
 */
private UploadResult uploadPartsInSeries(UploadPartRequestFactory requestFactory) {
    final List<PartETag> partETags = new ArrayList<PartETag>();
    while (requestFactory.hasMoreRequests()) {
        if (threadPool.isShutdown())
            throw new CancellationException("TransferManager has been shutdown");
        UploadPartRequest uploadPartRequest = requestFactory.getNextUploadPartRequest();
        // Mark the stream in case we need to reset it
        InputStream inputStream = uploadPartRequest.getInputStream();
        if (inputStream != null && inputStream.markSupported()) {
            if (uploadPartRequest.getPartSize() >= Integer.MAX_VALUE) {
                inputStream.mark(Integer.MAX_VALUE);
            } else {
                inputStream.mark((int) uploadPartRequest.getPartSize());
            }
        }
        partETags.add(cos.uploadPart(uploadPartRequest).getPartETag());
    }
    CompleteMultipartUploadRequest req = new CompleteMultipartUploadRequest(origReq.getBucketName(), origReq.getKey(), multipartUploadId, partETags).withGeneralProgressListener(origReq.getGeneralProgressListener());
    ObjectMetadata origMeta = origReq.getMetadata();
    if (origMeta != null) {
        ObjectMetadata objMeta = req.getObjectMetadata();
        if (objMeta == null) {
            objMeta = new ObjectMetadata();
        }
        objMeta.setUserMetadata(origMeta.getUserMetadata());
        req.setObjectMetadata(objMeta);
    }
    if (origReq.getPicOperations() != null) {
        req.setPicOperations(origReq.getPicOperations());
    }
    TransferManagerUtils.populateEndpointAddr(origReq, req);
    CompleteMultipartUploadResult res = cos.completeMultipartUpload(req);
    UploadResult uploadResult = new UploadResult();
    uploadResult.setBucketName(res.getBucketName());
    uploadResult.setKey(res.getKey());
    uploadResult.setETag(res.getETag());
    uploadResult.setVersionId(res.getVersionId());
    uploadResult.setRequestId(res.getRequestId());
    uploadResult.setDateStr(res.getDateStr());
    uploadResult.setCrc64Ecma(res.getCrc64Ecma());
    uploadResult.setCiUploadResult(res.getCiUploadResult());
    return uploadResult;
}
Also used : CancellationException(java.util.concurrent.CancellationException) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) UploadPartRequest(com.qcloud.cos.model.UploadPartRequest) CompleteMultipartUploadResult(com.qcloud.cos.model.CompleteMultipartUploadResult) CompleteMultipartUploadResult(com.qcloud.cos.model.CompleteMultipartUploadResult) UploadResult(com.qcloud.cos.model.UploadResult) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) PartETag(com.qcloud.cos.model.PartETag) CompleteMultipartUploadRequest(com.qcloud.cos.model.CompleteMultipartUploadRequest)

Example 22 with ObjectMetadata

use of com.qcloud.cos.model.ObjectMetadata in project cos-java-sdk-v5 by tencentyun.

the class PutGetDelTest method testPutObjectWithContentType.

@Test
public void testPutObjectWithContentType() throws IOException {
    ObjectMetadata originObjectMeta = new ObjectMetadata();
    originObjectMeta.setContentType("image/tiff");
    testPutObjectByStreamDiffSize(0L, originObjectMeta);
}
Also used : ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) Test(org.junit.Test)

Example 23 with ObjectMetadata

use of com.qcloud.cos.model.ObjectMetadata in project cos-java-sdk-v5 by tencentyun.

the class PutGetDelTest method testPutObjectWithServerSideEncryption.

@Test
public void testPutObjectWithServerSideEncryption() throws IOException {
    useServerEncryption = true;
    ObjectMetadata originObjectMeta = new ObjectMetadata();
    originObjectMeta.setServerSideEncryption("AES256");
    testPutObjectByStreamDiffSize(1L, originObjectMeta);
    useServerEncryption = false;
}
Also used : ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) Test(org.junit.Test)

Example 24 with ObjectMetadata

use of com.qcloud.cos.model.ObjectMetadata 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 25 with ObjectMetadata

use of com.qcloud.cos.model.ObjectMetadata in project cos-java-sdk-v5 by tencentyun.

the class CompleteMultipartCopy method call.

@Override
public CopyResult call() throws Exception {
    CompleteMultipartUploadResult res;
    try {
        CompleteMultipartUploadRequest req = new CompleteMultipartUploadRequest(origReq.getDestinationBucketName(), origReq.getDestinationKey(), uploadId, collectPartETags()).withGeneralProgressListener(origReq.getGeneralProgressListener());
        ObjectMetadata origMeta = origReq.getNewObjectMetadata();
        if (origMeta != null) {
            ObjectMetadata objMeta = req.getObjectMetadata();
            if (objMeta == null) {
                objMeta = new ObjectMetadata();
            }
            objMeta.setUserMetadata(origMeta.getUserMetadata());
            req.setObjectMetadata(objMeta);
        }
        TransferManagerUtils.populateEndpointAddr(origReq, req);
        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());
    copyResult.setCrc64Ecma(res.getCrc64Ecma());
    monitor.copyComplete();
    return copyResult;
}
Also used : CompleteMultipartUploadResult(com.qcloud.cos.model.CompleteMultipartUploadResult) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) CosClientException(com.qcloud.cos.exception.CosClientException) CompleteMultipartUploadRequest(com.qcloud.cos.model.CompleteMultipartUploadRequest) CopyResult(com.qcloud.cos.model.CopyResult)

Aggregations

ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)64 File (java.io.File)21 COSClient (com.qcloud.cos.COSClient)15 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)15 ClientConfig (com.qcloud.cos.ClientConfig)14 Region (com.qcloud.cos.region.Region)14 CosClientException (com.qcloud.cos.exception.CosClientException)13 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)12 COSCredentials (com.qcloud.cos.auth.COSCredentials)12 PutObjectResult (com.qcloud.cos.model.PutObjectResult)12 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)11 Test (org.junit.Test)11 CosServiceException (com.qcloud.cos.exception.CosServiceException)10 IOException (java.io.IOException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 InputStream (java.io.InputStream)7 GetObjectMetadataRequest (com.qcloud.cos.model.GetObjectMetadataRequest)6 UploadResult (com.qcloud.cos.model.UploadResult)5 TransferManager (com.qcloud.cos.transfer.TransferManager)5 Upload (com.qcloud.cos.transfer.Upload)5