如何对 AWS CLI 响应进行分页?

问题描述 投票:0回答:4

我正在尝试对 EC2 预留实例产品进行分页,但似乎无法通过 CLI 进行分页(见下文)。

% aws ec2 describe-reserved-instances-offerings --max-results 20                                                                                 
{
    "NextToken": "someToken", 
    "ReservedInstancesOfferings": [
        {
             ...
        }
    ]
}
% aws ec2 describe-reserved-instances-offerings --max-results 20 --starting-token someToken
Parameter validation failed:
Unknown parameter in input: "PaginationConfig", must be one of: DryRun, ReservedInstancesOfferingIds, InstanceType, AvailabilityZone, ProductDescription, Filters, InstanceTenancy, OfferingType, NextToken, MaxResults, IncludeMarketplace, MinDuration, MaxDuration, MaxInstanceCount

[1] 中的文档说要使用

start-token
。我该怎么做?

[1] http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-reserved-instances-offerings.html

amazon-web-services pagination aws-cli
4个回答
6
投票

尊重 marjamis 的 2017 年解决方案,该解决方案必须适用于之前的 CLI 版本,请参阅从 Mac 笔记本电脑和 aws-cli/2.1.2 的 bash 中从 AWS 进行分页的工作方法

# The scope of this example requires that credentials are already available or
# are passed in with the AWS CLI command.  
# The parsing example uses jq, available from https://stedolan.github.io/jq/

# The below command is the one being executed and should be adapted appropriately.
# Note that the max items may need adjusting depending on how many results are returned.
aws_command="aws emr list-instances --max-items 333 --cluster-id $active_cluster"
unset NEXT_TOKEN

function parse_output() {
  if [ ! -z "$cli_output" ]; then
    # The output parsing below also needs to be adapted as needed.
    echo $cli_output | jq -r '.Instances[] | "\(.Ec2InstanceId)"' >> listOfinstances.txt
    NEXT_TOKEN=$(echo $cli_output | jq -r ".NextToken")
  fi
}

# The command is run and output parsed in the below statements.
cli_output=$($aws_command)
parse_output

# The below while loop runs until either the command errors due to throttling or
# comes back with a pagination token.  In the case of being throttled / throwing
# an error, it sleeps for three seconds and then tries again.
while [ "$NEXT_TOKEN" != "null" ]; do
  if [ "$NEXT_TOKEN" == "null" ] || [ -z "$NEXT_TOKEN" ] ; then
    echo "now running: $aws_command "
    sleep 3
    cli_output=$($aws_command)
    parse_output
  else
    echo "now paginating: $aws_command --starting-token $NEXT_TOKEN"
    sleep 3
    cli_output=$($aws_command --starting-token $NEXT_TOKEN)
    parse_output
  fi
done  #pagination loop

3
投票

看起来像是一些被破坏的文档。

如果您运行以下命令,则此操作有效:

aws ec2 describe-reserved-instances-offerings --max-results 20 --next-token someToken

翻译错误消息,它说它期望

NextToken
,可以在 CLI 上表示为
next-token


0
投票

如果您继续阅读您提供的参考文档,您将了解到:

--起始令牌(字符串)

指定从哪里开始分页的标记。这是之前截断的响应中的 NextToken。

此外:

--最大项目(整数)

要退回的商品总数。如果可用项目总数超过 max-items 中指定的值,则输出中将提供 NextToken,您可以使用它来恢复分页。


0
投票

警告!我尝试使用

--max-results
并注意到它花了很长时间并且结果有很多重复。快速浏览一下 documentation,会警告
--max-results
--page-size
应设置为相同的值。

来自文档:

如果为 --page-size 和 --max-items 指定不同的值,您可能会得到意外的结果,其中项目丢失或重复。为了防止这种情况,请对 --page-size 和 --max-items 使用相同的数字来同步 AWS CLI 分页与底层服务的分页。

一旦我将

--page-size
设为与
--max-results
相同的值,它不仅消除了重复项,而且结果开始很快返回。

© www.soinside.com 2019 - 2024. All rights reserved.