I have a container on Azure and I want to see a list of all the blob I have in it. If I type on my shell
az storage blob list --container-name MYCONTAINER --account-name MYSTORAGEACCOUNT
I get as an output a list of all my blobs like this:
[
{
"content": null,
"deleted": false,
"metadata": null,
"name": "myblobname.csv",
"properties": {
"appendBlobCommittedBlockCount": null,
"blobTier": "Hot",
"blobTierChangeTime": null,
"blobTierInferred": true,
"blobType": "BlockBlob",
"contentLength": 275821,
"contentRange": null,
"contentSettings": {
"cacheControl": null,
"contentDisposition": null,
"contentEncoding": null,
"contentLanguage": null,
"contentMd5": "YmQ hefF/6IdNRv GbD94A==",
"contentType": "application/octet-stream"
},
"copy": {
"completionTime": null,
"id": null,
"progress": null,
"source": null,
"status": null,
"statusDescription": null
},
"creationTime": "2021-11-03T03:28:40 00:00",
"deletedTime": null,
"etag": "0x8D99E7A082AFA6A",
"lastModified": "2021-11-03T03:28:40 00:00",
"lease": {
"duration": null,
"state": "available",
"status": "unlocked"
},
"pageBlobSequenceNumber": null,
"remainingRetentionDays": null,
"serverEncrypted": true
},
"snapshot": null
},
{
...
}
]
but, as an output, I only would like the name and properties.contentLength.
can anyone suggest me how should I edit my script in order to get only the params I want? Thank you very much
CodePudding user response:
You're looking for How to query Azure CLI command output using a JMESPath query.
The Azure CLI uses the
--query
argument to execute a JMESPath query on the results of commands. JMESPath is a query language for JSON, giving you the ability to select and modify data from CLI output. Queries are executed on the JSON output before any display formatting.
In your specific case, try this:
az storage blob list --container-name MYCONTAINER --account-name MYSTORAGEACCOUNT --query '[].properties.contentLength'
In this query statement, you're parsing all of the entities in an array (your results), and accessing the contentLength under properties for each of the array items.