我尝试使用此处给出的代码列出 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/