use of com.qcloud.cos.model.GetObjectRequest 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.GetObjectRequest in project cos-java-sdk-v5 by tencentyun.
the class GetObjectDemo method GetObjectToFileDemo.
public static void GetObjectToFileDemo() {
// 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
// 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
// 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
String key = "test/my_test.json";
String bucketName = "mybucket-1251668577";
boolean useTrafficLimit = false;
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
if (useTrafficLimit) {
getObjectRequest.setTrafficLimit(8 * 1024 * 1024);
}
File localFile = new File("my_test.json");
ObjectMetadata objectMetadata = cosclient.getObject(getObjectRequest, localFile);
System.out.println(objectMetadata.getContentLength());
}
use of com.qcloud.cos.model.GetObjectRequest in project cos-java-sdk-v5 by tencentyun.
the class PutObjectDemo method putObjectDemo.
static void putObjectDemo() {
String bucketName = "examplebucket-1251668577";
String key = "abc/abc.txt";
String localPath = "abc.txt";
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setHeader("expires", new Date(1660000000000L));
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, new File(localPath));
putObjectRequest.withMetadata(objectMetadata);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
System.out.println(putObjectResult.getRequestId());
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
COSObject cosObject = cosClient.getObject(getObjectRequest);
System.out.println(cosObject.getObjectMetadata().getRequestId());
cosClient.shutdown();
}
use of com.qcloud.cos.model.GetObjectRequest 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.model.GetObjectRequest in project cos-java-sdk-v5 by tencentyun.
the class PutGetDelTest method testRequestSpecifiedKeyInfoPutGetDel.
@Test
public void testRequestSpecifiedKeyInfoPutGetDel() throws CosServiceException, IOException, InterruptedException {
COSClient cosclient = new COSClient(new AnonymousCOSCredentials(), clientConfig);
File localFile = buildTestFile(1024L);
COSCredentials cosCredentials = new BasicCOSCredentials(secretId, secretKey);
try {
String key = "ut/request-specified-key";
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
putObjectRequest.setCosCredentials(cosCredentials);
cosclient.putObject(putObjectRequest);
GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
getObjectRequest.setCosCredentials(cosCredentials);
cosclient.getObject(getObjectRequest);
DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucket, key);
deleteObjectRequest.setCosCredentials(cosCredentials);
cosclient.deleteObject(deleteObjectRequest);
} finally {
localFile.delete();
cosclient.shutdown();
}
}
Aggregations