【问题标题】:AWS SSM parameter store not fetching all key/valuesAWS SSM 参数存储未获取所有键/值
【发布时间】:2020-05-12 15:59:35
【问题描述】:

有人可以告诉我为什么下面的代码只从参数存储中获取很少的条目吗?

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

【问题讨论】:

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


    【解决方案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());
    

    【讨论】:

    • 非常感谢。是的,确实是分页响应。您的解决方案就像一个魅力。在您的回复中更新了我的代码。
    【解决方案2】:

    这是基于上述代码的新 2.0 版 AWS SSM 管理器的代码。请注意,我已将 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());
          }
    

    【讨论】:

      【解决方案3】:
       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'))中有更多值,它将发送一个令牌指示给定路径中有更多值,您必须使用收到的令牌发出以下请求从上一个请求中获取其余值。

      【讨论】:

        猜你喜欢
        • 2020-03-31
        • 1970-01-01
        • 1970-01-01
        • 2019-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-14
        • 2020-03-30
        相关资源
        最近更新 更多