azure cli-使用python查询JMESPath



所以我正在构建一个非常简单的脚本,我最初是在bash上构建的,但我想在多个平台上重用它,所以我决定在python中实现它。我确实在bash中实现了它,并使用JMESPath实现了查询,但Python似乎抱怨引用。或者至少我认为这就是问题所在,所以请其他几双眼睛为我指出这个问题。

这是代码:

from azure.cli.core import get_default_cli as azcli
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
#extensions = [".jpg", ".pdf"]
storageAccounts = []
while True:
selected = input ("Enter the storage account names one per line, when ready to continue type 'done': ")
if selected.lower() == "done":
break
elif not selected:
print (f"{selected}")
continue
storageAccounts.append(selected)
print ("Selected accounts:")
for account in storageAccounts:
print(account)

print (f"these are the selected accounts {storageAccounts}")
date = input("Enter the date you want to query from in the YYYY-MM-DD format: ")

for blobs in storageAccounts:
azcli().invoke(['storage', 'blob', 'list',
'--account-name ', '%s' % blobs ,
'--container-name ' , 'backup',
'--num-results', "*",
'--auth-mode', 'key',
'--output', 'table',
'--query', "[?properties.creationTime>='%s'.{name:name, created:properies.creationTime}" % date])

因此,当它转到最后一行,即JMESPath查询时,它表示这是一个无效的JMESPath_type值。我试着逃避单引号,我也试着对整个查询进行单引号引用,但似乎都不起作用,我确信我错过了一些愚蠢的东西,但这已经让我很沮丧了!:(

这是输出和错误:

Selected accounts:
account1
account2
Enter the storage account names one per line, when ready to continue type 'done': done
these are the selected accounts ['account1', 'account2']
Enter the date you want to query from in the YYYY-MM-DD format: 2022-02-15
argument --query: invalid jmespath_type value: "[?properties.creationTime>='2022-02-15'.{name:name, created:properies.creationTime}"
To learn more about --query, please visit: 'https://learn.microsoft.com/cli/azure/query-azure-cli'
Process finished with exit code 2

操作系统-Windows 10

IDE-PyCharm 2021.3.2(社区版(

Python-3.9.10

根据@β.εηιτ.βε的建议,查看您发布的代码,以帮助其他社区成员解决同样的问题。代码似乎是正确的,我们只需要添加缺少的参数。

参数--查询:无效的jmespath_type值:"[?properties.creationTime>='2022-02-15'。{name:name,created:properies.creationTime}"若要了解有关--query的更多信息,请访问:'https://learn.microsoft.com/cli/azure/query-azure-cli'

要解决上述问题,请确保在--query参数中添加],如下所示。

'--query', "[?properties.creationTime>='%s'.{name:name, created:properies.creationTime}" % date])

正如你已经修复了它,并得到了另一个错误:

它还抱怨这个无法识别的参数:--帐户名测试1

为了解决这个问题,'--account-name '参数中似乎有空白。

请尝试删除空白'--account-name'添加&运行代码,希望它能运行。

由于没有在本地设置,我没有进行测试。

有关更多信息,请参阅以下链接:-

  • GitHub|JMES关于blob创建/修改时间的查询不起作用&Azure文档CLi Python

  • MICRSOFT文档:-如何使用JMESPath查询查询Azure CLI命令输出

最新更新