|
| 1 | +--- |
| 2 | +title: Use Azure CLI for files & ACLs in Azure Data Lake Storage Gen2 (preview) |
| 3 | +description: Use the Azure CLI to manage directories and file and directory access control lists (ACL) in storage accounts that have a hierarchical namespace. |
| 4 | +services: storage |
| 5 | +author: normesta |
| 6 | +ms.service: storage |
| 7 | +ms.subservice: data-lake-storage-gen2 |
| 8 | +ms.topic: conceptual |
| 9 | +ms.date: 11/24/2019 |
| 10 | +ms.author: normesta |
| 11 | +ms.reviewer: prishet |
| 12 | +--- |
| 13 | + |
| 14 | +# Use Azure CLI for files & ACLs in Azure Data Lake Storage Gen2 (preview) |
| 15 | + |
| 16 | +This article shows you how to use the [Azure Command-Line Interface (CLI)](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest) to create and manage directories, files, and permissions in storage accounts that have a hierarchical namespace. |
| 17 | + |
| 18 | +> [!IMPORTANT] |
| 19 | +> The the `storage-preview` extension that is featured in this article is currently in public preview. |
| 20 | +
|
| 21 | +[Sample](https://github.com/Azure/azure-cli-extensions/tree/master/src/storage-preview#adls-gen2-support) | [Gen1 to Gen2 mapping](https://github.com/Azure/azure-cli-extensions/tree/master/src/storage-preview#mapping-from-adls-gen1-to-adls-gen2) | [Give feedback](https://github.com/Azure/azure-cli-extensions/issues) |
| 22 | +## Prerequisites |
| 23 | + |
| 24 | +> [!div class="checklist"] |
| 25 | +> * An Azure subscription. See [Get Azure free trial](https://azure.microsoft.com/pricing/free-trial/). |
| 26 | +> * A storage account that has hierarchical namespace (HNS) enabled. Follow [these](data-lake-storage-quickstart-create-account.md) instructions to create one. |
| 27 | +> * Azure CLI version `2.0.67` or higher. |
| 28 | +
|
| 29 | +## Install the storage CLI extension |
| 30 | + |
| 31 | +1. Open the [Azure Cloud Shell](https://docs.microsoft.com/azure/cloud-shell/overview?view=azure-cli-latest), or if you've [installed](https://docs.microsoft.com/cli/azure/install-azure-cli?view=azure-cli-latest) the Azure CLI locally, open a command console application such as Windows PowerShell. |
| 32 | + |
| 33 | +2. Verify that the version of Azure CLI that have installed is `2.0.67` or higher by using the following command. |
| 34 | + |
| 35 | + ```azurecli |
| 36 | + az --version |
| 37 | + ``` |
| 38 | + If your version of Azure CLI is lower than `2.0.67`, then install a later version. See [Install the Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli?view=azure-cli-latest). |
| 39 | + |
| 40 | +3. Install the `storage-preview` extension. |
| 41 | + |
| 42 | + ```azurecli |
| 43 | + az extension add -n storage-preview |
| 44 | + ``` |
| 45 | + |
| 46 | +## Connect to the account |
| 47 | + |
| 48 | +1. If you're using Azure CLI locally, run the login command. |
| 49 | + |
| 50 | + ```azurecli |
| 51 | + az login |
| 52 | + ``` |
| 53 | + |
| 54 | + If the CLI can open your default browser, it will do so and load an Azure sign-in page. |
| 55 | + |
| 56 | + Otherwise, open a browser page at [https://aka.ms/devicelogin](https://aka.ms/devicelogin) and enter the authorization code displayed in your terminal. Then, sign in with your account credentials in the browser. |
| 57 | + |
| 58 | + To learn more about different authentication methods, see Sign in with Azure CLI. |
| 59 | + |
| 60 | +2. If your identity is associated with more than one subscription, then set your active subscription to subscription of the storage account that will host your static website. |
| 61 | + |
| 62 | + ```azurecli |
| 63 | + az account set --subscription <subscription-id> |
| 64 | + ``` |
| 65 | + |
| 66 | + Replace the `<subscription-id>` placeholder value with the ID of your subscription. |
| 67 | + |
| 68 | +## Create a file system |
| 69 | + |
| 70 | +A file system acts as a container for your files. You can create one by using the `az storage container create` command. |
| 71 | + |
| 72 | +This example creates a file system named `my-file-system`. |
| 73 | + |
| 74 | +```azurecli |
| 75 | +az storage container create --name my-file-system |
| 76 | +``` |
| 77 | + |
| 78 | +## Create a directory |
| 79 | + |
| 80 | +Create a directory reference by using the `az storage blob directory create` command. |
| 81 | + |
| 82 | +This example adds a directory named `my-directory` to a file system named `my-file-system` that is located in an account named `mystorageaccount`. |
| 83 | + |
| 84 | +```azurecli |
| 85 | +az storage blob directory create -c my-file-system -d my-directory --account-name mystorageaccount |
| 86 | +``` |
| 87 | + |
| 88 | +## Show directory properties |
| 89 | + |
| 90 | +You can print the properties of a directory to the console by using the `az storage blob show` command. |
| 91 | + |
| 92 | +```azurecli |
| 93 | +az storage blob directory show -c my-file-system -d my-directory --account-name mystorageaccount |
| 94 | +``` |
| 95 | + |
| 96 | +## Rename or move a directory |
| 97 | + |
| 98 | +Rename or move a directory by using the `az storage blob directory move` command. |
| 99 | + |
| 100 | +This example renames a directory from the name `my-directory` to the name `my-new-directory`. |
| 101 | + |
| 102 | +```azurecli |
| 103 | +az storage blob directory move -c my-file-system -d my-new-directory -s my-directory --account-name mystorageaccount |
| 104 | +``` |
| 105 | + |
| 106 | +## Delete a directory |
| 107 | + |
| 108 | +Delete a directory by using the `az storage blob directory delete` command. |
| 109 | + |
| 110 | +This example deletes a directory named `my-directory`. |
| 111 | + |
| 112 | +```azurecli |
| 113 | +az storage blob directory delete -c my-file-system -d my-directory --account-name mystorageaccount |
| 114 | +``` |
| 115 | + |
| 116 | +## Check if a directory exists |
| 117 | + |
| 118 | +Determine if a specific directory exists in the file system by using the `az storage blob directory exist` command. |
| 119 | + |
| 120 | +This example reveals whether a directory named `my-directory` exists in the `my-file-system` file system. |
| 121 | + |
| 122 | +```azurecli |
| 123 | +az storage blob directory exists -c my-file-system -d my-directory --account-name mystorageaccount |
| 124 | +``` |
| 125 | + |
| 126 | +## Download from a directory |
| 127 | + |
| 128 | +Download a file from a directory by using the `az storage blob directory download` command. |
| 129 | + |
| 130 | +This example downloads a file named `upload.txt` from a directory named `my-directory`. |
| 131 | + |
| 132 | +```azurecli |
| 133 | +az storage blob directory download -c my-file-system --account-name mystorageaccount -s "my-directory/upload.txt" -d "C:\mylocalfolder\download.txt" |
| 134 | +``` |
| 135 | + |
| 136 | +This example downloads an entire directory. |
| 137 | + |
| 138 | +```azurecli |
| 139 | +az storage blob directory download -c my-file-system --account-name mystorageaccount -s "my-directory/" -d "C:\mylocalfolder" --recursive |
| 140 | +``` |
| 141 | + |
| 142 | +## List directory contents |
| 143 | + |
| 144 | +List the contents of a directory by using the `az storage blob directory list` command. |
| 145 | + |
| 146 | +This example lists the contents of a directory named `my-directory` that is located in the `my-file-system` file system of a storage account named `mystorageaccount`. |
| 147 | + |
| 148 | +```azurecli |
| 149 | +az storage blob directory list -c my-file-system -d my-directory --account-name mystorageaccount |
| 150 | +``` |
| 151 | + |
| 152 | +## Upload a file to a directory |
| 153 | + |
| 154 | +Upload a file to a directory by using the `az storage blob directory upload` command. |
| 155 | + |
| 156 | +This example uploads a file named `upload.txt` to a directory named `my-directory`. |
| 157 | + |
| 158 | +```azurecli |
| 159 | +az storage blob directory upload -c my-file-system --account-name mystorageaccount -s "C:\mylocaldirectory\upload.txt" -d my-directory |
| 160 | +``` |
| 161 | + |
| 162 | +This example uploads an entire directory. |
| 163 | + |
| 164 | +```azurecli |
| 165 | +az storage blob directory upload -c my-file-system --account-name mystorageaccount -s "C:\mylocaldirectory\" -d my-directory --recursive |
| 166 | +``` |
| 167 | + |
| 168 | +## Show file properties |
| 169 | + |
| 170 | +You can print the properties of a file to the console by using the `az storage blob show` command. |
| 171 | + |
| 172 | +```azurecli |
| 173 | +az storage blob show -c my-file-system -b my-file.txt --account-name mystorageaccount |
| 174 | +``` |
| 175 | + |
| 176 | +## Rename or move a file |
| 177 | + |
| 178 | +Rename or move a file by using the `az storage blob move` command. |
| 179 | + |
| 180 | +This example renames a file from the name `my-file.txt` to the name `my-file-renamed.txt`. |
| 181 | + |
| 182 | +```azurecli |
| 183 | +az storage blob move -c my-file-system -d my-file-renamed.txt -s my-file.txt --account-name mystorageaccount |
| 184 | +``` |
| 185 | + |
| 186 | +## Delete a file |
| 187 | + |
| 188 | +Delete a file by using the `az storage blob delete` command. |
| 189 | + |
| 190 | +This example deletes a file named `my-file.txt` |
| 191 | + |
| 192 | +```azurecli |
| 193 | +az storage blob delete -c my-file-system -b my-file.txt --account-name mystorageaccount |
| 194 | +``` |
| 195 | + |
| 196 | +## Manage permissions |
| 197 | + |
| 198 | +You can get, set, and update access permissions of directories and files. |
| 199 | + |
| 200 | +### Get directory and file permissions |
| 201 | + |
| 202 | +Get the ACL of a **directory** by using the `az storage blob directory access show` command. |
| 203 | + |
| 204 | +This example gets the ACL of a directory, and then prints the ACL to the console. |
| 205 | + |
| 206 | +```azurecli |
| 207 | +az storage blob directory access show -d my-directory -c my-file-system --account-name mystorageaccount |
| 208 | +``` |
| 209 | + |
| 210 | +Get the access permissions of a **file** by using the `az storage blob access show` command. |
| 211 | + |
| 212 | +This example gets the ACL of a file and then prints the ACL to the console. |
| 213 | + |
| 214 | +```azurecli |
| 215 | +az storage blob access show -b my-directory/upload.txt -c my-file-system --account-name mystorageaccount |
| 216 | +``` |
| 217 | + |
| 218 | +The following image shows the output after getting the ACL of a directory. |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | +In this example, the owning user has read, write, and execute permissions. The owning group has only read and execute permissions. For more information about access control lists, see [Access control in Azure Data Lake Storage Gen2](data-lake-storage-access-control.md). |
| 223 | + |
| 224 | +### Set directory and file permissions |
| 225 | + |
| 226 | +Use the `az storage blob directory access set` command to set the ACL of a **directory**. |
| 227 | + |
| 228 | +This example sets the ACL on a directory for the owning user, owning group, or other users, and then prints the ACL to the console. |
| 229 | + |
| 230 | +```azurecli |
| 231 | +az storage blob directory access set -a "user::rw-,group::rw-,other::-wx" -d my-directory -c my-file-system --account-name mystorageaccount |
| 232 | +``` |
| 233 | + |
| 234 | +Use the `az storage blob access set` command to set the acl of a **file**. |
| 235 | + |
| 236 | +This example sets the ACL on a file for the owning user, owning group, or other users, and then prints the ACL to the console. |
| 237 | + |
| 238 | +```azurecli |
| 239 | +az storage blob access set -a "user::rw-,group::rw-,other::-wx" -b my-directory/upload.txt -c my-file-system --account-name mystorageaccount |
| 240 | +``` |
| 241 | +The following image shows the output after setting the ACL of a file. |
| 242 | + |
| 243 | + |
| 244 | + |
| 245 | +In this example, the owning user and owning group have only read and write permissions. All other users have write and execute permissions. For more information about access control lists, see [Access control in Azure Data Lake Storage Gen2](data-lake-storage-access-control.md). |
| 246 | + |
| 247 | +### Update directory and file permissions |
| 248 | + |
| 249 | +Another way to set this permission is to use the `az storage blob directory access update` or `az storage blob access update` command. |
| 250 | + |
| 251 | +Update the ACL of a directory or file by setting the `-permissions` parameter to the short form of an ACL. |
| 252 | + |
| 253 | +This example updates the ACL of a **directory**. |
| 254 | + |
| 255 | +```azurecli |
| 256 | +az storage blob directory access update --permissions "rwxrwxrwx" -d my-directory -c my-file-system --account-name mystorageaccount |
| 257 | +``` |
| 258 | + |
| 259 | +This example updates the ACL of a **file**. |
| 260 | + |
| 261 | +```azurecli |
| 262 | +az storage blob access update --permissions "rwxrwxrwx" -b my-directory/upload.txt -c my-file-system --account-name mystorageaccount |
| 263 | +``` |
| 264 | + |
| 265 | +You can also update the owning user and group of a directory or file by setting the `--owner` or `group` parameters to the entity ID or User Principal Name (UPN) of a user. |
| 266 | + |
| 267 | +This example changes the owner of a directory. |
| 268 | + |
| 269 | +```azurecli |
| 270 | +az storage blob directory access update --owner xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx -d my-directory -c my-file-system --account-name mystorageaccount |
| 271 | +``` |
| 272 | + |
| 273 | +This example changes the owner of a file. |
| 274 | + |
| 275 | +```azurecli |
| 276 | +az storage blob access update --owner xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx -b my-directory/upload.txt -c my-file-system --account-name mystorageaccount |
| 277 | +``` |
| 278 | +## Manage user-defined metadata |
| 279 | + |
| 280 | +You can add user-defined metadata to a file or directory by using the `az storage blob directory metadata update` command with one or more name-value pairs. |
| 281 | + |
| 282 | +This example adds user-defined metadata for a directory named `my-directory` directory. |
| 283 | + |
| 284 | +```azurecli |
| 285 | +az storage blob directory metadata update --metadata tag1=value1 tag2=value2 -c my-file-system -d my-directory --account-name mystorageaccount |
| 286 | +``` |
| 287 | + |
| 288 | +This example shows all user-defined metadata for directory named `my-directory`. |
| 289 | + |
| 290 | +```azurecli |
| 291 | +az storage blob directory metadata show -c my-file-system -d my-directory --account-name mystorageaccount |
| 292 | +``` |
| 293 | + |
| 294 | +## See also |
| 295 | + |
| 296 | +* [Sample](https://github.com/Azure/azure-cli-extensions/tree/master/src/storage-preview) |
| 297 | +* [Gen1 to Gen2 mapping](https://github.com/Azure/azure-cli-extensions/tree/master/src/storage-preview#mapping-from-adls-gen1-to-adls-gen2) |
| 298 | +* [Give feedback](https://github.com/Azure/azure-cli-extensions/issues) |
| 299 | +* [Known capability gaps](data-lake-storage-known-issues.md#api-scope-data-lake-client-library) |
| 300 | +* [Source code](https://github.com/Azure/azure-cli-extensions/tree/master/src) |
| 301 | + |
0 commit comments