@Test public void testGetCredential() { TreeMap<String, Object> config = new TreeMap<String, Object>(); try { Properties properties = new Properties(); File configFile = new File("local.properties"); properties.load(new FileInputStream(configFile)); config.put("secretId", properties.getProperty("SecretId")); config.put("secretKey", properties.getProperty("SecretKey")); if (properties.containsKey("https.proxyHost")) { System.setProperty("https.proxyHost", properties.getProperty("https.proxyHost")); System.setProperty("https.proxyPort", properties.getProperty("https.proxyPort")); } config.put("durationSeconds", 1800); config.put("bucket", "123123123-1253653367"); config.put("region", "ap-beijing"); config.put("allowPrefix", "exampleobject"); String[] allowActions = new String[] { "name/cos:PutObject", "name/cos:PostObject", "name/cos:InitiateMultipartUpload", "name/cos:ListMultipartUploads", "name/cos:ListParts", "name/cos:UploadPart", "name/cos:CompleteMultipartUpload" }; config.put("allowActions", allowActions); JSONObject credential = CosStsClient.getCredential(config); System.out.println(credential.toString(4)); } catch (Exception e) { e.printStackTrace(); throw new IllegalArgumentException("no valid secret !"); } }
public static JSONObject getCredential(TreeMap<String, Object> config) throws IOException { TreeMap<String, Object> params = new TreeMap<String, Object>(); Parameters parameters = new Parameters(); parameters.parse(config); if(parameters.secretId == null) throw new IllegalArgumentException("secretId is null"); if(parameters.secretKey == null) throw new IllegalArgumentException("secretKey is null"); String policy = parameters.policy; if (policy != null) { params.put("Policy", policy); } else { params.put("Policy", getPolicy(parameters).toString()); } params.put("DurationSeconds", parameters.duration); params.put("Name", "cos-sts-java"); params.put("Action", "GetFederationToken"); params.put("Version", "2018-08-13"); String region = RegionCodeFilter.convert(parameters.region); params.put("Region", region); if(parameters.secretType != null) { params.put("SecretType", parameters.secretType); } String host = STS_DEFAULT_HOST; if (parameters.host != null) { host = parameters.host; } String stsHost = STS_DEFAULT_HOST; if (host.startsWith("sts.") && host.endsWith(".tencentcloudapi.com")) { stsHost = host; } String path = "/"; String result = null; JSONObject jsonResult = null; try { result = Request.send(params, (String) parameters.secretId, parameters.secretKey, "POST", host, stsHost, path); jsonResult = new JSONObject(result); JSONObject data = jsonResult.optJSONObject("Response"); if (data == null) { data = jsonResult; } long expiredTime = data.getLong("ExpiredTime"); data.put("startTime", expiredTime - parameters.duration); return downCompat(data); } catch (Exception e) { if (jsonResult != null) { JSONObject response = jsonResult.optJSONObject("Response"); if (response != null) { JSONObject error = response.optJSONObject("Error"); if (error != null) { String message = error.optString("Message"); String code = error.optString("Code"); if ("InvalidParameterValue".equals(code) && message != null && message.contains("Region")) { if (RegionCodeFilter.block(region)) { return getCredential(config); } } } } } throw new IOException("result = " + result, e); } }
CosStsClient { public static JSONObject getCredential(TreeMap<String, Object> config) throws IOException { TreeMap<String, Object> params = new TreeMap<String, Object>(); Parameters parameters = new Parameters(); parameters.parse(config); if(parameters.secretId == null) throw new IllegalArgumentException("secretId is null"); if(parameters.secretKey == null) throw new IllegalArgumentException("secretKey is null"); String policy = parameters.policy; if (policy != null) { params.put("Policy", policy); } else { params.put("Policy", getPolicy(parameters).toString()); } params.put("DurationSeconds", parameters.duration); params.put("Name", "cos-sts-java"); params.put("Action", "GetFederationToken"); params.put("Version", "2018-08-13"); String region = RegionCodeFilter.convert(parameters.region); params.put("Region", region); if(parameters.secretType != null) { params.put("SecretType", parameters.secretType); } String host = STS_DEFAULT_HOST; if (parameters.host != null) { host = parameters.host; } String stsHost = STS_DEFAULT_HOST; if (host.startsWith("sts.") && host.endsWith(".tencentcloudapi.com")) { stsHost = host; } String path = "/"; String result = null; JSONObject jsonResult = null; try { result = Request.send(params, (String) parameters.secretId, parameters.secretKey, "POST", host, stsHost, path); jsonResult = new JSONObject(result); JSONObject data = jsonResult.optJSONObject("Response"); if (data == null) { data = jsonResult; } long expiredTime = data.getLong("ExpiredTime"); data.put("startTime", expiredTime - parameters.duration); return downCompat(data); } catch (Exception e) { if (jsonResult != null) { JSONObject response = jsonResult.optJSONObject("Response"); if (response != null) { JSONObject error = response.optJSONObject("Error"); if (error != null) { String message = error.optString("Message"); String code = error.optString("Code"); if ("InvalidParameterValue".equals(code) && message != null && message.contains("Region")) { if (RegionCodeFilter.block(region)) { return getCredential(config); } } } } } throw new IOException("result = " + result, e); } } }
CosStsClient { public static JSONObject getCredential(TreeMap<String, Object> config) throws IOException { TreeMap<String, Object> params = new TreeMap<String, Object>(); Parameters parameters = new Parameters(); parameters.parse(config); if(parameters.secretId == null) throw new IllegalArgumentException("secretId is null"); if(parameters.secretKey == null) throw new IllegalArgumentException("secretKey is null"); String policy = parameters.policy; if (policy != null) { params.put("Policy", policy); } else { params.put("Policy", getPolicy(parameters).toString()); } params.put("DurationSeconds", parameters.duration); params.put("Name", "cos-sts-java"); params.put("Action", "GetFederationToken"); params.put("Version", "2018-08-13"); String region = RegionCodeFilter.convert(parameters.region); params.put("Region", region); if(parameters.secretType != null) { params.put("SecretType", parameters.secretType); } String host = STS_DEFAULT_HOST; if (parameters.host != null) { host = parameters.host; } String stsHost = STS_DEFAULT_HOST; if (host.startsWith("sts.") && host.endsWith(".tencentcloudapi.com")) { stsHost = host; } String path = "/"; String result = null; JSONObject jsonResult = null; try { result = Request.send(params, (String) parameters.secretId, parameters.secretKey, "POST", host, stsHost, path); jsonResult = new JSONObject(result); JSONObject data = jsonResult.optJSONObject("Response"); if (data == null) { data = jsonResult; } long expiredTime = data.getLong("ExpiredTime"); data.put("startTime", expiredTime - parameters.duration); return downCompat(data); } catch (Exception e) { if (jsonResult != null) { JSONObject response = jsonResult.optJSONObject("Response"); if (response != null) { JSONObject error = response.optJSONObject("Error"); if (error != null) { String message = error.optString("Message"); String code = error.optString("Code"); if ("InvalidParameterValue".equals(code) && message != null && message.contains("Region")) { if (RegionCodeFilter.block(region)) { return getCredential(config); } } } } } throw new IOException("result = " + result, e); } }  }
CosStsClient { public static JSONObject getCredential(TreeMap<String, Object> config) throws IOException { TreeMap<String, Object> params = new TreeMap<String, Object>(); Parameters parameters = new Parameters(); parameters.parse(config); if(parameters.secretId == null) throw new IllegalArgumentException("secretId is null"); if(parameters.secretKey == null) throw new IllegalArgumentException("secretKey is null"); String policy = parameters.policy; if (policy != null) { params.put("Policy", policy); } else { params.put("Policy", getPolicy(parameters).toString()); } params.put("DurationSeconds", parameters.duration); params.put("Name", "cos-sts-java"); params.put("Action", "GetFederationToken"); params.put("Version", "2018-08-13"); String region = RegionCodeFilter.convert(parameters.region); params.put("Region", region); if(parameters.secretType != null) { params.put("SecretType", parameters.secretType); } String host = STS_DEFAULT_HOST; if (parameters.host != null) { host = parameters.host; } String stsHost = STS_DEFAULT_HOST; if (host.startsWith("sts.") && host.endsWith(".tencentcloudapi.com")) { stsHost = host; } String path = "/"; String result = null; JSONObject jsonResult = null; try { result = Request.send(params, (String) parameters.secretId, parameters.secretKey, "POST", host, stsHost, path); jsonResult = new JSONObject(result); JSONObject data = jsonResult.optJSONObject("Response"); if (data == null) { data = jsonResult; } long expiredTime = data.getLong("ExpiredTime"); data.put("startTime", expiredTime - parameters.duration); return downCompat(data); } catch (Exception e) { if (jsonResult != null) { JSONObject response = jsonResult.optJSONObject("Response"); if (response != null) { JSONObject error = response.optJSONObject("Error"); if (error != null) { String message = error.optString("Message"); String code = error.optString("Code"); if ("InvalidParameterValue".equals(code) && message != null && message.contains("Region")) { if (RegionCodeFilter.block(region)) { return getCredential(config); } } } } } throw new IOException("result = " + result, e); } }  static JSONObject getCredential(TreeMap<String, Object> config); static String getPolicy(List<Scope> scopes); }
CosStsClient { public static JSONObject getCredential(TreeMap<String, Object> config) throws IOException { TreeMap<String, Object> params = new TreeMap<String, Object>(); Parameters parameters = new Parameters(); parameters.parse(config); if(parameters.secretId == null) throw new IllegalArgumentException("secretId is null"); if(parameters.secretKey == null) throw new IllegalArgumentException("secretKey is null"); String policy = parameters.policy; if (policy != null) { params.put("Policy", policy); } else { params.put("Policy", getPolicy(parameters).toString()); } params.put("DurationSeconds", parameters.duration); params.put("Name", "cos-sts-java"); params.put("Action", "GetFederationToken"); params.put("Version", "2018-08-13"); String region = RegionCodeFilter.convert(parameters.region); params.put("Region", region); if(parameters.secretType != null) { params.put("SecretType", parameters.secretType); } String host = STS_DEFAULT_HOST; if (parameters.host != null) { host = parameters.host; } String stsHost = STS_DEFAULT_HOST; if (host.startsWith("sts.") && host.endsWith(".tencentcloudapi.com")) { stsHost = host; } String path = "/"; String result = null; JSONObject jsonResult = null; try { result = Request.send(params, (String) parameters.secretId, parameters.secretKey, "POST", host, stsHost, path); jsonResult = new JSONObject(result); JSONObject data = jsonResult.optJSONObject("Response"); if (data == null) { data = jsonResult; } long expiredTime = data.getLong("ExpiredTime"); data.put("startTime", expiredTime - parameters.duration); return downCompat(data); } catch (Exception e) { if (jsonResult != null) { JSONObject response = jsonResult.optJSONObject("Response"); if (response != null) { JSONObject error = response.optJSONObject("Error"); if (error != null) { String message = error.optString("Message"); String code = error.optString("Code"); if ("InvalidParameterValue".equals(code) && message != null && message.contains("Region")) { if (RegionCodeFilter.block(region)) { return getCredential(config); } } } } } throw new IOException("result = " + result, e); } }  static JSONObject getCredential(TreeMap<String, Object> config); static String getPolicy(List<Scope> scopes); static final String STS_DEFAULT_HOST; }