问题描述
为了从 Parameter Store SSM 收集一些值,我一直在使用 boto3,这是我使用的代码,非常简单:
def get_raw_parameters_group_by_namespace(namespace_path):
raw_params_response = None
try:
if not namespace_path:
raise Exception('Namespace path should be specified to get data')
raw_params_response = ssm_ps.get_parameters_by_path(Path = namespace_path)
except Exception as e:
raise Exception('An error ocurred while trying to get parameters group: ' + str(e))
return raw_params_response
我以前在 SSM 中有大约 7 到 10 个参数,该方法效果很好,但是,这些天我们需要添加一些额外的参数,并且它们的数量增加到 14,所以我尝试在 boto3 ssm 中添加一个属性名为“MaxResults”的方法并将其设置为 50:
ssm_ps.get_parameters_by_path(Path = namespace_path,MaxResults = 50)
但我得到以下信息:
"error": "An error ocurred while trying to get parameters group: An error occurred (ValidationException) when calling the GetParametersByPath operation: 1 validation error detected: Value '50' at 'maxResults' Failed to satisfy constraint: Member must have value less than or equal to 10."
与团队讨论后,增加帐户中的配额不是一个选项,所以我想知道使用 "NextToken"
属性是否是一个不错的选择。
我不确定如何使用它,我搜索了示例,但找不到有用的东西。请问有人知道如何使用NextToken吗?或者它应该如何工作的例子?
我尝试了类似的东西:
raw_params_response = ssm_ps.get_parameters_by_path(Path = namespace_path,NextToken = 'Token')
但我不确定这个的用法。
提前致谢。
解决方法
我记得在某个时候遇到过这个问题。
我是这样使用它的:
import boto3
client = boto3.client('ssm',region_name='eu-central-1')
paginator = client.get_paginator('get_parameters_by_path')
response_iterator = paginator.paginate(
Path='/some/path'
)
parameters=[]
for page in response_iterator:
for entry in page['Parameters']:
parameters.append(entry)
你会在参数中得到一个类似 [{"Name": "/some/path/param,"Value": "something"}]
的列表,其中包含路径下的所有参数。
*edit: response 将比 Name,Value 键更丰富。检查分页器文档!