use of com.qcloud.cos.exception.CosServiceException in project cos-java-sdk-v5 by tencentyun.
the class MultipartUploadTest method testAbortMultipartUploads.
@Test
public void testAbortMultipartUploads() {
if (!judgeUserInfoValid()) {
return;
}
String key = "ut/testAbortMultipart.txt";
String uploadId = testInitMultipart(key);
AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest(bucket, key, uploadId);
cosclient.abortMultipartUpload(abortMultipartUploadRequest);
try {
testListMultipart(key, uploadId, 1, new ArrayList<String>());
fail("Aborted uploadid is still valid. uploadId:" + uploadId);
} catch (CosServiceException e) {
}
}
use of com.qcloud.cos.exception.CosServiceException 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.exception.CosServiceException 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.exception.CosServiceException in project cos-java-sdk-v5 by tencentyun.
the class BatchDeleteTest method batchDeletePartExistFileForVersionSuspended.
@Test
public void batchDeletePartExistFileForVersionSuspended() throws IOException, InterruptedException {
if (!judgeUserInfoValid()) {
return;
}
BucketVersioningConfiguration bucketVersioningEnabled = new BucketVersioningConfiguration(BucketVersioningConfiguration.SUSPENDED);
cosclient.setBucketVersioningConfiguration(new SetBucketVersioningConfigurationRequest(bucket, bucketVersioningEnabled));
Thread.sleep(5000L);
DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucket);
ArrayList<KeyVersion> keyList = new ArrayList<>();
long deleteFileCount = 5;
for (long fileIndex = 0; fileIndex < deleteFileCount; ++fileIndex) {
File localFile = buildTestFile(fileIndex * 1024);
String key = "ut/" + localFile.getName();
PutObjectResult putObjectResult = putObjectFromLocalFile(localFile, key);
keyList.add(new KeyVersion(key, "null"));
}
keyList.add(new KeyVersion("ut/not_exist_key.txt", "null"));
deleteObjectsRequest.setKeys(keyList);
try {
DeleteObjectsResult deleteObjectsResult = cosclient.deleteObjects(deleteObjectsRequest);
List<DeletedObject> deleteObjectResultArray = deleteObjectsResult.getDeletedObjects();
} catch (MultiObjectDeleteException mde) {
List<DeletedObject> deleteObjects = mde.getDeletedObjects();
List<DeleteError> deleteErrors = mde.getErrors();
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
}
}
use of com.qcloud.cos.exception.CosServiceException in project cos-java-sdk-v5 by tencentyun.
the class SimpleUploadFileDemo method SimpleUploadFileFromLocal.
// 将本地文件上传到COS
public static void SimpleUploadFileFromLocal(boolean useTrafficLimit) {
// 1 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket名需包含appid
String bucketName = "mybucket-1251668577";
String key = "aaa/bbb.txt";
File localFile = new File("src/test/resources/len10M.txt");
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
if (useTrafficLimit) {
// 限流使用的单位是bit/s, 这里测试1MB/s的上传带宽限制
putObjectRequest.setTrafficLimit(8 * 1024 * 1024);
}
// 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
putObjectRequest.setStorageClass(StorageClass.Standard_IA);
try {
PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
// putobjectResult会返回文件的etag
String etag = putObjectResult.getETag();
String crc64 = putObjectResult.getCrc64Ecma();
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
}
// 关闭客户端
cosclient.shutdown();
}
Aggregations