如何在AWS CLI响应中进行分页?

7

我试图在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] http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-reserved-instances-offerings.html中的文档建议使用start-token,我应该如何做呢?

3个回答

6

尊重2017年 marjamis 的解决方案,该方案可能适用于之前的CLI版本,请查看从Mac笔记本电脑和aws-cli/2.1.2分页的实现方法。

# 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


1

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

--starting-token (字符串)

一个标记,用于指定从哪里开始分页。这是先前截断响应中的NextToken。

此外:

--max-items (整数)

要返回的总项目数。如果可用项目的总数大于max-items中指定的值,则输出中将提供NextToken以便恢复分页。


  1. 我认为你贴错了链接...?
  2. 但是如果你使用 starting-token 作为标志,命令将无法工作。你必须使用 next-token
- keian
@keian 1. 抱歉,我已经用正确的链接更新了帖子。2. 第一次执行命令时,您不能使用“--starting-token”参数,因为您尚未收到任何“NextToken”(如果响应由于分页而被截断,则会作为响应的一部分提供)。 - matsev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接