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;
}
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);
}
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());
}
}
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());
}
}
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());
}
}
Aggregations