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
+47-41Lines changed: 47 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
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
@@ -102,65 +102,71 @@ 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
107
Use the `--prefix` parameter to select either a single known file or a range of files whose names begin with a defined string.
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 more than multiple object types.
110
110
111
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).
112
112
113
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.
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
120
containerName="demo-container"
123
121
blobPrefix="img-louis"
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
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
142
+
143
+
#Approach 3: Use the continuation token to return the next set of blobs.
144
+
145
+
$resultSet=$(az storage blob list `
146
+
--account-name $storageAccount `
147
+
--container $containerName `
148
+
--prefix $blobPrefix `
149
+
--num-results $numResults `
150
+
--show-next-marker `
151
+
--query "[].{Name:name, Marker:nextMarker}" `
152
+
--auth-mode login `
153
+
--output tsv)
154
+
155
+
$markerEntry=$resultSet[$numResults] | cut -f2
156
+
$marker= echo $markerEntry | cut -f2
132
157
133
-
#Approach 2: Use the --prefix parameter to list blobs in all containers
134
158
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
152
-
done
153
159
```
154
160
155
161
## Download a blob
156
162
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.
163
+
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
164
159
165
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
166
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.
167
+
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
168
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.
169
+
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
170
165
171
```azurecli-interactive
166
172
#!/bin/bash
@@ -226,7 +232,7 @@ User-defined metadata consists of one or more name-value pairs that you specify
226
232
227
233
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
234
229
-
For additional information, see the [az storage blob show](/cli/azure/storage/blob#az-storage-blob-show) reference.
235
+
For more information, see the [az storage blob show](/cli/azure/storage/blob#az-storage-blob-show) reference.
230
236
231
237
```azurecli-interactive
232
238
#!/bin/bash
@@ -245,7 +251,7 @@ az storage blob show \
245
251
246
252
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
253
248
-
For additional information, see the [az storage blob metadata](/cli/azure/storage/blob#az-storage-blob-metadata) reference.
254
+
For more information, see the [az storage blob metadata](/cli/azure/storage/blob#az-storage-blob-metadata) reference.
249
255
250
256
The example below first updates and then commits a blob's metadata, and then retrieves it.
251
257
@@ -278,7 +284,7 @@ az storage blob metadata show \
278
284
279
285
## Copy operations for blobs
280
286
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.
287
+
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
288
283
289
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
290
@@ -291,7 +297,7 @@ You can use the `az storage blob copy start-batch` command to recursively copy m
291
297
292
298
For more information, see the [az storage blob copy](/cli/azure/storage/blob/copy) reference.
293
299
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.
300
+
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
301
296
302
```azurecli-interactive
297
303
#!/bin/bash
@@ -333,11 +339,11 @@ az storage blob snapshot \
333
339
334
340
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
341
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.
342
+
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
343
338
344
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
345
340
-
For additional information, see the [az storage blob set-tier](/cli/azure/storage/blob#az-storage-blob-set-tier) reference.
346
+
For more information, see the [az storage blob set-tier](/cli/azure/storage/blob#az-storage-blob-set-tier) reference.
341
347
342
348
The following sample code sets the tier to **hot** for a single, named blob within the `archive` container.
343
349
@@ -363,7 +369,7 @@ Blob index tags make data management and discovery easier. Blob index tags are u
363
369
364
370
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
371
366
-
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.
367
373
368
374
```xml
369
375
<VenueName="House of Prime Rib"Type="Restaurant">
@@ -476,7 +482,7 @@ az storage blob delete-batch \
476
482
--auth-mode login
477
483
```
478
484
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.
485
+
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
486
481
487
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
488
@@ -510,11 +516,11 @@ az storage blob list \
510
516
## Restore a deleted blob
511
517
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
518
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.
519
+
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
520
515
521
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
522
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.
523
+
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
524
519
525
If versioning is disabled, the `az storage blob undelete` command is used to restore each soft-deleted blob in the container.
0 commit comments