azureazure-blob-storageazure-cliblobstorage

JMESPath with numeric value in Azure


I'm trying to select all the blobs with content (basically, to avoid directories).

I'm trying the following query:

az storage blob list --account-name myaccount --account-key myaccountkey --container-name mycontainer --prefix myprefix --query "[?properties.contentLength > '0'].name" --num-results "*" --output tsv|more

Azure CLI crashes with the following error:

TypeError: '>' not supported between instances of 'int' and 'str'

The full trace is:

Traceback (most recent call last):
  File "/usr/local/Cellar/azure-cli/2.62.0/libexec/lib/python3.11/site-packages/knack/cli.py", line 233, in invoke
    cmd_result = self.invocation.execute(args)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/azure-cli/2.62.0/libexec/lib/python3.11/site-packages/azure/cli/core/commands/__init__.py", line 676, in execute
    self.cli_ctx.raise_event(EVENT_INVOKER_FILTER_RESULT, event_data=event_data)
  File "/usr/local/Cellar/azure-cli/2.62.0/libexec/lib/python3.11/site-packages/knack/cli.py", line 170, in raise_event
    func(self, **kwargs)
  File "/usr/local/Cellar/azure-cli/2.62.0/libexec/lib/python3.11/site-packages/knack/query.py", line 45, in filter_output
    kwargs['event_data']['result'] = query_expression.search(
                                     ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/azure-cli/2.62.0/libexec/lib/python3.11/site-packages/jmespath/parser.py", line 509, in search
    result = interpreter.visit(self.parsed, value)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/azure-cli/2.62.0/libexec/lib/python3.11/site-packages/jmespath/visitor.py", line 94, in visit
    return method(node, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/azure-cli/2.62.0/libexec/lib/python3.11/site-packages/jmespath/visitor.py", line 180, in visit_filter_projection
    if self._is_true(self.visit(comparator_node, element)):
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/azure-cli/2.62.0/libexec/lib/python3.11/site-packages/jmespath/visitor.py", line 94, in visit
    return method(node, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/azure-cli/2.62.0/libexec/lib/python3.11/site-packages/jmespath/visitor.py", line 158, in visit_comparator
    return comparator_func(left, right)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: '>' not supported between instances of 'int' and 'str'

NOTE: Also reported as issue in github Azure


Solution

  • I think that this error is caused for comparing a string with an integer in your JMESPath query. Check once whether contentLength is treated as an integer by removing the quotes around 0 in your query.

    Test using the following command:

    az storage blob list --account-name myaccount --account-key myaccountkey --container-name mycontainer --prefix myprefix --query "[?properties.contentLength > \`0\`].name" --num-results "*" --output tsv | more
    

    This generally resolves TypeError.