use of com.qcloud.cos.COSClient in project cos-java-sdk-v5 by tencentyun.
the class TemporyTokenDemo method UseTemporyTokenUploadAndDownload.
// 使用临时秘钥进行上传和下载
public static void UseTemporyTokenUploadAndDownload() {
// 使用云api秘钥,可以获取一个临时secret id,secret key和session token,
BasicSessionCredentials cred = getSessionCredential();
// 设置区域, 这里设置为北京一区
ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
// 生成cos客户端对象
COSClient cosClient = new COSClient(cred, clientConfig);
// 上传的bucket名字
String bucketName = "rabbitliutj-1000000";
// 上传object, 建议20M以下的文件使用该接口
File localFile = new File("src/test/resources/len5M.txt");
String key = "upload_single_demo5M.txt";
// 上传
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
ObjectMetadata objectMetadata = new ObjectMetadata();
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
System.out.println(putObjectResult.getMetadata());
// 下载
File downFile = new File("src/test/resources/len5M_down.txt");
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
ObjectMetadata downObjectMeta = cosClient.getObject(getObjectRequest, downFile);
// 关闭客户端(关闭后台线程)
cosClient.shutdown();
}
use of com.qcloud.cos.COSClient in project cos-java-sdk-v5 by tencentyun.
the class TransferManagerDemo method multipartUploadWithMetaData.
public static void multipartUploadWithMetaData() {
// 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-shanghai"));
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket名需包含appid
String bucketName = "mybucket-1251668577";
ExecutorService threadPool = Executors.newFixedThreadPool(2);
// 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
TransferManager transferManager = new TransferManager(cosclient, threadPool);
String key = "aaa/bbb.txt";
File localFile = new File("src/test/resources/len20M.txt");
ObjectMetadata objectMetadata = new ObjectMetadata();
Map<String, String> userMeta = new HashMap<String, String>();
userMeta.put("usermeta", "hello-mult");
objectMetadata.setUserMetadata(userMeta);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
putObjectRequest.withMetadata(objectMetadata);
try {
// 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待upload结束, 成功返回UploadResult, 失败抛出异常.
long startTime = System.currentTimeMillis();
Upload upload = transferManager.upload(putObjectRequest);
// showTransferProgress(upload);
UploadResult uploadResult = upload.waitForUploadResult();
long endTime = System.currentTimeMillis();
System.out.println("used time: " + (endTime - startTime) / 1000);
System.out.println(uploadResult.getETag());
System.out.println(uploadResult.getCrc64Ecma());
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
transferManager.shutdownNow();
cosclient.shutdown();
}
use of com.qcloud.cos.COSClient in project cos-java-sdk-v5 by tencentyun.
the class TransferManagerDemo method copyFileForSameRegion.
// copy接口支持根据文件大小自动选择copy或者分块copy
// 以下代码展示同园区拷贝, 即将同园区的文件拷贝到另一个园区
public static void copyFileForSameRegion() {
// 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);
TransferManagerConfiguration transferManagerConfiguration = new TransferManagerConfiguration();
transferManagerConfiguration.setMultipartCopyThreshold(20 * 1024 * 1024);
transferManager.setConfiguration(transferManagerConfiguration);
// 要拷贝的bucket region, 支持跨园区拷贝
Region srcBucketRegion = new Region("ap-beijing-1");
// 源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 {
Copy copy = transferManager.copy(copyObjectRequest);
// 返回一个异步结果copy, 可同步的调用waitForCopyResult等待copy结束, 成功返回CopyResult, 失败抛出异常.
CopyResult copyResult = copy.waitForCopyResult();
System.out.println(copyResult.getCrc64Ecma());
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
transferManager.shutdownNow();
cosclient.shutdown();
}
use of com.qcloud.cos.COSClient in project cos-java-sdk-v5 by tencentyun.
the class TransferManagerDemo method copyFileForDiffRegion.
// copy接口支持根据文件大小自动选择copy或者分块copy
// 以下代码展示跨园区拷贝, 即将一个园区的文件拷贝到另一个园区
public static void copyFileForDiffRegion() {
// 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";
COSClient srcCOSClient = new COSClient(cred, new ClientConfig(srcBucketRegion));
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketRegion, srcBucketName, srcKey, destBucketName, destKey);
try {
Copy copy = transferManager.copy(copyObjectRequest, srcCOSClient, null);
// 返回一个异步结果copy, 可同步的调用waitForCopyResult等待copy结束, 成功返回CopyResult, 失败抛出异常.
CopyResult copyResult = copy.waitForCopyResult();
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
transferManager.shutdownNow();
srcCOSClient.shutdown();
cosclient.shutdown();
}
use of com.qcloud.cos.COSClient in project cos-java-sdk-v5 by tencentyun.
the class TransferManagerDemo method resumableDownloadFile.
public static void resumableDownloadFile() {
// 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-shanghai"));
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
ExecutorService threadPool = Executors.newFixedThreadPool(5);
// 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
TransferManager transferManager = new TransferManager(cosclient, threadPool);
GetObjectRequest getObj = new GetObjectRequest("mybucket-1000000000", "/path/to/key");
File dstFile = new File("dstFile");
Download download = transferManager.download(getObj, dstFile, true);
showTransferProgress(download);
try {
download.waitForCompletion();
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
transferManager.shutdownNow();
cosclient.shutdown();
}
Aggregations