Search in sources :

Example 1 with Scope

use of com.tencent.cloud.Scope in project cloud-sdk by mizhousoft.

the class COSObjectStorageServiceImpl method getUploadTempCredential.

/**
 * {@inheritDoc}
 */
@Override
public OSSTempCredential getUploadTempCredential(String bucketName, Set<String> objectNames, int oneDurationSeconds) throws CloudSDKException {
    TreeMap<String, Object> config = new TreeMap<String, Object>();
    // 固定密钥
    config.put("SecretId", profile.getAccessKey());
    // 固定密钥
    config.put("SecretKey", profile.getSecretKey());
    // 临时密钥有效时长,单位是秒
    int durationSeconds = oneDurationSeconds * objectNames.size();
    config.put("durationSeconds", durationSeconds);
    try {
        List<Scope> scopes = new ArrayList<>(5);
        for (String objectName : objectNames) {
            scopes.add(new Scope("name/cos:PutObject", bucketName, profile.getRegion(), objectName));
            scopes.add(new Scope("name/cos:PostObject", bucketName, profile.getRegion(), objectName));
        }
        String policy = CosStsClient.getPolicy(scopes);
        config.put("policy", policy);
        JSONObject credential = CosStsClient.getCredential(config);
        JSONObject cre = credential.getJSONObject("credentials");
        String tmpSecretId = cre.getString("tmpSecretId");
        String tmpSecretKey = cre.getString("tmpSecretKey");
        String token = cre.getString("sessionToken");
        long startTime = credential.getLong("startTime");
        long expiredTime = credential.getLong("expiredTime");
        OSSTempCredential tc = new OSSTempCredential();
        tc.setExpiredTime(expiredTime);
        tc.setSecretId(tmpSecretId);
        tc.setSecretKey(tmpSecretKey);
        tc.setToken(token);
        tc.setStartTime(startTime);
        tc.setBucketName(bucketName);
        tc.setRegion(profile.getRegion());
        tc.setObjectNames(objectNames);
        return tc;
    } catch (IOException e) {
        throw new CloudSDKException(e.getMessage(), e);
    }
}
Also used : CloudSDKException(com.mizhousoft.cloudsdk.CloudSDKException) OSSTempCredential(com.mizhousoft.cloudsdk.oss.OSSTempCredential) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TreeMap(java.util.TreeMap) Scope(com.tencent.cloud.Scope) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject)

Aggregations

CloudSDKException (com.mizhousoft.cloudsdk.CloudSDKException)1 OSSTempCredential (com.mizhousoft.cloudsdk.oss.OSSTempCredential)1 Scope (com.tencent.cloud.Scope)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 JSONObject (org.json.JSONObject)1