You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/storage/blobs/blob-cli.md
+79-47Lines changed: 79 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
---
2
2
title: Manage block blobs with Azure CLI
3
3
titleSuffix: Azure Storage
4
-
description: Manage blobs with Azure CLI
4
+
description: Manage blobs with Azure CLI.
5
5
author: StevenMatthew
6
6
7
7
ms.author: shaas
8
8
ms.service: azure-blob-storage
9
9
ms.topic: how-to
10
-
ms.date: 08/28/2023
10
+
ms.date: 06/11/2024
11
11
ms.devlang: azurecli
12
12
ms.custom: devx-track-azurecli
13
13
---
@@ -75,7 +75,7 @@ The second operation demonstrates the use of the `az storage blob upload-batch`
75
75
#!/bin/bash
76
76
storageAccount="<storage-account>"
77
77
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")
79
79
80
80
path="C:\\temp\\"
81
81
filename="demo-file.txt"
@@ -102,65 +102,97 @@ az storage blob upload-batch \
102
102
103
103
## List blobs
104
104
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.
106
106
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.
108
108
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.
110
110
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).
112
112
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.
114
114
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.
116
116
117
117
```azurecli-interactive
118
-
119
118
#!/bin/bash
120
119
storageAccount="<storage-account>"
121
-
blobName="demo-file.txt"
122
-
containerName="demo-container"
123
-
blobPrefix="img-louis"
120
+
containerName="<container-name>"
121
+
blobPrefix="<prefix-string>"
124
122
numResults=5
125
123
126
-
#Approach 1: List all blobs in a named container
124
+
#Approach 1: List all blobs in a container by name.
125
+
127
126
az storage blob list \
127
+
--account-name $storageAccount \
128
128
--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 \
129
136
--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
132
142
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.
134
144
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
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.
158
190
159
191
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).
160
192
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.
162
194
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.
164
196
165
197
```azurecli-interactive
166
198
#!/bin/bash
@@ -226,7 +258,7 @@ User-defined metadata consists of one or more name-value pairs that you specify
226
258
227
259
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.
228
260
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.
230
262
231
263
```azurecli-interactive
232
264
#!/bin/bash
@@ -245,7 +277,7 @@ az storage blob show \
245
277
246
278
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.
247
279
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.
249
281
250
282
The example below first updates and then commits a blob's metadata, and then retrieves it.
251
283
@@ -278,7 +310,7 @@ az storage blob metadata show \
278
310
279
311
## Copy operations for blobs
280
312
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.
282
314
283
315
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.
284
316
@@ -291,7 +323,7 @@ You can use the `az storage blob copy start-batch` command to recursively copy m
291
323
292
324
For more information, see the [az storage blob copy](/cli/azure/storage/blob/copy) reference.
293
325
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.
295
327
296
328
```azurecli-interactive
297
329
#!/bin/bash
@@ -333,11 +365,11 @@ az storage blob snapshot \
333
365
334
366
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.
335
367
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.
337
369
338
370
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).
339
371
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.
341
373
342
374
The following sample code sets the tier to **hot** for a single, named blob within the `archive` container.
343
375
@@ -363,7 +395,7 @@ Blob index tags make data management and discovery easier. Blob index tags are u
363
395
364
396
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.
365
397
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.
367
399
368
400
```xml
369
401
<VenueName="House of Prime Rib"Type="Restaurant">
@@ -476,7 +508,7 @@ az storage blob delete-batch \
476
508
--auth-mode login
477
509
```
478
510
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.
480
512
481
513
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.
482
514
@@ -510,11 +542,11 @@ az storage blob list \
510
542
## Restore a deleted blob
511
543
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.
512
544
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.
514
546
515
547
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.
516
548
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 uniquelynamed 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.
518
550
519
551
If versioning is disabled, the `az storage blob undelete` command is used to restore each soft-deleted blob in the container.
0 commit comments