use of com.qcloud.cos.auth.BasicSessionCredentials in project cos-java-sdk-v5 by tencentyun.
the class CachedTemporyTokenCredentialsProvider method fetchNewCOSCredentials.
@Override
public COSCredentials fetchNewCOSCredentials() {
TemporyToken temporyToken = AbstractCOSClientTest.fetchTempToken(this.temporyTokenDuration);
if (temporyToken == null) {
return null;
}
if (temporyToken.getTempSecretId() == null || temporyToken.getTempSecretKey() == null || temporyToken.getTempToken() == null) {
return null;
}
COSSessionCredentials credentials = new BasicSessionCredentials(temporyToken.getTempSecretId(), temporyToken.getTempSecretKey(), temporyToken.getTempToken());
return credentials;
}
use of com.qcloud.cos.auth.BasicSessionCredentials 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.auth.BasicSessionCredentials in project cloud-sdk by mizhousoft.
the class COSObjectStorageServiceImpl method init.
public void init(COSProfile config) throws CloudSDKException {
validate(config);
COSCredentials cred = null;
if (StringUtils.isBlank(config.getSessionToken())) {
cred = new BasicCOSCredentials(config.getAccessKey(), config.getSecretKey());
} else {
cred = new BasicSessionCredentials(config.getAccessKey(), config.getSecretKey(), config.getSessionToken());
}
RegionEnum region = RegionEnum.get(config.getRegion());
ClientConfig clientConfig = new ClientConfig(new Region(region.getValue()));
clientConfig.setHttpProtocol(HttpProtocol.https);
COSClient cosClient = new COSClient(cred, clientConfig);
this.profile = config;
this.cosClient = cosClient;
LOG.info("Init cos client successfully.");
}
use of com.qcloud.cos.auth.BasicSessionCredentials in project cos-java-sdk-v5 by tencentyun.
the class AbstractCOSClientTest method buildTemporyCredentialsCOSClient.
protected static COSClient buildTemporyCredentialsCOSClient(long tokenDuration) {
TemporyToken temporyToken = fetchTempToken(tokenDuration);
BasicSessionCredentials tempCred = new BasicSessionCredentials(temporyToken.getTempSecretId(), temporyToken.getTempSecretKey(), temporyToken.getTempToken());
ClientConfig temporyClientConfig = new ClientConfig(new Region(region));
COSClient tempCOSClient = new COSClient(tempCred, temporyClientConfig);
return tempCOSClient;
}
Aggregations