从Java中的AWS参数存储中获取价值

时间:2020-09-17 10:02:30

标签: java aws-cdk jira-rest-java-api aws-parameter-store

我需要使用我的Java代码获取存储在aws参数存储区中的api令牌。我需要api令牌来认证JiraClient。但是我不确定获取价值的正确方法。这是我尝试过的。

import com.atlassian.jira.rest.client.api.IssueRestClient;
import com.atlassian.jira.rest.client.api.JiraRestClient;
import com.atlassian.jira.rest.client.api.domain.input.IssueInput;
import com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder;
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
import software.amazon.awscdk.services.ssm.StringParameter;

import java.net.URI;

    public class JiraClient {
        private final String email;
        private final String apiToken;
        private final String jiraUrl;
        private final JiraRestClient restClient;
    
        public JiraClient(String email, String jiraUrl) {
    
            this.email = email;
            this.jiraUrl = jiraUrl;
            this.apiToken = StringParameter.valueForSecureStringParameter(this, "/JIRA/Token", 1);
            this.restClient = getJiraRestClient();
        }
    
        private JiraRestClient getJiraRestClient() {
            return new AsynchronousJiraRestClientFactory()
                    .createWithBasicHttpAuthentication(getJiraUri(), this.email, this.apiToken);
        }
    
        public String createIssue(String projectKey, Long issueTypeId, String issueSummary) {
    
            IssueRestClient issueClient = restClient.getIssueClient();
            IssueInput newIssue = new IssueInputBuilder(projectKey, issueTypeId, issueSummary).build();
            return issueClient.createIssue(newIssue).claim().getKey();
        }
    
        private URI getJiraUri() {
            return URI.create(this.jiraUrl);
        }
    }

this.apiToken = StringParameter.valueForSecureStringParameter(this, "/JIRA/Token", 1);行中,第一个参数应为“构造对象”。

1 个答案:

答案 0 :(得分:1)

您正在使用aws-cdk中的StringParameter.valueForSecureStringParameter方法,这是AWS的“基础结构即代码”服务。

您应该使用在AWS Java SDK的AWSSimpleSystemsManagement界面中定义的getParameter()方法。