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