use of com.qcloud.cos.model.GetObjectRequest in project cos-java-sdk-v5 by tencentyun.
the class TransferManagerTest method testTransferManagerUploadDownBigFile.
@Test
public void testTransferManagerUploadDownBigFile() throws IOException, CosServiceException, CosClientException, InterruptedException {
if (!judgeUserInfoValid()) {
return;
}
TransferManager transferManager = new TransferManager(cosclient);
File localFile = buildTestFile(1024 * 1024 * 10L);
File downFile = new File(localFile.getAbsolutePath() + ".down");
String key = "ut/" + localFile.getName();
String destKey = key + ".copy";
try {
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setServerSideEncryption("AES256");
putObjectRequest.setMetadata(objectMetadata);
Upload upload = transferManager.upload(putObjectRequest);
UploadResult uploadResult = upload.waitForUploadResult();
assertTrue(uploadResult.getETag().contains("-"));
assertTrue(upload.isResumeableMultipartUploadAfterFailed());
assertNotNull(upload.getResumeableMultipartUploadId());
assertNotNull(uploadResult.getRequestId());
assertNotNull(uploadResult.getDateStr());
GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
Download download = transferManager.download(getObjectRequest, downFile);
download.waitForCompletion();
// check file
assertEquals(Md5Utils.md5Hex(localFile), Md5Utils.md5Hex(downFile));
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, key, bucket, destKey);
Copy copy = transferManager.copy(copyObjectRequest, cosclient, null);
copy.waitForCompletion();
} finally {
// delete file on cos
clearObject(key);
// delete file on cos
clearObject(destKey);
if (localFile.exists()) {
assertTrue(localFile.delete());
}
if (downFile.exists()) {
assertTrue(downFile.delete());
}
}
}
use of com.qcloud.cos.model.GetObjectRequest in project cos-java-sdk-v5 by tencentyun.
the class TransferManagerTest method testTransferManagerUploadDownCopySmallFile.
@Test
public void testTransferManagerUploadDownCopySmallFile() throws IOException, CosServiceException, CosClientException, InterruptedException {
if (!judgeUserInfoValid()) {
return;
}
TransferManager transferManager = new TransferManager(cosclient);
File localFile = buildTestFile(1024 * 1024 * 2L);
File downFile = new File(localFile.getAbsolutePath() + ".down");
String key = "ut/" + localFile.getName();
String destKey = key + ".copy";
try {
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
Upload upload = transferManager.upload(putObjectRequest);
UploadResult uploadResult = upload.waitForUploadResult();
// head object
headSimpleObject(key, localFile.length(), Md5Utils.md5Hex(localFile));
assertEquals(Md5Utils.md5Hex(localFile), uploadResult.getETag());
assertFalse(upload.isResumeableMultipartUploadAfterFailed());
assertNull(upload.getResumeableMultipartUploadId());
assertNotNull(uploadResult.getRequestId());
assertNotNull(uploadResult.getDateStr());
GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
Download download = transferManager.download(getObjectRequest, downFile);
download.waitForCompletion();
// check file
assertEquals(Md5Utils.md5Hex(localFile), Md5Utils.md5Hex(downFile));
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, key, bucket, destKey);
Copy copy = transferManager.copy(copyObjectRequest, cosclient, null);
copy.waitForCompletion();
} finally {
// clear object
clearObject(key);
// clear dest object
clearObject(destKey);
// delete smallfile
if (localFile.exists()) {
assertTrue(localFile.delete());
}
if (downFile.exists()) {
assertTrue(downFile.delete());
}
}
}
use of com.qcloud.cos.model.GetObjectRequest in project cos-java-sdk-v5 by tencentyun.
the class AbstractCOSClientTest method getObject.
// 下载COS的object
protected static void getObject(String key, File localDownFile, long[] range, long expectedLength, String expectedMd5) {
System.setProperty(SkipMd5CheckStrategy.DISABLE_GET_OBJECT_MD5_VALIDATION_PROPERTY, "true");
GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
ResponseHeaderOverrides responseHeaders = new ResponseHeaderOverrides();
String responseContentType = "image/x-icon";
String responseContentLanguage = "zh-CN";
String responseContentDispositon = "filename=\"abc.txt\"";
String responseCacheControl = "no-cache";
String expireStr = DateUtils.formatRFC822Date(new Date(System.currentTimeMillis() + 24 * 3600 * 1000));
responseHeaders.setContentType(responseContentType);
responseHeaders.setContentLanguage(responseContentLanguage);
responseHeaders.setContentDisposition(responseContentDispositon);
responseHeaders.setCacheControl(responseCacheControl);
responseHeaders.setExpires(expireStr);
getObjectRequest.setResponseHeaders(responseHeaders);
if (range != null) {
if (range[1] == range[0] && range[1] == 0) {
assertEquals(expectedLength, 1);
getObjectRequest.setRange(range[0], range[1]);
} else {
assertEquals(expectedLength, range[1] - range[0] + 1);
getObjectRequest.setRange(range[0], range[1]);
}
}
try {
ObjectMetadata objectMetadata = cosclient.getObject(getObjectRequest, localDownFile);
assertEquals(responseContentType, objectMetadata.getContentType());
assertEquals(responseContentLanguage, objectMetadata.getContentLanguage());
assertEquals(responseContentDispositon, objectMetadata.getContentDisposition());
assertEquals(responseCacheControl, objectMetadata.getCacheControl());
assertEquals(expectedLength, localDownFile.length());
assertEquals(expectedMd5, Md5Utils.md5Hex(localDownFile));
} catch (SecurityException se) {
if (cosclient instanceof COSEncryptionClient && cryptoConfiguration != null && cryptoConfiguration.getCryptoMode() == CryptoMode.StrictAuthenticatedEncryption && range != null) {
return;
}
fail(se.toString());
} catch (Exception e) {
fail(e.toString());
}
}
use of com.qcloud.cos.model.GetObjectRequest 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.GetObjectRequest in project cos-java-sdk-v5 by tencentyun.
the class TransferManager method resumeDownload.
/**
* Resumes an download operation. This download operation uses the same configuration as the
* original download. Any data already fetched will be skipped, and only the remaining data is
* retrieved from Qcloud COS.
*
* @param persistableDownload the download to resume.
* @return A new <code>Download</code> object to use to check the state of the download, listen
* for progress notifications, and otherwise manage the download.
*
* @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 Download resumeDownload(PersistableDownload persistableDownload) {
assertParameterNotNull(persistableDownload, "PausedDownload is mandatory to resume a download.");
GetObjectRequest request = new GetObjectRequest(persistableDownload.getBucketName(), persistableDownload.getKey(), persistableDownload.getVersionId());
if (persistableDownload.getRange() != null && persistableDownload.getRange().length == 2) {
long[] range = persistableDownload.getRange();
request.setRange(range[0], range[1]);
}
request.setResponseHeaders(persistableDownload.getResponseHeaders());
return doDownload(request, new File(persistableDownload.getFile()), null, null, APPEND_MODE);
}
Aggregations