python - 如何在列出所有 VM 时使用 Azure Python SDK 中的 nextLink 属性

标签 python azure azure-virtual-machine azure-sdk azure-sdk-python

我尝试使用此处给出的代码列出 Azure 中订阅中的所有虚拟机:

 from azure.mgmt.compute import ComputeManagementClient
 from azure.identity import ClientSecretCredential
    
 credentials = ClientSecretCredential(
    client_id='',
    client_secret='',
    tenant_id=''
 )
    
 subID= ''
    
 computer_client = ComputeManagementClient(credentials,subID)
 vms = computer_client.virtual_machines.list_all()
    
 for vm in vms:
    print( vm.name )

这很好用。它列出了第一页的所有虚拟机。但是,如何获取 next_link 属性并使用下一页虚拟机?语法是什么? 在文档中:https://learn.microsoft.com/en-us/python/api/azure-mgmt-compute/azure.mgmt.compute.v2019_07_01.models.virtualmachinelistresult?view=azure-python

它说:

next_link str

Required

The URI to fetch the next page of VMs. Call ListNext() with this URI to fetch the next page of Virtual Machines.

我只是想知道使用 next_link 属性并调用 ListNext() 的语法是什么样的?

最佳答案

你不需要关心nextLink,listAll()返回的对象是一个迭代器,当您可以继续迭代时,它将自动为您使用 nextLink。如果您希望所有虚拟机同时存在于内存中,您可以执行类似 list(vms) 的操作在将迭代器转换为实际列表时,将立即读取所有页面并将所有内容放入内存中。

如果您还有其他问题,请随时在我们的 Github 存储库上提出问题:https://github.com/Azure/azure-sdk-for-python/issues

(我是 Microsoft Azure SDK for Python 的工程经理)

关于python - 如何在列出所有 VM 时使用 Azure Python SDK 中的 nextLink 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73505119/

相关文章:

python - Node & python 不返回相同的 hash256

python - 向可从自定义基类访问的 celery 函数添加额外属性

python - 使现有函数可以从类中访问

c# - 正确部署了Azure中的所有文件,但仍然无法看到网页

asp.net-mvc - 另一个 Controller MVC5的显示 View

linux - Azure 运行命令返回截断的输出

azure - 自动调整 Azure VM 镜像大小

python - 使用 matplotlib 在 python 中的 xkcd 注释中不会中断行

azure - Azure 上的 URL 重写配置

适用于 Linux 的 Azure 扩展脚本