Search in sources :

Example 1 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class CopyFileDemo method copySmallFileDemo.

// copyObject最大支持5G文件的copy, 5G以上的文件copy请参照TransferManagerDemo中的copy示例
public static void copySmallFileDemo() {
    // 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 region, 支持跨园区拷贝
    Region srcBucketRegion = new Region("ap-shanghai");
    // 源bucket, bucket名需包含appid
    String srcBucketName = "srcBucket-1251668577";
    // 要拷贝的源文件
    String srcKey = "aaa/bbb.txt";
    // 目的bucket, bucket名需包含appid
    String destBucketName = "destBucket-1251668577";
    // 要拷贝的目的文件
    String destKey = "ccc/ddd.txt";
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketRegion, srcBucketName, srcKey, destBucketName, destKey);
    try {
        CopyObjectResult copyObjectResult = cosclient.copyObject(copyObjectRequest);
        String crc64 = copyObjectResult.getCrc64Ecma();
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosServiceException(com.qcloud.cos.exception.CosServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CopyObjectResult(com.qcloud.cos.model.CopyObjectResult) CosClientException(com.qcloud.cos.exception.CosClientException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 2 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class CopyFileDemo method copyWithNewMetaDataDemo.

public static void copyWithNewMetaDataDemo() {
    // 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);
    ExecutorService threadPool = Executors.newFixedThreadPool(32);
    // 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
    TransferManager transferManager = new TransferManager(cosclient, threadPool);
    // 要拷贝的bucket region, 支持跨园区拷贝
    Region srcBucketRegion = new Region("ap-shanghai");
    // 源bucket, bucket名需包含appid
    String srcBucketName = "srcBucket-1251668577";
    // 要拷贝的源文件
    String srcKey = "aaa/bbb.txt";
    // 目的bucket, bucket名需包含appid
    String destBucketName = "destBucket-1251668577";
    // 要拷贝的目的文件
    String destKey = "ccc/ddd.txt";
    ClientConfig srcClientConfig = new ClientConfig(srcBucketRegion);
    COSClient srcCosclient = new COSClient(cred, srcClientConfig);
    GetObjectRequest getReq = new GetObjectRequest(srcBucketName, srcKey);
    File srcFile = new File("srcFile");
    srcCosclient.getObject(getReq, srcFile);
    String srcMD5 = "";
    try {
        srcMD5 = Md5Utils.md5Hex(srcFile);
    } catch (Exception e) {
        e.printStackTrace();
    }
    ObjectMetadata destMeta = new ObjectMetadata();
    destMeta.addUserMetadata("md5", srcMD5);
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketRegion, srcBucketName, srcKey, destBucketName, destKey);
    copyObjectRequest.setNewObjectMetadata(destMeta);
    try {
        CopyObjectResult copyObjectResult = cosclient.copyObject(copyObjectRequest);
        System.out.print(copyObjectResult.getRequestId());
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    cosclient.shutdown();
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosClientException(com.qcloud.cos.exception.CosClientException) CosClientException(com.qcloud.cos.exception.CosClientException) CosServiceException(com.qcloud.cos.exception.CosServiceException) COSClient(com.qcloud.cos.COSClient) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) CosServiceException(com.qcloud.cos.exception.CosServiceException) CopyObjectResult(com.qcloud.cos.model.CopyObjectResult) ExecutorService(java.util.concurrent.ExecutorService) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 3 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class DelFileDemo method BatchDelFileWithVersion.

// 批量删除带有版本号的文件(即bucket开启了多版本)
public static void BatchDelFileWithVersion() {
    // 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";
    DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);
    // 设置要删除的key列表, 最多一次删除1000个
    ArrayList<KeyVersion> keyList = new ArrayList<>();
    // 传入要删除的文件名
    keyList.add(new KeyVersion("aaa.txt", "axbefagagaxxfafa"));
    keyList.add(new KeyVersion("bbb.mp4", "awcafa1faxg0lx"));
    keyList.add(new KeyVersion("ccc/ddd.jpg", "kafa1kxxaa2ymh"));
    deleteObjectsRequest.setKeys(keyList);
    // 批量删除文件
    try {
        DeleteObjectsResult deleteObjectsResult = cosclient.deleteObjects(deleteObjectsRequest);
        List<DeletedObject> deleteObjectResultArray = deleteObjectsResult.getDeletedObjects();
    } catch (MultiObjectDeleteException mde) {
        // 如果部分产出成功部分失败, 返回MultiObjectDeleteException
        List<DeletedObject> deleteObjects = mde.getDeletedObjects();
        List<DeleteError> deleteErrors = mde.getErrors();
    } catch (CosServiceException e) {
        // 如果是其他错误, 比如参数错误, 身份验证不过等会抛出CosServiceException
        e.printStackTrace();
    } catch (CosClientException e) {
        // 如果是客户端错误,比如连接不上COS
        e.printStackTrace();
    }
    // 关闭客户端
    cosclient.shutdown();
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) 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) COSClient(com.qcloud.cos.COSClient) CosServiceException(com.qcloud.cos.exception.CosServiceException) MultiObjectDeleteException(com.qcloud.cos.exception.MultiObjectDeleteException) Region(com.qcloud.cos.region.Region) ArrayList(java.util.ArrayList) List(java.util.List) ClientConfig(com.qcloud.cos.ClientConfig) DeletedObject(com.qcloud.cos.model.DeleteObjectsResult.DeletedObject)

Example 4 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class SSECustomerDemo method SSECustomerDownload.

public static void SSECustomerDownload() {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("COS_SECRETID", "COS_SECRETKEY");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "examplebucket-1250000000";
    String key = "aaa/bbb.txt";
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    SSECustomerKey sseCustomerKey = new SSECustomerKey("MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=");
    getObjectRequest.setSSECustomerKey(sseCustomerKey);
    try {
        COSObject cosObject = cosclient.getObject(getObjectRequest);
        COSObjectInputStream cosObjectInputStream = cosObject.getObjectContent();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(cosObjectInputStream));
        System.out.println(bufferedReader.readLine());
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // 关闭客户端
    cosclient.shutdown();
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) InputStreamReader(java.io.InputStreamReader) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosClientException(com.qcloud.cos.exception.CosClientException) IOException(java.io.IOException) COSClient(com.qcloud.cos.COSClient) CosServiceException(com.qcloud.cos.exception.CosServiceException) BufferedReader(java.io.BufferedReader) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 5 with CosClientException

use of com.qcloud.cos.exception.CosClientException in project cos-java-sdk-v5 by tencentyun.

the class SSECustomerDemo method SSECustomerUpload.

public static void SSECustomerUpload() {
    // 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("COS_SECRETID", "COS_SECRETKEY");
    // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 要求http协议
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "examplebucket-1250000000";
    String key = "aaa/bbb.txt";
    File localFile = new File("test.txt");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    SSECustomerKey sseCustomerKey = new SSECustomerKey("MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=");
    putObjectRequest.setSSECustomerKey(sseCustomerKey);
    // 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
    putObjectRequest.setStorageClass(StorageClass.Standard_IA);
    try {
        PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
        // putobjectResult会返回文件的etag, 该md5值根据s3语义不是对象的md5,只是唯一性标志
        String etag = putObjectResult.getETag();
    } 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) CosClientException(com.qcloud.cos.exception.CosClientException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File)

Aggregations

CosClientException (com.qcloud.cos.exception.CosClientException)111 CosServiceException (com.qcloud.cos.exception.CosServiceException)64 COSCredentials (com.qcloud.cos.auth.COSCredentials)41 ClientConfig (com.qcloud.cos.ClientConfig)39 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)39 Region (com.qcloud.cos.region.Region)39 COSClient (com.qcloud.cos.COSClient)37 IOException (java.io.IOException)31 File (java.io.File)28 ByteArrayInputStream (java.io.ByteArrayInputStream)18 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)15 TransferManager (com.qcloud.cos.transfer.TransferManager)14 ExecutorService (java.util.concurrent.ExecutorService)14 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)13 URISyntaxException (java.net.URISyntaxException)13 MultiObjectDeleteException (com.qcloud.cos.exception.MultiObjectDeleteException)12 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)12 SecretKey (javax.crypto.SecretKey)12 MalformedURLException (java.net.MalformedURLException)11 PutObjectResult (com.qcloud.cos.model.PutObjectResult)10