簡體   English   中英

azure cli - 使用 python 進行 JMESPath 查詢

[英]azure cli - JMESPath querying using python

所以我正在構建一個非常簡單的腳本,我最初是在 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 值。 我試過 escaping 單引號,我也試過單引號整個查詢,但似乎都沒有用,我確信我錯過了一些愚蠢的東西,但它已經讓我很沮喪了::(

這是 output 和錯誤:

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

正如@ β.εηοιτ.βε所建議的那樣,並查看了您的代碼並發布了一個答案,以幫助其他社區成員解決同樣的問題。 代碼似乎是正確的,我們只需要添加缺少的參數。

argument --query: invalid jmespath_type value: "[?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])

當您修復它時,出現另一個錯誤:

它還抱怨這個無法識別的 arguments: --account-name test1

要解決它, '--account-name '參數中似乎有空格。

請嘗試刪除空格'--account-name'添加並運行代碼,希望它能運行。

由於沒有在我的本地設置,我沒有測試過。

有關更多信息,請參閱以下鏈接:-

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM