AWS SSM参数存储未获取所有键/值

时间:2018-10-18 11:22:09

标签: java amazon-web-services aws-sdk

有人可以让我知道为什么下面的代码只从参数存储中获取一些条目吗?

   GetParametersByPathRequest getParametersByPathRequest = new GetParametersByPathRequest();
      getParametersByPathRequest.withPath("/").setRecursive(true);
      getParametersByPathRequest.setWithDecryption(true);
   GetParametersByPathResult result = client.getParametersByPath(getParametersByPathRequest);

   result.getParameters().forEach(parameter -> {
        System.out.println(parameter.getName() + " - > " + parameter.getValue());
    });

3 个答案:

答案 0 :(得分:1)

GetParametersByPath是分页操作。每次调用后,您必须从结果对象中检索NextToken,如果它不是null也不为空,则必须进行另一个调用,并将其添加到请求中。

下面是使用DescribeParameters的示例,它具有相同的行为:

DescribeParametersRequest request = new DescribeParametersRequest();
DescribeParametersResult response;
do
{
    response = client.describeParameters(request);
    for (ParameterMetadata param : response.getParameters())
    {
        // do something with metadata
    }
    request.setNextToken(response.getNextToken());
}
while ((response.getNextToken() != null) && ! respose.getNextToken.isEmpty());

答案 1 :(得分:0)

以下是基于上述代码的新版本的AWS SSM Manager 2.0版本的代码。注意,我将maxResults设置为1以证明循环。您将要删除它。 AWS已在新代码中提到他们希望强调不变性。

使用此依赖项:

<dependency>
  <groupId>software.amazon.awssdk</groupId>
  <artifactId>ssm</artifactId>
  <version>2.10.32</version>
</dependency>

我想到了以下代码:

 private void refreshCache() {
        StopWatch sw = StopWatch.createStarted();
        GetParametersByPathRequest request = GetParametersByPathRequest.builder()
            .path(prefix)
            .withDecryption(useDecryption)
            .maxResults(1)
            .build();

        GetParametersByPathResponse response;
        do {
          response = ssm.getParametersByPath(request);
          for (Parameter p : response.parameters()) {
            //do something with the values.
          }
          request = GetParametersByPathRequest.builder()
              .path(prefix)
              .withDecryption(useDecryption)
              .nextToken(response.nextToken())
              .maxResults(1)
              .build();
        }
        while (StringUtils.isNotBlank(response.nextToken()));
        LOG.trace("Refreshed parameters in {}ms", sw.getTime());
      }

答案 2 :(得分:0)

 private void getSsmParams() {

    AWSSimpleSystemsManagement client = AWSSimpleSystemsManagementClientBuilder.defaultClient();
    GetParametersByPathRequest request = new GetParametersByPathRequest();
    request.withRecursive(true);
    request.withPath('/your/path/parameterName').setWithDecryption(true);

    GetParametersByPathResult response;

    do {

      response = client.getParametersByPath(request);

      for (Parameter p : response.parameters()) {
        //do something with the values. maybe add to a list
      }

      request.setNextToken(response.getNextToken())
    }

    while (StringUtils.isNotBlank(response.getNextToken()));

  }

上面对我有用的代码。ssm一次只发送10个参数,因此,如果要以编程方式从ssm参数存储中获取10个以上的参数,则必须使用多个调用来获取它们。这里的令牌很重要,如果您给定的路径(request.withPath('/your/path/parameterName'))中有更多的值,它将发送一个令牌,表明给定的路径中有更多的值,并且您必须使用从上一个请求中收到的令牌,以获取其余值。