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
@@ -18,7 +18,7 @@ Azure blob storage allows you to store large amounts of unstructured object data
18
18
19
19
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.
20
20
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.
22
22
23
23
## Prerequisites
24
24
@@ -48,29 +48,32 @@ To use this example, supply values for the variables and ensure that you've logg
48
48
49
49
```azurecli
50
50
#!/bin/bash
51
-
export AZURE_STORAGE_ACCOUNT="<storage-account>"
51
+
storageAccount="<storage-account>"
52
52
containerName="demo-container-1"
53
53
containerPrefix="demo-container-"
54
54
55
55
# Approach 1: Create a container
56
56
az storage container create \
57
57
--name $containerName \
58
+
--account-name $storageAccount \
58
59
--auth-mode login
59
60
60
61
# Approach 2: Create containers with a loop
61
-
for value in {2..4}
62
+
for value in {2..5}
62
63
do
63
64
az storage container create \
64
65
--name $containerPrefix$value \
66
+
--account-name $storageAccount \
65
67
--auth-mode login
66
68
done
67
69
68
70
# Approach 3: Create containers by splitting multiple values
@@ -89,25 +92,28 @@ Read more about the [az storage container list](/cli/azure/storage/container#az_
89
92
90
93
```azurecli-interactive
91
94
#!/bin/bash
92
-
export AZURE_STORAGE_ACCOUNT="<storage-account>"
93
-
numResults="3"
95
+
storageAccount="<storage-account>"
94
96
containerPrefix="demo-container-"
95
97
containerName="demo-container-1"
98
+
numResults="3"
96
99
97
100
# Approach 1: List maximum containers
98
101
az storage container list \
102
+
--account-name $storageAccount \
99
103
--auth-mode login
100
104
101
105
# Approach 2: List a defined number of named containers
102
106
az storage container list \
103
107
--prefix $containerPrefix \
104
108
--num-results $numResults \
109
+
--account-name $storageAccount \
105
110
--auth-mode login
106
111
107
112
# Approach 3: List an individual container
108
113
az storage container list \
109
114
--prefix $containerPrefix \
110
115
--query "[?name=='$containerName']" \
116
+
--account-name $storageAccount \
111
117
--auth-mode login
112
118
```
113
119
@@ -125,53 +131,88 @@ In the following example, the first approach displays the properties of a single
125
131
126
132
```azurecli-interactive
127
133
#!/bin/bash
128
-
export AZURE_STORAGE_ACCOUNT="<storage-account>"
134
+
storageAccount="<storage-account>"
129
135
containerPrefix="demo-container-"
130
136
containerName="demo-container-1"
131
137
132
138
# Show a named container's properties
133
139
az storage container show \
134
140
--name $containerName \
141
+
--account-name $storageAccount \
135
142
--auth-mode login
136
143
137
144
# List several containers and show their properties
138
145
containerList=$(az storage container list \
139
146
--query "[].name" \
140
147
--prefix $containerPrefix \
148
+
--account-name $storageAccount \
141
149
--auth-mode login \
142
150
--output tsv)
143
-
for item in $containerList
151
+
152
+
for row in $containerList
144
153
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
148
156
done
149
157
```
150
158
151
159
### Read and write container metadata
152
160
153
161
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.
154
162
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.
156
164
157
165
```azurecli-interactive
158
166
#!/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-"
161
170
162
171
# Create metadata string
163
172
metadata="key=value pie=delicious"
164
173
165
-
# Update metadata
174
+
# Update named container metadata
166
175
az storage container metadata update \
167
176
--name $containerName \
168
177
--metadata $metadata \
178
+
--account-name $storageAccount \
169
179
--auth-mode login
170
180
171
181
# Display metadata
172
182
az storage container metadata show \
173
183
--name $containerName \
184
+
--account-name $storageAccount \
174
185
--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
175
216
```
176
217
177
218
## Delete containers
@@ -183,65 +224,95 @@ Depending on your use case, you can delete a single container or a group of cont
183
224
184
225
```azurecli-interactive
185
226
#!/bin/bash
186
-
export AZURE_STORAGE_ACCOUNT="<storage-account>"
227
+
storageAccount="<storage-account>"
187
228
containerName="demo-container-1"
188
229
containerPrefix="demo-container-"
189
230
190
231
# Delete a single named container
191
232
az storage container delete \
192
233
--name $containerName \
234
+
--account-name $storageAccount \
193
235
--auth-mode login
194
236
195
237
# Delete containers by iterating a loop
196
238
list=$(az storage container list \
197
239
--query "[].name" \
198
-
--auth-mode login \
199
240
--prefix $containerPrefix \
241
+
--account-name $storageAccount \
242
+
--auth-mode login \
200
243
--output tsv)
201
-
for item in $list
244
+
for row in $list
202
245
do
246
+
tmpName=$(echo $row | sed -e 's/\r//g')
203
247
az storage container delete \
204
-
--name $item \
248
+
--name $tmpName \
249
+
--account-name $storageAccount \
205
250
--auth-mode login
206
251
done
207
252
```
208
253
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.
210
255
211
256
Use the following example to retrieve a list of containers deleted within the storage account's associated retention period.
212
257
213
258
```azurecli-interactive
259
+
#!/bin/bash
260
+
storageAccount="<storage-account>"
261
+
containerPrefix="demo-container-"
262
+
214
263
# Retrieve a list of containers including those recently deleted
215
264
az storage container list \
216
-
--prefix $prefix \
265
+
--prefix $containerPrefix \
217
266
--include-deleted \
267
+
--account-name $storageAccount\
218
268
--auth-mode login
219
269
```
220
270
221
271
## Restore a soft-deleted container
222
272
223
273
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.
224
274
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.
226
276
227
277
To learn more about the soft delete data protection option, refer to the [Soft delete for containers](soft-delete-container-overview.md) article.
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
245
316
```
246
317
247
318
## Get a shared access signature for a container
@@ -255,14 +326,15 @@ Azure Storage supports three types of shared access signatures: user delegation,
255
326
> [!CAUTION]
256
327
> 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.
257
328
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.
259
330
260
331
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.
261
332
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.
0 commit comments