Search in sources :

Example 11 with CosServiceException

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

the class PicOperationDemo method PicPersistentProcessing.

public static void PicPersistentProcessing() {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("SECRET_ID", "SECRET_KEY");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // 4. bucket名需包含appid
    String bucketName = "examplebucket-1250000000";
    String key = "example.jpg";
    File localFile = new File("/data/example.jpg");
    // 5.对图像进行持久化处理
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setHeader("Pic-Operations", "{\"is_pic_info\":1,\"rules\":[{\"fileid\":\"example.png\",\"rule\":\"imageView2/format/png\"}]}");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    putObjectRequest.setMetadata(objectMetadata);
    try {
        PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
        // putobjectResult会返回文件的etag
        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) 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) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 12 with CosServiceException

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

the class MultipartUploadDemo method InitMultipartUploadDemo.

public static String InitMultipartUploadDemo() {
    // 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-guangzhou"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    String key = "aaa/bbb.txt";
    InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, key);
    // 设置存储类型, 默认是标准(Standard), 低频(Standard_IA), 归档(Archive)
    request.setStorageClass(StorageClass.Standard);
    try {
        InitiateMultipartUploadResult initResult = cosclient.initiateMultipartUpload(request);
        // 获取uploadid
        String uploadId = initResult.getUploadId();
        return uploadId;
    } catch (CosServiceException e) {
        throw e;
    } catch (CosClientException e) {
        throw e;
    } finally {
        cosclient.shutdown();
    }
}
Also used : COSClient(com.qcloud.cos.COSClient) InitiateMultipartUploadResult(com.qcloud.cos.model.InitiateMultipartUploadResult) 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) InitiateMultipartUploadRequest(com.qcloud.cos.model.InitiateMultipartUploadRequest) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 13 with CosServiceException

use of com.qcloud.cos.exception.CosServiceException 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());
    }
}
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 14 with CosServiceException

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

the class COSClient method setBucketCrossOriginConfiguration.

@Override
public void setBucketCrossOriginConfiguration(SetBucketCrossOriginConfigurationRequest setBucketCrossOriginConfigurationRequest) throws CosClientException, CosServiceException {
    rejectNull(setBucketCrossOriginConfigurationRequest, "The set bucket cross origin configuration request object must be specified.");
    rejectNull(clientConfig.getRegion(), "region is null, region in clientConfig must be specified when setting bucket cross origin configuration");
    String bucketName = setBucketCrossOriginConfigurationRequest.getBucketName();
    BucketCrossOriginConfiguration bucketCrossOriginConfiguration = setBucketCrossOriginConfigurationRequest.getCrossOriginConfiguration();
    rejectNull(bucketName, "The bucket name parameter must be specified when setting bucket cross origin configuration.");
    rejectNull(bucketCrossOriginConfiguration, "The cross origin configuration parameter must be specified when setting bucket cross origin configuration.");
    CosHttpRequest<SetBucketCrossOriginConfigurationRequest> request = createRequest(bucketName, null, setBucketCrossOriginConfigurationRequest, HttpMethodName.PUT);
    request.addParameter("cors", null);
    byte[] content = new BucketConfigurationXmlFactory().convertToXmlByteArray(bucketCrossOriginConfiguration);
    request.addHeader("Content-Length", String.valueOf(content.length));
    request.addHeader("Content-Type", "application/xml");
    request.setContent(new ByteArrayInputStream(content));
    try {
        byte[] md5 = Md5Utils.computeMD5Hash(content);
        String md5Base64 = BinaryUtils.toBase64(md5);
        request.addHeader("Content-MD5", md5Base64);
    } catch (Exception e) {
        throw new CosClientException("Couldn't compute md5 sum", e);
    }
    invoke(request, voidCosResponseHandler);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CosClientException(com.qcloud.cos.exception.CosClientException) DecoderException(org.apache.commons.codec.DecoderException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MultiObjectDeleteException(com.qcloud.cos.exception.MultiObjectDeleteException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) MalformedURLException(java.net.MalformedURLException)

Example 15 with CosServiceException

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

the class COSClient method setBucketReplicationConfiguration.

@Override
public void setBucketReplicationConfiguration(SetBucketReplicationConfigurationRequest setBucketReplicationConfigurationRequest) throws CosClientException, CosServiceException {
    rejectNull(setBucketReplicationConfigurationRequest, "The set bucket replication configuration request object must be specified.");
    rejectNull(clientConfig.getRegion(), "region is null, region in clientConfig must be specified when setting bucket replication configuration");
    final String bucketName = setBucketReplicationConfigurationRequest.getBucketName();
    final BucketReplicationConfiguration bucketReplicationConfiguration = setBucketReplicationConfigurationRequest.getReplicationConfiguration();
    rejectNull(bucketName, "The bucket name parameter must be specified when setting replication configuration.");
    rejectNull(bucketReplicationConfiguration, "The replication configuration parameter must be specified when setting replication configuration.");
    CosHttpRequest<SetBucketReplicationConfigurationRequest> request = createRequest(bucketName, null, setBucketReplicationConfigurationRequest, HttpMethodName.PUT);
    request.addParameter("replication", null);
    final byte[] bytes = new BucketConfigurationXmlFactory().convertToXmlByteArray(bucketReplicationConfiguration);
    request.addHeader("Content-Length", String.valueOf(bytes.length));
    request.addHeader("Content-Type", "application/xml");
    request.setContent(new ByteArrayInputStream(bytes));
    try {
        request.addHeader("Content-MD5", BinaryUtils.toBase64(Md5Utils.computeMD5Hash(bytes)));
    } catch (Exception e) {
        throw new CosClientException("Not able to compute MD5 of the replication rule configuration. Exception Message : " + e.getMessage(), e);
    }
    invoke(request, voidCosResponseHandler);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CosClientException(com.qcloud.cos.exception.CosClientException) DecoderException(org.apache.commons.codec.DecoderException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MultiObjectDeleteException(com.qcloud.cos.exception.MultiObjectDeleteException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) MalformedURLException(java.net.MalformedURLException)

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