use of com.qcloud.cos.auth.BasicCOSCredentials in project cos-java-sdk-v5 by tencentyun.
the class MultipartUploadDemo method completePartDemo.
// complete完成分片上传
public static void completePartDemo(String uploadId, List<PartETag> partETags) {
// 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";
// uploadid(通过initiateMultipartUpload或者ListMultipartUploads获取)
// 分片上传结束后,调用complete完成分片上传
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
try {
CompleteMultipartUploadResult completeResult = cosclient.completeMultipartUpload(completeMultipartUploadRequest);
String etag = completeResult.getETag();
String crc64 = completeResult.getCrc64Ecma();
} catch (CosServiceException e) {
throw e;
} catch (CosClientException e) {
throw e;
}
cosclient.shutdown();
}
use of com.qcloud.cos.auth.BasicCOSCredentials in project cos-java-sdk-v5 by tencentyun.
the class MultipartUploadDemo method UploadPartDemo.
// 分块上传(上传某一个分片的数据)
public static List<PartETag> UploadPartDemo(String uploadId) {
// 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";
// uploadid(通过initiateMultipartUpload或者ListMultipartUploads获取)
boolean userTrafficLimit = false;
List<PartETag> partETags = new LinkedList<>();
// 生成要上传的数据, 这里初始化一个10M的数据
for (int i = 0; i < 10; i++) {
byte[] data = new byte[1024 * 1024];
UploadPartRequest uploadPartRequest = new UploadPartRequest();
uploadPartRequest.setBucketName(bucketName);
uploadPartRequest.setKey(key);
uploadPartRequest.setUploadId(uploadId);
// 设置分块的数据来源输入流
uploadPartRequest.setInputStream(new ByteArrayInputStream(data));
// 设置分块的长度
// 设置数据长度
uploadPartRequest.setPartSize(data.length);
// 假设要上传的part编号是10
uploadPartRequest.setPartNumber(i + 1);
if (userTrafficLimit) {
uploadPartRequest.setTrafficLimit(8 * 1024 * 1024);
}
try {
UploadPartResult uploadPartResult = cosclient.uploadPart(uploadPartRequest);
PartETag partETag = uploadPartResult.getPartETag();
partETags.add(partETag);
String crc64 = uploadPartResult.getCrc64Ecma();
} catch (CosServiceException e) {
throw e;
} catch (CosClientException e) {
throw e;
}
}
cosclient.shutdown();
return partETags;
}
use of com.qcloud.cos.auth.BasicCOSCredentials in project cos-java-sdk-v5 by tencentyun.
the class AppendObjectDemo method AppendObjectFromLocal.
public static void AppendObjectFromLocal() {
// 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";
try {
File localFile = new File("1M.txt");
AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, localFile);
appendObjectRequest.setPosition(0L);
AppendObjectResult appendObjectResult = cosclient.appendObject(appendObjectRequest);
long nextAppendPosition = appendObjectResult.getNextAppendPosition();
System.out.println(nextAppendPosition);
localFile = new File("2M.txt");
appendObjectRequest = new AppendObjectRequest(bucketName, key, localFile);
appendObjectRequest.setPosition(nextAppendPosition);
appendObjectResult = cosclient.appendObject(appendObjectRequest);
nextAppendPosition = appendObjectResult.getNextAppendPosition();
System.out.println(nextAppendPosition);
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
}
// 关闭客户端
cosclient.shutdown();
}
use of com.qcloud.cos.auth.BasicCOSCredentials in project cos-java-sdk-v5 by tencentyun.
the class BucketDemo method JudgeBucketExistDemo.
// 查询bucket是否存在
public static void JudgeBucketExistDemo() {
// 1 初始化用户身份信息(appid, 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);
String bucketName = "publicreadbucket-1251668577";
// 判断bucket是否存在
cosclient.doesBucketExist(bucketName);
// 关闭客户端
cosclient.shutdown();
}
use of com.qcloud.cos.auth.BasicCOSCredentials in project cos-java-sdk-v5 by tencentyun.
the class BucketDemo method ListBuckets.
public static void ListBuckets() {
// 1 初始化用户身份信息(appid, secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials("AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "****************************");
// 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);
List<Bucket> buckets = cosclient.listBuckets();
for (Bucket bucket : buckets) {
System.out.println(bucket.getName());
System.out.println(bucket.getLocation());
System.out.println(bucket.getOwner());
System.out.println(bucket.getType());
System.out.println(bucket.getBucketType());
}
}
Aggregations