use of com.qcloud.cos.model.COSObject in project cos-java-sdk-v5 by tencentyun.
the class PutObjectDemo method putObjectDemo.
static void putObjectDemo() {
String bucketName = "examplebucket-1251668577";
String key = "abc/abc.txt";
String localPath = "abc.txt";
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setHeader("expires", new Date(1660000000000L));
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, new File(localPath));
putObjectRequest.withMetadata(objectMetadata);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
System.out.println(putObjectResult.getRequestId());
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
COSObject cosObject = cosClient.getObject(getObjectRequest);
System.out.println(cosObject.getObjectMetadata().getRequestId());
cosClient.shutdown();
}
use of com.qcloud.cos.model.COSObject in project cos-java-sdk-v5 by tencentyun.
the class PutGetDelTest method testGetObjectIfMatchWrongEtag.
@Test
public void testGetObjectIfMatchWrongEtag() 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);
// 打乱一下,得到一个错误的etag
String wrongEtag = fileEtag.substring(5) + fileEtag.substring(0, 5);
GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
List<String> eTagList = new ArrayList<>();
eTagList.add(wrongEtag);
getObjectRequest.setMatchingETagConstraints(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 testGetObjectIfNoneMatchWrongEtag.
@Test
public void testGetObjectIfNoneMatchWrongEtag() 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 + "\"");
getObjectRequest.setNonmatchingETagConstraints(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 PutGetDelTest method testGetObjectIfMatchRightEtag.
@Test
public void testGetObjectIfMatchRightEtag() 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.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 call.
/**
* This method must return a non-null object, or else the existing implementation in
* {@link AbstractTransfer#waitForCompletion()} would block forever.
*
* @return the downloaded file
*/
@Override
public File call() throws Exception {
try {
latch.await();
download.setState(TransferState.InProgress);
COSObject cosObject = retryableDownloadCOSObjectToFile(dstfile, new DownloadTaskImpl(cos, download, req), resumeExistingDownload);
if (cosObject == null) {
download.setState(TransferState.Canceled);
download.setMonitor(new DownloadMonitor(download, null));
} else {
download.setState(TransferState.Completed);
}
return dstfile;
} catch (Throwable t) {
// Downloads aren't allowed to move from canceled to failed
if (download.getState() != TransferState.Canceled) {
download.setState(TransferState.Failed);
}
if (t instanceof Exception)
throw (Exception) t;
else
throw (Error) t;
}
}
Aggregations