Search in sources :

Example 1 with GetObjectMetadataRequest

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

the class AbstractCOSClientTest method headSimpleObject.

protected static ObjectMetadata headSimpleObject(String key, long expectedLength, String expectedEtag) {
    ObjectMetadata objectMetadata = cosclient.getObjectMetadata(new GetObjectMetadataRequest(bucket, key));
    if (!useClientEncryption) {
        assertEquals(expectedLength, objectMetadata.getContentLength());
    } else {
        assertEquals(expectedLength, Long.valueOf(objectMetadata.getUserMetaDataOf(Headers.UNENCRYPTED_CONTENT_LENGTH)).longValue());
    }
    if (useClientEncryption) {
        assertEquals(false, expectedEtag.equals(objectMetadata.getETag()));
    } else {
        assertEquals(true, expectedEtag.equals(objectMetadata.getETag()));
    }
    assertNotNull(objectMetadata.getLastModified());
    return objectMetadata;
}
Also used : GetObjectMetadataRequest(com.qcloud.cos.model.GetObjectMetadataRequest) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 2 with GetObjectMetadataRequest

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

the class ListVersionsTest method headAndGetVersion.

private void headAndGetVersion(String key, String versionId, String expectedEtag, long expectedLength, File downloadLocalFile) throws CosServiceException, FileNotFoundException, IOException {
    GetObjectMetadataRequest getObjectMetadataRequest = new GetObjectMetadataRequest(bucket, key, versionId);
    ObjectMetadata objectMetadata = cosclient.getObjectMetadata(getObjectMetadataRequest);
    assertFalse(objectMetadata.isDeleteMarker());
    assertEquals(expectedLength, objectMetadata.getContentLength());
    assertEquals(expectedEtag, objectMetadata.getETag());
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key, versionId);
    ObjectMetadata objectMetadata2 = cosclient.getObject(getObjectRequest, downloadLocalFile);
    assertEquals(expectedLength, downloadLocalFile.length());
    assertEquals(expectedEtag, Md5Utils.md5Hex(downloadLocalFile));
    assertFalse(objectMetadata2.isDeleteMarker());
    assertEquals(expectedLength, objectMetadata2.getContentLength());
    assertEquals(expectedEtag, objectMetadata2.getETag());
    assertTrue(downloadLocalFile.delete());
}
Also used : GetObjectMetadataRequest(com.qcloud.cos.model.GetObjectMetadataRequest) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest)

Example 3 with GetObjectMetadataRequest

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

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

use of com.qcloud.cos.model.GetObjectMetadataRequest 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)

Aggregations

GetObjectMetadataRequest (com.qcloud.cos.model.GetObjectMetadataRequest)6 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)6 COSProgressListenerChain (com.qcloud.cos.event.COSProgressListenerChain)2 MultipleFileTransferProgressUpdatingListener (com.qcloud.cos.event.MultipleFileTransferProgressUpdatingListener)2 ProgressListenerChain (com.qcloud.cos.event.ProgressListenerChain)2 TransferProgressUpdatingListener (com.qcloud.cos.event.TransferProgressUpdatingListener)2 TransferCompletionFilter (com.qcloud.cos.event.TransferCompletionFilter)1 CosClientException (com.qcloud.cos.exception.CosClientException)1 FileLockException (com.qcloud.cos.exception.FileLockException)1 CopyImpl (com.qcloud.cos.internal.CopyImpl)1 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1