use of com.qcloud.cos.model.CopyObjectResult 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();
}
use of com.qcloud.cos.model.CopyObjectResult 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();
}
use of com.qcloud.cos.model.CopyObjectResult in project cos-java-sdk-v5 by tencentyun.
the class KmsUploadDemo method CopyObjectWithKmsMeta.
public static void CopyObjectWithKmsMeta() {
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"));
// 设置使用https请求
clientConfig.setHttpProtocol(HttpProtocol.https);
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket名需包含appid
String kmsKeyId = "your-kms-key-id";
String encryptionContext = Base64.encodeAsString("{\"Ssekmstest\":\"Ssekmstest\"}".getBytes());
// 要拷贝的bucket region, 支持跨园区拷贝
Region srcBucketRegion = new Region("ap-guangzhou");
// 源bucket, bucket名需包含appid
String srcBucketName = "mybucket-1251668577";
// 要拷贝的源文件
String srcKey = "aaa/bbb.txt";
// 目的bucket, bucket名需包含appid
String destBucketName = "mybucket-1251668577";
// 要拷贝的目的文件
String destKey = "ccc/ddd.txt";
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketRegion, srcBucketName, srcKey, destBucketName, destKey);
copyObjectRequest.setSSECOSKeyManagementParams(new SSECOSKeyManagementParams(kmsKeyId, encryptionContext));
try {
CopyObjectResult copyObjectResult = cosclient.copyObject(copyObjectRequest);
String crc64 = copyObjectResult.getCrc64Ecma();
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
}
// 关闭客户端
cosclient.shutdown();
}
use of com.qcloud.cos.model.CopyObjectResult in project cos-java-sdk-v5 by tencentyun.
the class CopyCallable method copyInOneChunk.
/**
* Performs the copy of the COS object from source bucket to destination bucket. The COS object
* is copied to destination in one single request.
*
* @returns CopyResult response information from the server.
*/
private CopyResult copyInOneChunk() {
CopyObjectResult copyObjectResult = cos.copyObject(copyObjectRequest);
if (copyObjectResult == null) {
return null;
}
CopyResult copyResult = new CopyResult();
copyResult.setSourceBucketName(copyObjectRequest.getSourceBucketName());
copyResult.setSourceKey(copyObjectRequest.getSourceKey());
copyResult.setDestinationBucketName(copyObjectRequest.getDestinationBucketName());
copyResult.setDestinationKey(copyObjectRequest.getDestinationKey());
copyResult.setETag(copyObjectResult.getETag());
copyResult.setVersionId(copyObjectResult.getVersionId());
copyResult.setRequestId(copyObjectResult.getRequestId());
copyResult.setDateStr(copyObjectResult.getDateStr());
copyResult.setCrc64Ecma(copyObjectResult.getCrc64Ecma());
return copyResult;
}
use of com.qcloud.cos.model.CopyObjectResult in project cos-java-sdk-v5 by tencentyun.
the class PutObjectCopyTest method testCopySameRegionDiffSize.
private void testCopySameRegionDiffSize(long fileSize, ObjectMetadata newObjectMetaData) throws IOException {
if (!judgeUserInfoValid()) {
return;
}
File localFile = buildTestFile(fileSize);
String srcEtag = Md5Utils.md5Hex(localFile);
String srcKey = String.format("ut/src_len_%d.txt", fileSize);
String destKey = String.format("ut/dest_len_%d.txt", fileSize);
try {
PutObjectResult putObjectResult = putObjectFromLocalFile(localFile, srcKey);
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, srcKey, bucket, destKey);
copyObjectRequest.setSourceVersionId(putObjectResult.getVersionId());
copyObjectRequest.setStorageClass(StorageClass.Standard_IA);
if (newObjectMetaData != null) {
copyObjectRequest.setNewObjectMetadata(newObjectMetaData);
}
CopyObjectResult copyObjectResult = cosclient.copyObject(copyObjectRequest);
assertNotNull(copyObjectResult.getRequestId());
assertNotNull(copyObjectResult.getDateStr());
// assertEquals(srcEtag, copyObjectResult.getETag());
headSimpleObject(srcKey, fileSize, srcEtag);
/*
ObjectMetadata destObjectMetadata = headSimpleObject(destKey, fileSize, srcEtag);
if (newObjectMetaData != null) {
checkMetaData(newObjectMetaData, destObjectMetadata);
}
*/
} finally {
// delete file on cos
clearObject(srcKey);
clearObject(destKey);
// delete local file
if (localFile.exists()) {
assertTrue(localFile.delete());
}
}
}
Aggregations