use of com.qcloud.cos.model.COSObject in project hadoop-cos by tencentyun.
the class CosNativeFileSystemStore method retrieve.
/**
* retrieve cos key
*
* @param key The key is the object name that is being retrieved from the
* cos bucket.
* @return This method returns null if the key is not found.
* @throws IOException Retrieve the specified cos key failed.
*/
@Override
public InputStream retrieve(String key) throws IOException {
LOG.debug("Retrieve the key: {}.", key);
GetObjectRequest getObjectRequest = new GetObjectRequest(this.bucketName, key);
if (this.trafficLimit >= 0) {
getObjectRequest.setTrafficLimit(this.trafficLimit);
}
this.setEncryptionMetadata(getObjectRequest, new ObjectMetadata());
try {
COSObject cosObject = (COSObject) callCOSClientWithRetry(getObjectRequest);
return cosObject.getObjectContent();
} catch (Exception e) {
String errMsg = String.format("Retrieving the cos key [%s] occurs an exception: %s", key, e);
handleException(new Exception(errMsg), key);
}
// never will get here
return null;
}
use of com.qcloud.cos.model.COSObject in project jeesuite-libs by vakinge.
the class QcloudProvider method getObjectInputStream.
@Override
public InputStream getObjectInputStream(String bucketName, String fileKey) {
try {
String _bucketName = buildBucketName(bucketName);
String _fileKey = resolveFileKey(bucketName, fileKey);
COSObject cosObject = cosclient.getObject(_bucketName, _fileKey);
return cosObject.getObjectContent();
} catch (Exception e) {
throw new JeesuiteBaseException(500, buildMessage(bucketName, e));
}
}
use of com.qcloud.cos.model.COSObject in project cos-java-sdk-v5 by tencentyun.
the class PutGetDelTest method testGetObjectIfNoneMatchRightEtag.
@Test
public void testGetObjectIfNoneMatchRightEtag() throws IOException {
if (!judgeUserInfoValid()) {
return;
}
File localFile = buildTestFile(1024);
String key = "ut/" + localFile.getName();
cosclient.putObject(bucket, key, localFile);
try {
String fileEtag = Md5Utils.md5Hex(localFile);
GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
List<String> eTagList = new ArrayList<>();
eTagList.add("\"" + fileEtag + "\"");
getObjectRequest.setNonmatchingETagConstraints(eTagList);
COSObject cosObject = cosclient.getObject(getObjectRequest);
assertNull(cosObject);
} catch (CosServiceException cse) {
fail(cse.toString());
} finally {
cosclient.deleteObject(bucket, key);
assertTrue(localFile.delete());
}
}
use of com.qcloud.cos.model.COSObject in project cos-java-sdk-v5 by tencentyun.
the class PutGetDelTest method testGetObjectIfMatchContainRightEtag.
@Ignore
public void testGetObjectIfMatchContainRightEtag() throws IOException {
if (!judgeUserInfoValid()) {
return;
}
File localFile = buildTestFile(1024);
String key = "ut/" + localFile.getName();
cosclient.putObject(bucket, key, localFile);
try {
String fileEtag = Md5Utils.md5Hex(localFile);
String wrongEtag = fileEtag.substring(5) + fileEtag.substring(0, 5);
GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
List<String> eTagList = new ArrayList<>();
eTagList.add("\"" + wrongEtag + "\"");
eTagList.add("\"" + fileEtag + "\"");
getObjectRequest.setMatchingETagConstraints(eTagList);
COSObject cosObject = cosclient.getObject(getObjectRequest);
assertNotNull(cosObject);
} catch (CosServiceException cse) {
fail(cse.toString());
} finally {
cosclient.deleteObject(bucket, key);
assertTrue(localFile.delete());
}
}
use of com.qcloud.cos.model.COSObject in project cos-java-sdk-v5 by tencentyun.
the class DownloadCallable method retryableDownloadCOSObjectToFile.
private COSObject retryableDownloadCOSObjectToFile(File file, RetryableCOSDownloadTask retryableCOSDownloadTask, boolean appendData) {
boolean hasRetried = false;
COSObject cosObject;
for (; ; ) {
if (resumeExistingDownload && hasRetried) {
// Need to adjust the get range or else we risk corrupting the downloaded file
adjustRequest(req);
}
cosObject = retryableCOSDownloadTask.getCOSObjectStream();
if (cosObject == null)
return null;
try {
if (testing && resumeExistingDownload && !hasRetried) {
throw new CosClientException("testing");
}
ServiceUtils.downloadToFile(cosObject, file, retryableCOSDownloadTask.needIntegrityCheck(), appendData, expectedFileLength);
return cosObject;
} catch (CosClientException ace) {
if (!ace.isRetryable())
throw ace;
// The current code will retry the download only when case 2) or 3) happens.
if (ace.getCause() instanceof SocketException || ace.getCause() instanceof SSLProtocolException) {
throw ace;
} else {
if (hasRetried)
throw ace;
else {
log.info("Retry the download of object " + cosObject.getKey() + " (bucket " + cosObject.getBucketName() + ")", ace);
hasRetried = true;
}
}
} finally {
cosObject.getObjectContent().abort();
}
}
}
Aggregations