Search in sources :

Example 1 with BasicSessionCredentials

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;
}
Also used : COSSessionCredentials(com.qcloud.cos.auth.COSSessionCredentials) BasicSessionCredentials(com.qcloud.cos.auth.BasicSessionCredentials)

Example 2 with BasicSessionCredentials

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();
}
Also used : COSClient(com.qcloud.cos.COSClient) BasicSessionCredentials(com.qcloud.cos.auth.BasicSessionCredentials) PutObjectResult(com.qcloud.cos.model.PutObjectResult) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 3 with BasicSessionCredentials

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.");
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicSessionCredentials(com.qcloud.cos.auth.BasicSessionCredentials) RegionEnum(com.mizhousoft.tencent.RegionEnum) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 4 with BasicSessionCredentials

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;
}
Also used : BasicSessionCredentials(com.qcloud.cos.auth.BasicSessionCredentials) Region(com.qcloud.cos.region.Region)

Aggregations

BasicSessionCredentials (com.qcloud.cos.auth.BasicSessionCredentials)4 Region (com.qcloud.cos.region.Region)3 COSClient (com.qcloud.cos.COSClient)2 ClientConfig (com.qcloud.cos.ClientConfig)2 RegionEnum (com.mizhousoft.tencent.RegionEnum)1 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)1 COSCredentials (com.qcloud.cos.auth.COSCredentials)1 COSSessionCredentials (com.qcloud.cos.auth.COSSessionCredentials)1 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)1 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)1 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)1 PutObjectResult (com.qcloud.cos.model.PutObjectResult)1 File (java.io.File)1