0

So I'm building a quite simple script which I originally built on bash but I want to reuse it on multiple platforms so I decided to do it in python. I did get it working in bash and I got the querying using JMESPath working but Python seems to complain about quoting. Or at least that's what I think it's the issue, so couple of other pair of eyes would be really appreciated to point me to the issue please.

Here's the code:

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])

so when it's going to the last line which is the JMESPath query it says that it's an invalid jmespath_type value. I tried escaping the single quotes, I tried also single quoting the whole query but none seem to work and I am sure it's something stupid that I am missing but it already frustrated me enough! :(

Here's the output and the error:

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

OS - Windows 10

IDE - PyCharm 2021.3.2 (Community Edition)

Python - 3.9.10

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
Vlatko
  • 13
  • 7
  • You are missing the closing parenthesis `]` of your filter projection: `"[?properties.creationTime>='2022-02-15'.{name:name` <= there before the `.{`. – β.εηοιτ.βε Mar 22 '22 at 08:22
  • @β.εηοιτ.βε Jesus christ, how could I not see that hahaha!! Thanks!. Well, progress!! And new set of errors... Right now when I execute it just loads one string from the list in storageAccounts actually the last one, or that's what I see when it does the `for blobs in storageAccounts`. Also it complains about this `unrecognized arguments: --account-name test1`, but when I try to put it in the same line seems like it works but not when it's in a new line? Any ideas? – Vlatko Mar 23 '22 at 09:28
  • From the docs: [_Must be used in conjunction with either storage account key or a SAS token._](https://learn.microsoft.com/en-us/cli/azure/storage/blob?view=azure-cli-latest) but you have neither `--sas-token` nor `--account-key` specified in your command. – β.εηοιτ.βε Mar 23 '22 at 18:54
  • @β.εηοιτ.βε seems like it's not needed as by only using `az login` it's doing the job, i.e. listing the files in the blobs. so that works now thanks :) – Vlatko Mar 28 '22 at 14:03

1 Answers1

0

As suggested by @β.εηοιτ.βε and looked into your code posting an answer to help other community members to fix their issue for the same. The code it Seems correct where we just need to add missing parameters.

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'

To resolve the above issue make sure to add the ] in your --query parameter as below.

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

As you have fixed it, and getting an another error :

Also it complains about this unrecognized arguments: --account-name test1

To resolve it, it seems there is whitespace in '--account-name ' parameter.

Please try to remove the whitespace '--account-name' add & run the code hopefully it will run.

I have not tested due to have not setup in my local.

For more information please refer the below links:-

AjayKumarGhose
  • 4,257
  • 2
  • 4
  • 15
  • Hi. It seems that it wasn't a whitespace issue but rather on how the interpreter catch a variable inside a string. so I had to escape the `--account-name` by using ` '%s' % blobs, ` The issue with the query now seems to be that it doesn't select the creationTime in the table output for some reason. I must mention that I've changed the way of execution i.e. I am now storing the strings in a variable within the for loop called `collection` which contains the whole `az` command and then I invoke it with a `try: azcli().invoke(collection)`. – Vlatko Mar 28 '22 at 14:06
  • The query now looks like this: `'--query', "[?properties.creationTime>=\'%s\'].{name:name, created:properies.creationTime}" % date]` However, when executed it just shows the `Name` column in the table but not `Created` one too. Any ideas why is that so?? – Vlatko Mar 28 '22 at 14:12