Search in sources :

Example 66 with ObjectMetadata

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

the class CosMetadataResponseHandler method handle.

@Override
public CosServiceResponse<ObjectMetadata> handle(CosHttpResponse response) throws Exception {
    ObjectMetadata metadata = new ObjectMetadata();
    populateObjectMetadata(response, metadata);
    CosServiceResponse<ObjectMetadata> cosResponse = parseResponseMetadata(response);
    cosResponse.setResult(metadata);
    return cosResponse;
}
Also used : ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 67 with ObjectMetadata

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

the class COSObjectResponseHandler method handle.

@Override
public CosServiceResponse<COSObject> handle(CosHttpResponse response) throws Exception {
    COSObject object = new COSObject();
    CosServiceResponse<COSObject> cosResponse = parseResponseMetadata(response);
    ObjectMetadata metadata = object.getObjectMetadata();
    populateObjectMetadata(response, metadata);
    object.setObjectContent(new COSObjectInputStream(response.getContent(), response.getHttpRequest()));
    cosResponse.setResult(object);
    return cosResponse;
}
Also used : COSObject(com.qcloud.cos.model.COSObject) COSObjectInputStream(com.qcloud.cos.model.COSObjectInputStream) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 68 with ObjectMetadata

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

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

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

the class AbstractCOSClientTest method headMultiPartObject.

protected static ObjectMetadata headMultiPartObject(String key, long expectedLength, int expectedPartNum) {
    ObjectMetadata objectMetadata = cosclient.getObjectMetadata(new GetObjectMetadataRequest(bucket, key));
    if (!useClientEncryption) {
        assertEquals(expectedLength, objectMetadata.getContentLength());
    }
    String etag = objectMetadata.getETag();
    assertTrue(etag.contains("-"));
    try {
        int etagPartNum = Integer.valueOf(etag.substring(etag.indexOf("-") + 1));
        assertEquals(expectedPartNum, etagPartNum);
    } catch (NumberFormatException e) {
        fail("part number in etag is invalid. etag: " + etag);
    }
    assertNotNull(objectMetadata.getLastModified());
    return objectMetadata;
}
Also used : GetObjectMetadataRequest(com.qcloud.cos.model.GetObjectMetadataRequest) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Aggregations

ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)77 CosClientException (com.qcloud.cos.exception.CosClientException)26 CosServiceException (com.qcloud.cos.exception.CosServiceException)23 IOException (java.io.IOException)22 File (java.io.File)21 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)17 COSClient (com.qcloud.cos.COSClient)15 ClientConfig (com.qcloud.cos.ClientConfig)14 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)14 PutObjectResult (com.qcloud.cos.model.PutObjectResult)14 Region (com.qcloud.cos.region.Region)14 ResponseNotCompleteException (com.qcloud.cos.exception.ResponseNotCompleteException)13 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)12 COSCredentials (com.qcloud.cos.auth.COSCredentials)12 Test (org.junit.Test)11 GetObjectMetadataRequest (com.qcloud.cos.model.GetObjectMetadataRequest)9 ByteArrayInputStream (java.io.ByteArrayInputStream)9 InputStream (java.io.InputStream)8 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)7 COSObject (com.qcloud.cos.model.COSObject)6