首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ResourceControllerV2和分页问题列出IBM资源

使用ResourceControllerV2和分页问题列出IBM资源
EN

Stack Overflow用户
提问于 2022-06-23 20:23:48
回答 2查看 104关注 0票数 1

我正在使用来迭代特定IBM帐户中的所有资源。我的麻烦在于,分页似乎“不适合我”。当我传入"next_url“时,我仍然会从调用中得到相同的列表。

这是我的测试代码。我成功地打印了我的许多COS实例,但我似乎只能打印第一个page....maybe --我已经看了这么长时间,只是遗漏了一些东西-- obvious...anyone知道为什么我不能检索下一页吗?

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-06-24 12:14:32

列出资源实例的API文档的角度来看,next_url的值包括URL路径和 start 参数,包括它的start令牌。

要检索下一页,只需传入参数start,并将令牌作为值。这并不理想。

我通常不使用SDK,而是使用简单的Python请求。然后,我可以使用端点(基本) URI + next_url作为完整URI。

如果坚持使用SDK,请使用urllib.parse提取查询参数。没有经过测试,但类似于:

代码语言:javascript
复制
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()
票数 0
EN

Stack Overflow用户

发布于 2022-06-28 18:40:16

您是否可以使用Search来列出帐户中的资源,而不是资源控制器?搜索索引正是为该操作设置的,而来自资源控制器的分页结果似乎要强得多。

https://cloud.ibm.com/apidocs/search#search

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72736166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档