use of com.qcloud.cos.auth.BasicCOSCredentials in project cos-java-sdk-v5 by tencentyun.
the class PutGetDelTest method testRequestSpecifiedTmpKeyInfoPutGetDel.
@Test
public void testRequestSpecifiedTmpKeyInfoPutGetDel() throws CosServiceException, IOException, InterruptedException {
COSClient cosclient = buildTemporyCredentialsCOSClient(1800L);
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();
}
}
use of com.qcloud.cos.auth.BasicCOSCredentials in project cos-java-sdk-v5 by tencentyun.
the class TransferManagerTest method testTransferManagerCopySmallFileFromDiffRegion.
// transfer manager对不同园区5G以下文件进行使用put object copy
@Ignore
public void testTransferManagerCopySmallFileFromDiffRegion() throws CosServiceException, CosClientException, InterruptedException {
if (!judgeUserInfoValid()) {
return;
}
COSCredentials srcCred = new BasicCOSCredentials(secretId, secretKey);
String srcRegion = "ap-guangzhou";
ClientConfig srcClientConfig = new ClientConfig(new Region(srcRegion));
COSClient srcCOSClient = new COSClient(srcCred, srcClientConfig);
String srcBucketName = "chengwus3gz-1251668577";
String srcKey = "ut_copy/len1G.txt";
String destKey = "ut_copy_dest/len1G.txt";
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(new Region(srcRegion), srcBucketName, srcKey, bucket, destKey);
Copy copy = transferManager.copy(copyObjectRequest, srcCOSClient, null);
CopyResult copyResult = copy.waitForCopyResult();
assertNotNull(copyResult.getRequestId());
assertNotNull(copyResult.getDateStr());
clearObject(destKey);
}
use of com.qcloud.cos.auth.BasicCOSCredentials in project cos-java-sdk-v5 by tencentyun.
the class TransferManagerTest method testTransferManagerCopyBigFileFromSameRegion.
// transfer manager对相同园区使用put object copy
@Ignore
public void testTransferManagerCopyBigFileFromSameRegion() throws CosServiceException, CosClientException, InterruptedException {
if (!judgeUserInfoValid()) {
return;
}
COSCredentials srcCred = new BasicCOSCredentials(secretId, secretKey);
String srcRegion = region;
ClientConfig srcClientConfig = new ClientConfig(new Region(srcRegion));
COSClient srcCOSClient = new COSClient(srcCred, srcClientConfig);
String srcBucketName = bucket;
String srcKey = "ut_copy/len10G_1.txt";
String destKey = "ut_copy_dest/len10G_2.txt";
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(new Region(srcRegion), srcBucketName, srcKey, bucket, destKey);
Copy copy = transferManager.copy(copyObjectRequest, srcCOSClient, null);
CopyResult copyResult = copy.waitForCopyResult();
assertNotNull(copyResult.getRequestId());
assertNotNull(copyResult.getDateStr());
}
use of com.qcloud.cos.auth.BasicCOSCredentials in project cos-java-sdk-v5 by tencentyun.
the class KMSEncryptionClientDemo method createCosClient.
static COSClient createCosClient(String region) {
// 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials("AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
// 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region(region));
// 为防止请求头部被篡改导致的数据无法解密,强烈建议只使用 https 协议发起请求
clientConfig.setHttpProtocol(HttpProtocol.https);
// 初始化 KMS 加密材料
KMSEncryptionMaterials encryptionMaterials = new KMSEncryptionMaterials(cmk);
// 使用AES/CTR模式,并将加密信息存储在文件元信息中.
// 如果想要此次加密的对象被 COS 其他的 SDK 解密下载,则必须选择 AesCtrEncryption 模式
CryptoConfiguration cryptoConf = new CryptoConfiguration(CryptoMode.AesCtrEncryption).withStorageMode(CryptoStorageMode.ObjectMetadata);
// // 如果 kms 服务的 region 与 cos 的 region 不一致,则在加密信息里指定 kms 服务的 region
// cryptoConf.setKmsRegion(kmsRegion);
// // 如果需要可以为 KMS 服务的 cmk 设置对应的描述信息。
// encryptionMaterials.addDescription("kms-region", "guangzhou");
// 生成加密客户端EncryptionClient, COSEncryptionClient是COSClient的子类, 所有COSClient支持的接口他都支持。
// EncryptionClient覆盖了COSClient上传下载逻辑,操作内部会执行加密操作,其他操作执行逻辑和COSClient一致
COSEncryptionClient cosEncryptionClient = new COSEncryptionClient(new COSStaticCredentialsProvider(cred), new KMSEncryptionMaterialsProvider(encryptionMaterials), clientConfig, cryptoConf);
return cosEncryptionClient;
}
use of com.qcloud.cos.auth.BasicCOSCredentials in project cos-java-sdk-v5 by tencentyun.
the class ListObjectsDemo method listObjectsDemo.
public static void listObjectsDemo() {
// 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);
// bucket名称, 需包含appid
String bucketName = "mybucket-1251668577";
ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
// 设置bucket名称
listObjectsRequest.setBucketName(bucketName);
// prefix表示列出的object的key以prefix开始
listObjectsRequest.setPrefix("");
// 设置最大遍历出多少个对象, 一次listobject最大支持1000
listObjectsRequest.setMaxKeys(1000);
// listObjectsRequest.setDelimiter("/");
ObjectListing objectListing = null;
try {
objectListing = cosclient.listObjects(listObjectsRequest);
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
}
// common prefix表示表示被delimiter截断的路径, 如delimter设置为/, common prefix则表示所有子目录的路径
List<String> commonPrefixs = objectListing.getCommonPrefixes();
// object summary表示所有列出的object列表
List<COSObjectSummary> cosObjectSummaries = objectListing.getObjectSummaries();
for (COSObjectSummary cosObjectSummary : cosObjectSummaries) {
// 文件的路径key
String key = cosObjectSummary.getKey();
// 文件的etag
String etag = cosObjectSummary.getETag();
// 文件的长度
long fileSize = cosObjectSummary.getSize();
// 文件的存储类型
String storageClasses = cosObjectSummary.getStorageClass();
System.out.println("key: " + key);
}
cosclient.shutdown();
}
Aggregations