我正在使用来迭代特定IBM帐户中的所有资源。我的麻烦在于,分页似乎“不适合我”。当我传入"next_url“时,我仍然会从调用中得到相同的列表。
这是我的测试代码。我成功地打印了我的许多COS实例,但我似乎只能打印第一个page....maybe --我已经看了这么长时间,只是遗漏了一些东西-- obvious...anyone知道为什么我不能检索下一页吗?
try:
####### authenticate and set the service url
auth = IAMAuthenticator(RESOURCE_CONTROLLER_APIKEY)
service = ResourceControllerV2(authenticator=auth)
service.set_service_url(RESOURCE_CONTROLLER_URL)
####### Retrieve the resource instance listing
r = service.list_resource_instances().get_result()
####### get the row count and resources list
rows_count = r['rows_count']
resources = r['resources']
while rows_count > 0:
print('Number of rows_count {}'.format(rows_count))
next_url = r['next_url']
for i, resource in enumerate(resources):
type = resource['id'].split(':')[4]
if type == 'cloud-object-storage':
instance_name = resource['name']
instance_id = resource['guid']
crn = resource['crn']
print('Found instance id : name - {} : {}'.format(instance_id, instance_name))
############### this is SUPPOSED to get the next page
r = service.list_resource_instances(start=next_url).get_result()
rows_count = r['rows_count']
resources = r['resources']
except Exception as e:
Error = 'Error : {}'.format(e)
print(Error)
exit(1)发布于 2022-06-24 12:14:32
从列出资源实例的API文档的角度来看,next_url的值包括URL路径和 start 参数,包括它的start令牌。
要检索下一页,只需传入参数start,并将令牌作为值。这并不理想。
我通常不使用SDK,而是使用简单的Python请求。然后,我可以使用端点(基本) URI + next_url作为完整URI。
如果坚持使用SDK,请使用urllib.parse提取查询参数。没有经过测试,但类似于:
from urllib.parse import urlparse,parse_qs
o=urlparse(next_url)
q=parse_qs(o.query)
r = service.list_resource_instances(start=q['start'][0]).get_result()发布于 2022-06-28 18:40:16
您是否可以使用Search来列出帐户中的资源,而不是资源控制器?搜索索引正是为该操作设置的,而来自资源控制器的分页结果似乎要强得多。
https://stackoverflow.com/questions/72736166
复制相似问题