繁体   English   中英

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

[英]AWS SSM parameter store not fetching all key/values

有人能告诉我为什么下面的代码只从参数存储中获取几个条目吗?

   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());
    });

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());

以下是基于上述代码的新版 AWS SSM 管理器 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());
      }
 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'))的路径(request.withPath('/your/path/parameterName'))中有更多值,它将发送一个令牌,指示给定路径中有更多值,并且您必须使用从前一个请求中收到的令牌发出以下请求,才能获得其余的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM