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;
}
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());
}
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;
}
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;
}
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;
}
Aggregations