Skip to content

Commit 519d048

Browse files
authored
Merge pull request #187636 from stevenmatthew/containerCLI
Container CLI
2 parents 682bd69 + 2e520ea commit 519d048

File tree

1 file changed

+102
-38
lines changed

1 file changed

+102
-38
lines changed

articles/storage/blobs/blob-containers-cli.md

Lines changed: 102 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author: stevenmatthew
77

88
ms.service: storage
99
ms.topic: how-to
10-
ms.date: 01/19/2022
10+
ms.date: 02/05/2022
1111
ms.author: shaas
1212
ms.subservice: blobs
1313
---
@@ -18,7 +18,7 @@ Azure blob storage allows you to store large amounts of unstructured object data
1818

1919
The Azure CLI is Azure's cross-platform command-line experience for managing Azure resources. You can use it in your browser with Azure Cloud Shell. You can also install it on macOS, Linux, or Windows and run it locally from the command line.
2020

21-
In this how-to article, you learn to use the Azure CLI to work with container objects.
21+
In this how-to article, you learn to use the Azure CLI with Bash to work with container objects.
2222

2323
## Prerequisites
2424

@@ -48,29 +48,32 @@ To use this example, supply values for the variables and ensure that you've logg
4848

4949
```azurecli
5050
#!/bin/bash
51-
export AZURE_STORAGE_ACCOUNT="<storage-account>"
51+
storageAccount="<storage-account>"
5252
containerName="demo-container-1"
5353
containerPrefix="demo-container-"
5454
5555
# Approach 1: Create a container
5656
az storage container create \
5757
--name $containerName \
58+
--account-name $storageAccount \
5859
--auth-mode login
5960
6061
# Approach 2: Create containers with a loop
61-
for value in {2..4}
62+
for value in {2..5}
6263
do
6364
az storage container create \
6465
--name $containerPrefix$value \
66+
--account-name $storageAccount \
6567
--auth-mode login
6668
done
6769
6870
# Approach 3: Create containers by splitting multiple values
69-
containerList="${containerPrefix}5 ${containerPrefix}6 ${containerPrefix}7"
71+
containerList="${containerPrefix}6 ${containerPrefix}7 ${containerPrefix}8"
7072
for container in $containerList
7173
do
7274
az storage container create \
7375
--name $container \
76+
--account-name $storageAccount \
7477
--auth-mode login
7578
done
7679
```
@@ -89,25 +92,28 @@ Read more about the [az storage container list](/cli/azure/storage/container#az_
8992

9093
```azurecli-interactive
9194
#!/bin/bash
92-
export AZURE_STORAGE_ACCOUNT="<storage-account>"
93-
numResults="3"
95+
storageAccount="<storage-account>"
9496
containerPrefix="demo-container-"
9597
containerName="demo-container-1"
98+
numResults="3"
9699
97100
# Approach 1: List maximum containers
98101
az storage container list \
102+
--account-name $storageAccount \
99103
--auth-mode login
100104
101105
# Approach 2: List a defined number of named containers
102106
az storage container list \
103107
--prefix $containerPrefix \
104108
--num-results $numResults \
109+
--account-name $storageAccount \
105110
--auth-mode login
106111
107112
# Approach 3: List an individual container
108113
az storage container list \
109114
--prefix $containerPrefix \
110115
--query "[?name=='$containerName']" \
116+
--account-name $storageAccount \
111117
--auth-mode login
112118
```
113119

@@ -125,53 +131,88 @@ In the following example, the first approach displays the properties of a single
125131

126132
```azurecli-interactive
127133
#!/bin/bash
128-
export AZURE_STORAGE_ACCOUNT="<storage-account>"
134+
storageAccount="<storage-account>"
129135
containerPrefix="demo-container-"
130136
containerName="demo-container-1"
131137
132138
# Show a named container's properties
133139
az storage container show \
134140
--name $containerName \
141+
--account-name $storageAccount \
135142
--auth-mode login
136143
137144
# List several containers and show their properties
138145
containerList=$(az storage container list \
139146
--query "[].name" \
140147
--prefix $containerPrefix \
148+
--account-name $storageAccount \
141149
--auth-mode login \
142150
--output tsv)
143-
for item in $containerList
151+
152+
for row in $containerList
144153
do
145-
az storage container show \
146-
--name $item \
147-
--auth-mode login
154+
tmpRow=$(echo $row | sed -e 's/\r//g')
155+
az storage container show --name $tmpRow --account-name $storageAccount --auth-mode login
148156
done
149157
```
150158

151159
### Read and write container metadata
152160

153161
Users that have many thousands of objects within their storage account can quickly locate specific containers based on their metadata. To read the metadata, you'll use the `az storage container metadata show` command. To update metadata, you'll need to call the `az storage container metadata update` command. The method only accepts space-separated key-value pairs. For more information, see the [az storage container metadata](/cli/azure/storage/container/metadata) documentation.
154162

155-
The example below first updates a container's metadata and afterward retrieves the container's metadata.
163+
The first example below updates and then retrieves a named container's metadata. The second example iterates the list of containers matching the `-prefix` value. Containers with names containing even numbers have their metadata set with values contained in the *metadata* variable.
156164

157165
```azurecli-interactive
158166
#!/bin/bash
159-
export AZURE_STORAGE_ACCOUNT="<storage-account>"
160-
containerName = "demo-container-1"
167+
storageAccount="<storage-account>"
168+
containerName="demo-container-1"
169+
containerPrefix="demo-container-"
161170
162171
# Create metadata string
163172
metadata="key=value pie=delicious"
164173
165-
# Update metadata
174+
# Update named container metadata
166175
az storage container metadata update \
167176
--name $containerName \
168177
--metadata $metadata \
178+
--account-name $storageAccount \
169179
--auth-mode login
170180
171181
# Display metadata
172182
az storage container metadata show \
173183
--name $containerName \
184+
--account-name $storageAccount \
174185
--auth-mode login
186+
187+
# Get list of containers
188+
containerList=$(az storage container list \
189+
--query "[].name" \
190+
--prefix $containerPrefix \
191+
--account-name $storageAccount \
192+
--auth-mode login \
193+
--output tsv)
194+
195+
# Update and display metadata
196+
for row in $containerList
197+
do
198+
#Get the container's number
199+
tmpName=$(echo $row | sed -e 's/\r//g')
200+
if [ `expr ${tmpName: ${#containerPrefix}} % 2` == 0 ]
201+
then
202+
az storage container metadata update \
203+
--name $tmpName \
204+
--metadata $metadata \
205+
--account-name $storageAccount \
206+
--auth-mode login
207+
208+
echo $tmpName
209+
210+
az storage container metadata show \
211+
--name $tmpName \
212+
--account-name $storageAccount \
213+
--auth-mode login
214+
fi
215+
done
175216
```
176217

177218
## Delete containers
@@ -183,65 +224,95 @@ Depending on your use case, you can delete a single container or a group of cont
183224
184225
```azurecli-interactive
185226
#!/bin/bash
186-
export AZURE_STORAGE_ACCOUNT="<storage-account>"
227+
storageAccount="<storage-account>"
187228
containerName="demo-container-1"
188229
containerPrefix="demo-container-"
189230
190231
# Delete a single named container
191232
az storage container delete \
192233
--name $containerName \
234+
--account-name $storageAccount \
193235
--auth-mode login
194236
195237
# Delete containers by iterating a loop
196238
list=$(az storage container list \
197239
--query "[].name" \
198-
--auth-mode login \
199240
--prefix $containerPrefix \
241+
--account-name $storageAccount \
242+
--auth-mode login \
200243
--output tsv)
201-
for item in $list
244+
for row in $list
202245
do
246+
tmpName=$(echo $row | sed -e 's/\r//g')
203247
az storage container delete \
204-
--name $item \
248+
--name $tmpName \
249+
--account-name $storageAccount \
205250
--auth-mode login
206251
done
207252
```
208253

209-
If you have container soft delete enabled for your storage account, then it's possible to retrieve containers that have been deleted. If your storage account's soft delete data protection option is enabled, the `--include-deleted` parameter will return containers deleted within the associated retention period. The `--include-deleted` parameter can only be used in conjunction with the `--prefix` parameter when returning a list of containers. To learn more about soft delete, refer to the [Soft delete for containers](soft-delete-container-overview.md) article.
254+
If you have container soft delete enabled for your storage account, then it's possible to retrieve containers that have been deleted. If your storage account's soft delete data protection option is enabled, the `--include-deleted` parameter will return containers deleted within the associated retention period. The `--include-deleted` parameter can only be used to return containers when used with the `--prefix` parameter. To learn more about soft delete, refer to the [Soft delete for containers](soft-delete-container-overview.md) article.
210255

211256
Use the following example to retrieve a list of containers deleted within the storage account's associated retention period.
212257

213258
```azurecli-interactive
259+
#!/bin/bash
260+
storageAccount="<storage-account>"
261+
containerPrefix="demo-container-"
262+
214263
# Retrieve a list of containers including those recently deleted
215264
az storage container list \
216-
--prefix $prefix \
265+
--prefix $containerPrefix \
217266
--include-deleted \
267+
--account-name $storageAccount\
218268
--auth-mode login
219269
```
220270

221271
## Restore a soft-deleted container
222272

223273
As mentioned in the [List containers](#list-containers) 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. Before you can follow this example, you'll need to enable soft delete and configure it on at least one of your storage accounts.
224274

225-
The following example explains how to restore a soft-deleted container with the `az storage container restore` command. You'll need to supply values for the `--name` and `--version` parameters to ensure that the correct version of the container is restored. If you don't know the version number, you can use the `az storage container list` command to retrieve it as shown in the following example.
275+
The following examples explain how to restore a soft-deleted container with the `az storage container restore` command. You'll need to supply values for the `--name` and `--version` parameters to ensure that the correct version of the container is restored. If you don't know the version number, you can use the `az storage container list` command to retrieve it as shown in the first example. The second example finds and restores all deleted containers within a specific storage account.
226276

227277
To learn more about the soft delete data protection option, refer to the [Soft delete for containers](soft-delete-container-overview.md) article.
228278

229279
```azurecli-interactive
230280
#!/bin/bash
231-
export AZURE_STORAGE_ACCOUNT="<storage-account>"
281+
storageAccount="<storage-account>"
232282
containerName="demo-container-1"
233283
234284
# Restore an individual named container
235285
containerVersion=$(az storage container list \
286+
--account-name $storageAccount \
236287
--query "[?name=='$containerName'].[version]" \
237288
--auth-mode login \
238289
--output tsv \
239-
--include-deleted)
290+
--include-deleted | sed -e 's/\r//g')
240291
241292
az storage container restore \
242293
--name $containerName \
243294
--deleted-version $containerVersion \
295+
--account-name $storageAccount \
244296
--auth-mode login
297+
298+
# Restore a list of deleted containers
299+
containerList=$(az storage container list \
300+
--account-name $storageAccount \
301+
--include-deleted \
302+
--auth-mode login \
303+
--query "[?deleted].{name:name,version:version}" \
304+
-o json)
305+
306+
for row in $(echo "${containerList}" | jq -c '.[]' )
307+
do
308+
tmpName=$(echo $row | jq -r '.name')
309+
tmpVersion=$(echo $row | jq -r '.version')
310+
az storage container restore \
311+
--account-name $storageAccount \
312+
--name $tmpName \
313+
--deleted-version $tmpVersion \
314+
--auth-mode login
315+
done
245316
```
246317

247318
## Get a shared access signature for a container
@@ -255,14 +326,15 @@ Azure Storage supports three types of shared access signatures: user delegation,
255326
> [!CAUTION]
256327
> Any client that possesses a valid SAS can access data in your storage account as permitted by that SAS. It's important to protect a SAS from malicious or unintended use. Use discretion in distributing a SAS, and have a plan in place for revoking a compromised SAS.
257328
258-
The following example illustrates the process of configuring a service SAS for a specific container using the `az storage container generate-sas` command. Because it is generating a service SAS, the example first retrieves the storage account key to pass as the `--account-key` value.
329+
The following example illustrates the process of configuring a service SAS for a specific container using the `az storage container generate-sas` command. Because it's generating a service SAS, the example first retrieves the storage account key to pass as the `--account-key` value.
259330

260331
The example will configure the SAS with start and expiry times and a protocol. It will also specify the **delete**, **read**, **write**, and **list** permissions in the SAS using the `--permissions` parameter. You can reference the full table of permissions in the [Create a service SAS](/rest/api/storageservices/create-service-sas) article.
261332

333+
Copy and paste the Blob SAS token value in a secure location. It will only be displayed once and can’t be retrieved once Bash is closed. To construct the SAS URL, append the SAS token (URI) to the URL for the storage service.
334+
262335
```azurecli-interactive
263336
#!/bin/bash
264-
storageAccount="<storage-account-name>"
265-
export AZURE_STORAGE_ACCOUNT=$storageAccount
337+
storageAccount="<storage-account>"
266338
containerName="demo-container-1"
267339
permissions="drwl"
268340
expiry=`date -u -d "30 minutes" '+%Y-%m-%dT%H:%MZ'`
@@ -279,16 +351,8 @@ az storage container generate-sas \
279351
--https-only \
280352
--permissions dlrw \
281353
--expiry $expiry \
282-
--account-key $accountKey
283-
```
284-
285-
## Clean up resources
286-
287-
If you want to delete the environment variables as part of this how-to article, run the following script.
288-
289-
```azurecli
290-
# Remove environment variables
291-
unset AZURE_STORAGE_ACCOUNT
354+
--account-key $accountKey \
355+
--account-name $storageAccount
292356
```
293357

294358
## Next steps

0 commit comments

Comments
 (0)