【发布时间】:2021-01-25 14:44:49
【问题描述】:
我有一个脚本,它使用 boto3 和成本浏览器 API 成功地从 AWS 获取成本数据。当我输入日期以提供 1 月至 12 月的数据时,我只获得 1 月至 4 月的数据。然后我尝试了 4 月至 7 月和 8 月至 10 月和 11 月至 12 月,这有效/给了我数据。我试图从脚本的 1 次运行中获取 Jan-Dec 数据,但我没有收到错误消息。我相信这与 nextpagetoken 和 while 循环无法正常工作有关。如何修复 nextpagetoken 部分以提供所有结果?
**Paginator 不符合这个特定的 api 调用
我的代码与此非常相似:
https://github.com/hjacobs/aws-cost-and-usage-report/blob/master/aws-cost-and-usage-report.py
results = []
token = None
while True:
if token:
kwargs = {'NextPageToken': token}
else:
kwargs = {}
data = cd.get_cost_and_usage(TimePeriod={'Start': '2020-01-01', 'End':
'2020-12-10'}, Granularity='DAILY', Metrics=['AmortizedCost'],
GroupBy=[{'Type': 'DIMENSION', 'Key':
'LINKED_ACCOUNT'}]} ,{'Dimensions': {'Key': 'LINKED_ACCOUNT','Values':
['123948500267568']}}]}, **kwargs)
for info in data['ResultsByTime']:
for group in info['Groups']:
print(group['Keys'][0], info['TimePeriod']['Start'],
group['Metrics']['AmortizedCost']['Amount'])#, group['Keys'][1])
token = data.get('NextPageToken')
if not token:
break
【问题讨论】:
标签: amazon-web-services api while-loop boto3