Search in sources :

Example 21 with CosServiceException

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

the class COSClient method copyPart.

@Override
public CopyPartResult copyPart(CopyPartRequest copyPartRequest) throws CosClientException, CosServiceException {
    rejectNull(copyPartRequest, "The CopyPartRequest parameter must be specified when copying a part");
    rejectNull(copyPartRequest.getSourceBucketName(), "The source bucket name must be specified when copying a part");
    rejectNull(copyPartRequest.getSourceKey(), "The source object key must be specified when copying a part");
    rejectNull(copyPartRequest.getDestinationBucketName(), "The destination bucket name must be specified when copying a part");
    rejectNull(copyPartRequest.getUploadId(), "The upload id must be specified when copying a part");
    rejectNull(copyPartRequest.getDestinationKey(), "The destination object key must be specified when copying a part");
    rejectNull(copyPartRequest.getPartNumber(), "The part number must be specified when copying a part");
    rejectNull(clientConfig.getRegion(), "region is null, region in clientConfig must be specified when copying a part");
    String destinationKey = copyPartRequest.getDestinationKey();
    String destinationBucketName = copyPartRequest.getDestinationBucketName();
    CosHttpRequest<CopyPartRequest> request = createRequest(destinationBucketName, destinationKey, copyPartRequest, HttpMethodName.PUT);
    populateRequestWithCopyPartParameters(request, copyPartRequest);
    request.addParameter("uploadId", copyPartRequest.getUploadId());
    request.addParameter("partNumber", Integer.toString(copyPartRequest.getPartNumber()));
    /*
         * We can't send a non-zero length Content-Length header if the user
         * specified it, otherwise it messes up the HTTP connection when the
         * remote server thinks there's more data to pull.
         */
    setZeroContentLength(request);
    CopyObjectResultHandler copyObjectResultHandler = null;
    try {
        @SuppressWarnings("unchecked") ResponseHeaderHandlerChain<CopyObjectResultHandler> handler = new ResponseHeaderHandlerChain<CopyObjectResultHandler>(// xml payload unmarshaller
        new Unmarshallers.CopyObjectUnmarshaller(), // header handlers
        new ServerSideEncryptionHeaderHandler<CopyObjectResultHandler>(), new COSVersionHeaderHandler());
        copyObjectResultHandler = invoke(request, handler);
    } catch (CosServiceException cse) {
        /*
             * If the request failed because one of the specified constraints
             * was not met (ex: matching ETag, modified since date, etc.), then
             * return null, so that users don't have to wrap their code in
             * try/catch blocks and check for this status code if they want to
             * use constraints.
             */
        if (cse.getStatusCode() == Constants.FAILED_PRECONDITION_STATUS_CODE) {
            return null;
        }
        throw cse;
    }
    /*
         * CopyPart has two failure modes: 1 - An HTTP error code is returned
         * and the error is processed like any other error response. 2 - An HTTP
         * 200 OK code is returned, but the response content contains an XML
         * error response.
         *
         * This makes it very difficult for the client runtime to cleanly detect
         * this case and handle it like any other error response. We could
         * extend the runtime to have a more flexible/customizable definition of
         * success/error (per request), but it's probably overkill for this one
         * special case.
         */
    if (copyObjectResultHandler.getErrorCode() != null) {
        String errorCode = copyObjectResultHandler.getErrorCode();
        String errorMessage = copyObjectResultHandler.getErrorMessage();
        String requestId = copyObjectResultHandler.getErrorRequestId();
        CosServiceException cse = new CosServiceException(errorMessage);
        cse.setErrorCode(errorCode);
        cse.setErrorType(ErrorType.Service);
        cse.setRequestId(requestId);
        cse.setStatusCode(200);
        throw cse;
    }
    CopyPartResult copyPartResult = new CopyPartResult();
    copyPartResult.setETag(copyObjectResultHandler.getETag());
    copyPartResult.setPartNumber(copyPartRequest.getPartNumber());
    copyPartResult.setLastModifiedDate(copyObjectResultHandler.getLastModified());
    copyPartResult.setVersionId(copyObjectResultHandler.getVersionId());
    copyPartResult.setSSEAlgorithm(copyObjectResultHandler.getSSEAlgorithm());
    copyPartResult.setSSECustomerAlgorithm(copyObjectResultHandler.getSSECustomerAlgorithm());
    copyPartResult.setSSECustomerKeyMd5(copyObjectResultHandler.getSSECustomerKeyMd5());
    copyPartResult.setCrc64Ecma(copyObjectResultHandler.getCrc64Ecma());
    return copyPartResult;
}
Also used : Unmarshallers(com.qcloud.cos.internal.Unmarshallers) COSVersionHeaderHandler(com.qcloud.cos.internal.COSVersionHeaderHandler) CosServiceException(com.qcloud.cos.exception.CosServiceException) CopyObjectResultHandler(com.qcloud.cos.internal.XmlResponsesSaxParser.CopyObjectResultHandler) ResponseHeaderHandlerChain(com.qcloud.cos.internal.ResponseHeaderHandlerChain)

Example 22 with CosServiceException

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

the class COSClient method setBucketRefererConfiguration.

@Override
public void setBucketRefererConfiguration(SetBucketRefererConfigurationRequest setBucketRefererConfigurationRequest) throws CosClientException, CosServiceException {
    rejectNull(setBucketRefererConfigurationRequest, "The request object parameter setBucketRefererConfigurationRequest must be specified.");
    String bucketName = setBucketRefererConfigurationRequest.getBucketName();
    BucketRefererConfiguration configuration = setBucketRefererConfigurationRequest.getConfiguration();
    rejectNull(bucketName, "The bucket name parameter must be specified when setting a bucket's referer configuration");
    rejectNull(configuration, "The bucket domain configuration parameter must be specified when setting a bucket's referer configuration");
    CosHttpRequest<SetBucketRefererConfigurationRequest> request = createRequest(bucketName, null, setBucketRefererConfigurationRequest, HttpMethodName.PUT);
    request.addParameter("referer", null);
    request.addHeader("Content-Type", "application/xml");
    byte[] bytes = new BucketConfigurationXmlFactory().convertToXmlByteArray(configuration);
    request.setContent(new ByteArrayInputStream(bytes));
    try {
        byte[] md5 = Md5Utils.computeMD5Hash(bytes);
        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 23 with CosServiceException

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

the class AbstractCOSClientTest method createBucket.

private static void createBucket() throws Exception {
    try {
        // 避免有查询缓存,导致创建bucket失败
        Thread.sleep(5000L);
        String bucketName = bucket;
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
        Bucket createdBucket = cosclient.createBucket(createBucketRequest);
        assertEquals(bucketName, createdBucket.getName());
        Thread.sleep(5000L);
        assertTrue(cosclient.doesBucketExist(bucketName));
    } catch (CosServiceException cse) {
        fail(cse.toString());
    }
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) Bucket(com.qcloud.cos.model.Bucket) CreateBucketRequest(com.qcloud.cos.model.CreateBucketRequest)

Example 24 with CosServiceException

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

the class CreateDeleteHeadBucketTest method testCreateDeleteBucketPublicRead.

@Test
public void testCreateDeleteBucketPublicRead() throws Exception {
    if (!judgeUserInfoValid()) {
        return;
    }
    try {
        String bucketName = String.format("java-pubr-%s", appid);
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
        createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);
        Bucket bucket = cosclient.createBucket(createBucketRequest);
        assertEquals(bucketName, bucket.getName());
        cosclient.headBucket(new HeadBucketRequest(bucketName));
        BucketVersioningConfiguration bucketVersioningConfiguration = cosclient.getBucketVersioningConfiguration(bucketName);
        assertEquals(BucketVersioningConfiguration.OFF, bucketVersioningConfiguration.getStatus());
        cosclient.deleteBucket(bucketName);
        // 删除bucket后, 由于server端有缓存 需要稍后查询, 这里sleep 5 秒
        Thread.sleep(5000L);
        assertFalse(cosclient.doesBucketExist(bucketName));
    } catch (CosServiceException cse) {
        fail(cse.toString());
    }
}
Also used : HeadBucketRequest(com.qcloud.cos.model.HeadBucketRequest) CosServiceException(com.qcloud.cos.exception.CosServiceException) Bucket(com.qcloud.cos.model.Bucket) CreateBucketRequest(com.qcloud.cos.model.CreateBucketRequest) BucketVersioningConfiguration(com.qcloud.cos.model.BucketVersioningConfiguration) Test(org.junit.Test)

Example 25 with CosServiceException

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

the class CreateDeleteHeadBucketTest method testCreateDeleteBucketPublicReadWrite.

@Test
public void testCreateDeleteBucketPublicReadWrite() throws Exception {
    if (!judgeUserInfoValid()) {
        return;
    }
    try {
        String bucketName = String.format("java-pubrw-%s", appid);
        CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
        createBucketRequest.setCannedAcl(CannedAccessControlList.PublicReadWrite);
        AccessControlList accessControlList = new AccessControlList();
        Grantee grantee = new UinGrantee("730123456");
        accessControlList.grantPermission(grantee, Permission.Write);
        createBucketRequest.setAccessControlList(accessControlList);
        Bucket bucket = cosclient.createBucket(createBucketRequest);
        assertEquals(bucketName, bucket.getName());
        assertTrue(cosclient.doesBucketExist(bucketName));
        BucketVersioningConfiguration bucketVersioningConfiguration = cosclient.getBucketVersioningConfiguration(bucketName);
        assertEquals(BucketVersioningConfiguration.OFF, bucketVersioningConfiguration.getStatus());
        cosclient.deleteBucket(bucketName);
        // 删除bucket后, 由于server端有缓存 需要稍后查询, 这里sleep 5 秒
        Thread.sleep(5000L);
        assertFalse(cosclient.doesBucketExist(bucketName));
    } catch (CosServiceException cse) {
        fail(cse.toString());
    }
}
Also used : CannedAccessControlList(com.qcloud.cos.model.CannedAccessControlList) AccessControlList(com.qcloud.cos.model.AccessControlList) UinGrantee(com.qcloud.cos.model.UinGrantee) UinGrantee(com.qcloud.cos.model.UinGrantee) Grantee(com.qcloud.cos.model.Grantee) CosServiceException(com.qcloud.cos.exception.CosServiceException) Bucket(com.qcloud.cos.model.Bucket) CreateBucketRequest(com.qcloud.cos.model.CreateBucketRequest) BucketVersioningConfiguration(com.qcloud.cos.model.BucketVersioningConfiguration) Test(org.junit.Test)

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