Skip to content

Commit 2ba21fe

Browse files
Merge pull request #274775 from tamram/tamram24-0508
update CLI examples per customer feedback
2 parents f7fa3ef + 7382651 commit 2ba21fe

File tree

1 file changed

+79
-47
lines changed

1 file changed

+79
-47
lines changed

articles/storage/blobs/blob-cli.md

Lines changed: 79 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
title: Manage block blobs with Azure CLI
33
titleSuffix: Azure Storage
4-
description: Manage blobs with Azure CLI
4+
description: Manage blobs with Azure CLI.
55
author: StevenMatthew
66

77
ms.author: shaas
88
ms.service: azure-blob-storage
99
ms.topic: how-to
10-
ms.date: 08/28/2023
10+
ms.date: 06/11/2024
1111
ms.devlang: azurecli
1212
ms.custom: devx-track-azurecli
1313
---
@@ -75,7 +75,7 @@ The second operation demonstrates the use of the `az storage blob upload-batch`
7575
#!/bin/bash
7676
storageAccount="<storage-account>"
7777
containerName="demo-container"
78-
lastModified=`date -d "7 days ago" '+%Y-%m-%dT%H:%MZ'`
78+
lastModified=$(date +%Y:%m:%d -d "7 days ago")
7979
8080
path="C:\\temp\\"
8181
filename="demo-file.txt"
@@ -102,65 +102,97 @@ az storage blob upload-batch \
102102

103103
## List blobs
104104

105-
By default, the `az storage blob list` command lists all blobs stored within a container. You can use various approaches to refine the scope of your search. There's no restriction on the number of containers or blobs a storage account may have. To potentially avoid retrieving thousands of blobs, it's a good idea to limit the amount of data returned.
105+
By default, the `az storage blob list` command lists all blobs stored within a container. You can use various approaches to refine the scope of your search. There's no restriction on the number of containers or blobs a storage account can have. To potentially avoid retrieving thousands of blobs, it's a good idea to limit the amount of data returned.
106106

107-
Use the `--prefix` parameter to select either a single known file or a range of files whose names begin with a defined string.
107+
Use the `--prefix` parameter to select either a single known file or a range of files whose names begin with a defined string. You can specify a virtual directory as part of the `--prefix` parameter.
108108

109-
By default, only blobs are returned in a listing operation. In some scenarios, you may want to pass a value for the `--include` parameter to return additional types of objects such as soft-deleted blobs, snapshots, and versions. These values can be combined to return more than multiple object types.
109+
By default, only blobs are returned in a listing operation. In some scenarios, you might want to pass a value for the `--include` parameter to return additional types of objects such as soft-deleted blobs, snapshots, and versions. These values can be combined to return multiple object types.
110110

111-
The `--num-results` parameter can be used to limit the number of unfiltered blobs returned from a container. A service limit of 5,000 is imposed on all Azure resources. This limit ensures that manageable amounts of data are retrieved and that performance isn't impacted. If the number of blobs returned exceeds either the `--num-results` value or the service limit, a continuation token is returned. This token allows you to use multiple requests to retrieve any number of blobs. More information is available on [Enumerating blob resources](/rest/api/storageservices/enumerating-blob-resources).
111+
The `--num-results` parameter can be used to limit the number of blobs returned from a container. A service limit of 5,000 is imposed on all Azure resources. This limit ensures that manageable amounts of data are retrieved and that performance isn't impacted. If the number of blobs returned exceeds either the `--num-results` value or the service limit, a continuation token is returned. This token allows you to use multiple requests to retrieve any number of blobs. More information is available on [Enumerating blob resources](/rest/api/storageservices/enumerating-blob-resources).
112112

113-
The following example shows several approaches used to provide a list of blobs. The first approach lists a single blob within a specific container resource. The second approach uses the `--prefix` parameter to list all blobs in all containers with a prefix of *louis*. The search is restricted to five containers using the `--num-results` parameter. The third approach uses `--num-results` and `--marker` parameters to limit the retrieval of all blobs within a container.
113+
The following example shows several approaches used to provide a list of blobs. The first approach lists all blobs within a specified container. The second approach uses the `--prefix` parameter to list all blobs in the containers that begin with the specified prefix.The third approach uses the `--num-results` parameter to limit the results returned and the `--show-next-marker` parameter to include the continuation token in the results. When a continuation token is present in the results, it is passed to the subsequent call to `az storage blob list` to retrieve the next set of results.
114114

115-
For additional information, see the [az storage blob list](/cli/azure/storage/blob#az-storage-blob-list) reference.
115+
For more information, see the [az storage blob list](/cli/azure/storage/blob#az-storage-blob-list) reference.
116116

117117
```azurecli-interactive
118-
119118
#!/bin/bash
120119
storageAccount="<storage-account>"
121-
blobName="demo-file.txt"
122-
containerName="demo-container"
123-
blobPrefix="img-louis"
120+
containerName="<container-name>"
121+
blobPrefix="<prefix-string>"
124122
numResults=5
125123
126-
#Approach 1: List all blobs in a named container
124+
#Approach 1: List all blobs in a container by name.
125+
127126
az storage blob list \
127+
--account-name $storageAccount \
128128
--container $containerName \
129+
--query "[].name" \
130+
--auth-mode login \
131+
--output tsv
132+
133+
#Approach 2: Use the --prefix parameter to list blobs starting with specified prefix.
134+
135+
az storage blob list \
129136
--account-name $storageAccount \
130-
--prefix $blobName
131-
--auth-mode login
137+
--container $containerName \
138+
--prefix $blobPrefix \
139+
--query "[].name" \
140+
--auth-mode login \
141+
--output tsv
132142
133-
#Approach 2: Use the --prefix parameter to list blobs in all containers
143+
#Approach 3: Use the continuation token to return the complete set of results.
134144
135-
containerList=$( \
136-
az storage container list \
137-
--query "[].name" \
138-
--num-results $numResults \
139-
--account-name $storageAccount \
140-
--auth-mode login \
141-
--output tsv
142-
)
143-
for row in $containerList
144-
do
145-
tmpName=$(echo $row | sed -e 's/\r//g')
146-
echo $tmpName
147-
az storage blob list \
148-
--prefix $blobPrefix \
149-
--container $tmpName \
150-
--account-name $storageAccount \
151-
--auth-mode login
145+
get_blobs() {
146+
if [ -z "$nextMarker" ]; then
147+
az storage blob list --container-name $containerName --num-results $numResults --account-name $storageAccount --show-next-marker --only-show-errors --auth-mode login
148+
else
149+
az storage blob list --container-name $containerName --num-results $numResults --marker $nextMarker --account-name $storageAccount --show-next-marker --only-show-errors --auth-mode login
150+
fi
151+
}
152+
153+
total=0
154+
nextMarker=""
155+
while true; do
156+
blobs=$(get_blobs $containerName $numResults $storageAccount $nextMarker)
157+
nextMarker=""
158+
blobsLength=$(echo $blobs | jq length)
159+
160+
if [ $blobsLength -le 0 ]; then
161+
break
162+
fi
163+
164+
blobIndex=0
165+
while read blob; do
166+
if [ $blobIndex -eq $(($blobsLength-1)) ];
167+
then
168+
nextMarker="$(echo $blob | jq -r .nextMarker)"
169+
else
170+
blobName=$(echo $blob | jq .name)
171+
echo "blobname: $blobName"
172+
fi
173+
((blobIndex++))
174+
done <<<$(echo $blobs | jq -c '.[]')
175+
176+
total=$(($total+$blobsLength-1))
177+
echo "Processed $total blobs so far"
178+
# echo "nextMarker: $nextMarker"
179+
if [ "${nextMarker}" = "null" ]; then
180+
echo -e "\nAccountName: $storageAccount, ContainerName: $containerName "
181+
echo "Processed $total blobs in total."
182+
break
183+
fi
152184
done
153185
```
154186

155187
## Download a blob
156188

157-
Depending on your use case, you'll use either the `az storage blob download` or `az storage blob download-batch` command to download blobs. To download an individual blob, call the `az storage blob download` command directly and pass values for the `--container-name`, `--file`, and `--name` parameters. The blob will be downloaded to the shell directory by default, but an alternate location can be specified. The operation will fail with an error if your specified path doesn't exist.
189+
Depending on your use case, you'll use either the `az storage blob download` or `az storage blob download-batch` command to download blobs. To download an individual blob, call the `az storage blob download` command directly and pass values for the `--container-name`, `--file`, and `--name` parameters. The blob is downloaded to the shell directory by default, but an alternate location can be specified. The operation will fail with an error if your specified path doesn't exist.
158190

159191
To recursively download multiple blobs from a storage container, use the `az storage blob download-batch` command. This command supports Unix filename pattern matching with the `--pattern` parameter. The supported patterns are `*`, `?`, `[seq]`, and `[!seq]`. To learn more, refer to the Python documentation on [Unix filename pattern matching](https://docs.python.org/3/library/fnmatch.html).
160192

161-
The following sample code provides an example of both single and multiple download approaches. It also offers a simplified approach to searching all containers for specific files using a wildcard. Because some environments may have many thousands of resources, using the `--num-results` parameter is recommended.
193+
The following sample code provides an example of both single and multiple download approaches. It also offers a simplified approach to searching all containers for specific files using a wildcard. Because some environments can have many thousands of resources, using the `--num-results` parameter is recommended.
162194

163-
For additional information, see the [az storage blob download](/cli/azure/storage/blob#az-storage-blob-download) and [az storage blob download batch](/cli/azure/storage/blob#az-storage-blob-download-batch)reference.
195+
For more information, see the [az storage blob download](/cli/azure/storage/blob#az-storage-blob-download) and [az storage blob download batch](/cli/azure/storage/blob#az-storage-blob-download-batch)reference.
164196

165197
```azurecli-interactive
166198
#!/bin/bash
@@ -226,7 +258,7 @@ User-defined metadata consists of one or more name-value pairs that you specify
226258

227259
To read blob properties or metadata, you must first retrieve the blob from the service. Use the `az storage blob show` command to retrieve a blob's properties and metadata, but not its content. The following example retrieves a blob and lists its properties.
228260

229-
For additional information, see the [az storage blob show](/cli/azure/storage/blob#az-storage-blob-show) reference.
261+
For more information, see the [az storage blob show](/cli/azure/storage/blob#az-storage-blob-show) reference.
230262

231263
```azurecli-interactive
232264
#!/bin/bash
@@ -245,7 +277,7 @@ az storage blob show \
245277

246278
Blob metadata is an optional set of name/value pairs associated with a blob. As shown in the previous example, there's no metadata associated with a blob initially, though it can be added when necessary. To read, use the `az storage blob metadata show` command. To update blob metadata, you'll use `az storage blob metadata update` and supply an array of key-value pairs. For more information, see the [az storage blob metadata](/cli/azure/storage/blob/metadata) reference.
247279

248-
For additional information, see the [az storage blob metadata](/cli/azure/storage/blob#az-storage-blob-metadata) reference.
280+
For more information, see the [az storage blob metadata](/cli/azure/storage/blob#az-storage-blob-metadata) reference.
249281

250282
The example below first updates and then commits a blob's metadata, and then retrieves it.
251283

@@ -278,7 +310,7 @@ az storage blob metadata show \
278310

279311
## Copy operations for blobs
280312

281-
There are many scenarios in which blobs of different types may be copied. Examples in this article are limited to block blobs. Azure CLI offers commands that perform operations on one resource or on multiple resources, depending on your requirements.
313+
There are many scenarios in which blobs of different types can be copied. Examples in this article are limited to block blobs. Azure CLI offers commands that perform operations on one resource or on multiple resources, depending on your requirements.
282314

283315
To copy a specific blob, use the `az storage blob copy start` command and specify values for source and destination containers and blobs. It's also possible to provide a uniform resource identifier (URI), share, or shared access signature (SAS) as the source.
284316

@@ -291,7 +323,7 @@ You can use the `az storage blob copy start-batch` command to recursively copy m
291323
292324
For more information, see the [az storage blob copy](/cli/azure/storage/blob/copy) reference.
293325

294-
The following sample code provides an example of both single and multiple copy operations. Because some environments may have many thousands of resources, using the `--num-results` parameter is recommended. The first example copies the **secret-town-road.png** blob from the **photos** container to the **locations** container. Both containers exist within the same storage account. The result verifies the success of the copy operation.
326+
The following sample code provides an example of both single and multiple copy operations. Because some environments can have many thousands of resources, using the `--num-results` parameter is recommended. The first example copies the **secret-town-road.png** blob from the **photos** container to the **locations** container. Both containers exist within the same storage account. The result verifies the success of the copy operation.
295327

296328
```azurecli-interactive
297329
#!/bin/bash
@@ -333,11 +365,11 @@ az storage blob snapshot \
333365

334366
When you change a blob's tier, you move the blob and all of its data to the target tier. You can change the tier between **hot**, **cool**, and **archive** with the `az storage blob set-tier` command.
335367

336-
Depending on your requirements, you may also utilize the *Copy Blob* operation to copy a blob from one tier to another. The *Copy Blob* operation will create a new blob in the desired tier while leaving the source blob remains in the original tier.
368+
Depending on your requirements, you may also utilize the *Copy Blob* operation to copy a blob from one tier to another. The *Copy Blob* operation creates a new blob in the desired tier while leaving the source blob remains in the original tier.
337369

338370
Changing tiers from **cool** or **hot** to **archive** takes place almost immediately. After a blob is moved to the **archive** tier, it's considered to be offline and can't be read or modified. Before you can read or modify an archived blob's data, you'll need to rehydrate it to an online tier. Read more about [Blob rehydration from the archive tier](archive-rehydrate-overview.md).
339371

340-
For additional information, see the [az storage blob set-tier](/cli/azure/storage/blob#az-storage-blob-set-tier) reference.
372+
For more information, see the [az storage blob set-tier](/cli/azure/storage/blob#az-storage-blob-set-tier) reference.
341373

342374
The following sample code sets the tier to **hot** for a single, named blob within the `archive` container.
343375

@@ -363,7 +395,7 @@ Blob index tags make data management and discovery easier. Blob index tags are u
363395
364396
The following example illustrates how to add blob index tags to a series of blobs. The example reads data from an XML file and uses it to create index tags on several blobs. To use the sample code, create a local *blob-list.xml* file in your *C:\temp* directory. The XML data is provided below.
365397

366-
For additional information, see the [az storage blob set-tier](/cli/azure/storage/blob#az-storage-blob-set-tier) reference.
398+
For more information, see the [az storage blob set-tier](/cli/azure/storage/blob#az-storage-blob-set-tier) reference.
367399

368400
```xml
369401
<Venue Name="House of Prime Rib" Type="Restaurant">
@@ -476,7 +508,7 @@ az storage blob delete-batch \
476508
--auth-mode login
477509
```
478510

479-
In some cases, it's possible to retrieve blobs that have been deleted. If your storage account's soft delete data protection option is enabled, the `--include d` parameter and value will return blobs deleted within the account's retention period. To learn more about soft delete, refer to thee [Soft delete for blobs](soft-delete-blob-overview.md) article.
511+
In some cases, it's possible to retrieve blobs that have been deleted. If your storage account's soft delete data protection option is enabled, passing the `--include d` parameter returns blobs that were deleted within the account's retention period. To learn more about soft delete, refer to thee [Soft delete for blobs](soft-delete-blob-overview.md) article.
480512

481513
Use the following examples to retrieve a list of blobs deleted within container's associated retention period. The first example displays a list of all recently deleted blobs and the dates on which they were deleted. The second example lists all deleted blobs matching a specific prefix.
482514

@@ -510,11 +542,11 @@ az storage blob list \
510542
## Restore a deleted blob
511543
As mentioned in the [List blobs](#list-blobs) section, you can configure the soft delete data protection option on your storage account. When enabled, it's possible to restore containers deleted within the associated retention period. You may also use versioning to maintain previous versions of your blobs for each recovery and restoration.
512544

513-
If blob versioning and blob soft delete are both enabled, then modifying, overwriting, deleting, or restoring a blob automatically creates a new version. The method you'll use to restore a deleted blob will depend upon whether versioning is enabled on your storage account.
545+
If blob versioning and blob soft delete are both enabled, then modifying, overwriting, deleting, or restoring a blob automatically creates a new version. The method you'll use to restore a deleted blob depends upon whether versioning is enabled on your storage account.
514546

515547
The following code sample restores all soft-deleted blobs or, if versioning is enabled, restores the latest version of a blob. It first determines whether versioning is enabled with the `az storage account blob-service-properties show` command.
516548

517-
If versioning is enabled, the `az storage blob list` command retrieves a list of all uniquely-named blob versions. Next, the blob versions on the list are retrieved and ordered by date. If no versions are found with the `isCurrentVersion` attribute value, the `az storage blob copy start` command is used to make an active copy of the blob's latest version.
549+
If versioning is enabled, the `az storage blob list` command retrieves a list of all uniquely named blob versions. Next, the blob versions on the list are retrieved and ordered by date. If no versions are found with the `isCurrentVersion` attribute value, the `az storage blob copy start` command is used to make an active copy of the blob's latest version.
518550

519551
If versioning is disabled, the `az storage blob undelete` command is used to restore each soft-deleted blob in the container.
520552

0 commit comments

Comments
 (0)