Search in sources :

Example 26 with CosServiceException

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) {
    }
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) AbortMultipartUploadRequest(com.qcloud.cos.model.AbortMultipartUploadRequest) Test(org.junit.Test)

Example 27 with CosServiceException

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());
    }
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) COSObject(com.qcloud.cos.model.COSObject) ArrayList(java.util.ArrayList) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) Test(org.junit.Test)

Example 28 with CosServiceException

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());
    }
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) COSObject(com.qcloud.cos.model.COSObject) ArrayList(java.util.ArrayList) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) Test(org.junit.Test)

Example 29 with CosServiceException

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();
    }
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) KeyVersion(com.qcloud.cos.model.DeleteObjectsRequest.KeyVersion) CosClientException(com.qcloud.cos.exception.CosClientException) ArrayList(java.util.ArrayList) DeleteObjectsResult(com.qcloud.cos.model.DeleteObjectsResult) DeleteObjectsRequest(com.qcloud.cos.model.DeleteObjectsRequest) CosServiceException(com.qcloud.cos.exception.CosServiceException) MultiObjectDeleteException(com.qcloud.cos.exception.MultiObjectDeleteException) SetBucketVersioningConfigurationRequest(com.qcloud.cos.model.SetBucketVersioningConfigurationRequest) BucketVersioningConfiguration(com.qcloud.cos.model.BucketVersioningConfiguration) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) DeletedObject(com.qcloud.cos.model.DeleteObjectsResult.DeletedObject) Test(org.junit.Test)

Example 30 with CosServiceException

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();
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosServiceException(com.qcloud.cos.exception.CosServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) PutObjectResult(com.qcloud.cos.model.PutObjectResult) CosClientException(com.qcloud.cos.exception.CosClientException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Aggregations

CosServiceException (com.qcloud.cos.exception.CosServiceException)82 CosClientException (com.qcloud.cos.exception.CosClientException)64 COSClient (com.qcloud.cos.COSClient)37 ClientConfig (com.qcloud.cos.ClientConfig)37 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)37 COSCredentials (com.qcloud.cos.auth.COSCredentials)37 Region (com.qcloud.cos.region.Region)37 File (java.io.File)28 IOException (java.io.IOException)20 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)15 TransferManager (com.qcloud.cos.transfer.TransferManager)14 ExecutorService (java.util.concurrent.ExecutorService)14 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)13 ByteArrayInputStream (java.io.ByteArrayInputStream)13 PutObjectResult (com.qcloud.cos.model.PutObjectResult)12 MultiObjectDeleteException (com.qcloud.cos.exception.MultiObjectDeleteException)11 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)11 ArrayList (java.util.ArrayList)10 LinkedList (java.util.LinkedList)10 Test (org.junit.Test)10