PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/articles/storage/storage-azure-cli.md

https://gitlab.com/yeah568/azure-content
Markdown | 222 lines | 124 code | 98 blank | 0 comment | 0 complexity | 96ac6b71a7fe4e1c6a2f6976c67d67da MD5 | raw file
  1. <properties
  2. pageTitle="Using the Azure CLI with Azure Storage | Microsoft Azure"
  3. description="Learn how to use the Azure Command-Line Interface (Azure CLI) with Azure Storage to create and manage storage accounts and work with Azure blobs and files. The Azure CLI is a cross-platform tool "
  4. services="storage"
  5. documentationCenter="na"
  6. authors="tamram"
  7. manager="carmonm"/>
  8. <tags
  9. ms.service="storage"
  10. ms.workload="storage"
  11. ms.tgt_pltfrm="na"
  12. ms.devlang="na"
  13. ms.topic="article"
  14. ms.date="05/02/2016"
  15. ms.author="micurd"/>
  16. # Using the Azure CLI with Azure Storage
  17. ## Overview
  18. The Azure CLI provides a set of open source, cross-platform commands for working with the Azure Platform. It provides much of the same functionality found in the [Azure portal](https://portal.azure.com) as well as rich data access functionality.
  19. In this guide, well explore how to use [Azure Command-Line Interface (Azure CLI)](../xplat-cli-install.md) to perform a variety of development and administration tasks with Azure Storage. We recommend that you download and install or upgrade to the latest Azure CLI before using this guide.
  20. This guide assumes that you understand the basic concepts of Azure Storage. The guide provides a number of scripts to demonstrate the usage of the Azure CLI with Azure Storage. Be sure to update the script variables based on your configuration before running each script.
  21. > [AZURE.NOTE] The guide provides the Azure CLI command and script examples for classic storage accounts. See [Using the Azure CLI for Mac, Linux, and Windows with Azure Resource Management](../virtual-machines/azure-cli-arm-commands.md#azure-storage-commands-to-manage-your-storage-objects) for Azure CLI commands for Resource Manager storage accounts.
  22. ## Get started with Azure Storage and the Azure CLI in 5 minutes
  23. This guide uses Ubuntu for examples, but other OS platforms should perform similarly.
  24. **New to Azure:** Get a Microsoft Azure subscription and a Microsoft account associated with that subscription. For information on Azure purchase options, see [Free Trial](https://azure.microsoft.com/pricing/free-trial/), [Purchase Options](https://azure.microsoft.com/pricing/purchase-options/), and [Member Offers](https://azure.microsoft.com/pricing/member-offers/) (for members of MSDN, Microsoft Partner Network, and BizSpark, and other Microsoft programs).
  25. See [Assigning administrator roles in Azure Active Directory (Azure AD)](https://msdn.microsoft.com/library/azure/hh531793.aspx) for more information about Azure subscriptions.
  26. **After creating a Microsoft Azure subscription and account:**
  27. 1. Download and install the Azure CLI following the instructions outlined in [Install the Azure CLI](../xplat-cli-install.md).
  28. 2. Once the Azure CLI has been installed, you will be able to use the azure command from your command-line interface (Bash, Terminal, Command prompt) to access the Azure CLI commands. Type `azure` command and you should see the following output.
  29. ![Azure Command Output][Image1]
  30. 3. In the command line interface, type `azure storage` to list out all the azure storage commands and get a first impression of the functionalities the Azure CLI provides. You can type command name with **-h** parameter (for example, `azure storage share create -h`) to see details of command syntax.
  31. 4. Now, well give you a simple script that shows basic Azure CLI commands to access Azure Storage. The script will first ask you to set two variables for your storage account and key. Then, the script will create a new container in this new storage account and upload an existing image file (blob) to that container. After the script lists all blobs in that container, it will download the image file to the destination directory which exists on the local computer.
  32. #!/bin/bash
  33. # A simple Azure storage example
  34. export AZURE_STORAGE_ACCOUNT=<storage_account_name>
  35. export AZURE_STORAGE_ACCESS_KEY=<storage_account_key>
  36. export container_name=<container_name>
  37. export blob_name=<blob_name>
  38. export image_to_upload=<image_to_upload>
  39. export destination_folder=<destination_folder>
  40. echo "Creating the container..."
  41. azure storage container create $container_name
  42. echo "Uploading the image..."
  43. azure storage blob upload $image_to_upload $container_name $blob_name
  44. echo "Listing the blobs..."
  45. azure storage blob list $container_name
  46. echo "Downloading the image..."
  47. azure storage blob download $container_name $blob_name $destination_folder
  48. echo "Done"
  49. 5. In your local computer, open your preferred text editor (vim for example). Type the above script into your text editor.
  50. 6. Now, you need to update the script variables based on your configuration settings.
  51. - **<storage_account_name>** Use the given name in the script or enter a new name for your storage account. **Important:** The name of the storage account must be unique in Azure. It must be lowercase, too!
  52. - **<storage_account_key>** The access key of your storage account.
  53. - **<container_name>** Use the given name in the script or enter a new name for your container.
  54. - **<image_to_upload>** Enter a path to a picture on your local computer, such as: "~/images/HelloWorld.png".
  55. - **<destination_folder>** Enter a path to a local directory to store files downloaded from Azure Storage, such as: ~/downloadImages.
  56. 7. After youve updated the necessary variables in vim, press key combinations Esc, : , wq! to save the script.
  57. 8. To run this script, simply type the script file name in the bash console. After this script runs, you should have a local destination folder that includes the downloaded image file. The following screenshot shows an example output:
  58. After the script runs, you should have a local destination folder that includes the downloaded image file.
  59. ## Manage storage accounts with the Azure CLI
  60. ### Connect to your Azure subscription
  61. While most of the storage commands will work without an Azure subscription, we recommend you to connect to your subscription from the Azure CLI. To configure the Azure CLI to work with your subscription, follow the steps in [Connect to an Azure subscription from the Azure CLI](../xplat-cli-connect.md).
  62. ### Create a new storage account
  63. To use Azure storage, you will need a storage account. You can create a new Azure storage account after you have configured your computer to connect to your subscription.
  64. azure storage account create <account_name>
  65. The name of your storage account must be between 3 and 24 characters in length and use numbers and lower-case letters only.
  66. ### Set a default Azure storage account in environment variables
  67. You can have multiple storage accounts in your subscription. You can choose one of them and set it in the environment variables for all the storage commands in the same session. This enables you to run the Azure CLI storage commands without specifying the storage account and key explicitly.
  68. export AZURE_STORAGE_ACCOUNT=<account_name>
  69. export AZURE_STORAGE_ACCESS_KEY=<key>
  70. Another way to set a default storage account is using connection string. Firstly get the connection string by command:
  71. azure storage account connectionstring show <account_name>
  72. Then copy the output connection string and set it to environment variable:
  73. export AZURE_STORAGE_CONNECTION_STRING=<connection_string>
  74. ## Create and manage blobs
  75. Azure Blob storage is a service for storing large amounts of unstructured data, such as text or binary data, that can be accessed from anywhere in the world via HTTP or HTTPS. This section assumes that you are already familiar with the Azure Blob storage concepts. For detailed information, see [Get started with Azure Blob storage using .NET](storage-dotnet-how-to-use-blobs.md) and [Blob Service Concepts](http://msdn.microsoft.com/library/azure/dd179376.aspx).
  76. ### Create a container
  77. Every blob in Azure storage must be in a container. You can create a private container using the `azure storage container create` command:
  78. azure storage container create mycontainer
  79. > [AZURE.NOTE] There are three levels of anonymous read access: **Off**, **Blob**, and **Container**. To prevent anonymous access to blobs, set the Permission parameter to **Off**. By default, the new container is private and can be accessed only by the account owner. To allow anonymous public read access to blob resources, but not to container metadata or to the list of blobs in the container, set the Permission parameter to **Blob**. To allow full public read access to blob resources, container metadata, and the list of blobs in the container, set the Permission parameter to **Container**. For more information, see [Manage anonymous read access to containers and blobs](storage-manage-access-to-resources.md).
  80. ### Upload a blob into a container
  81. Azure Blob Storage supports block blobs and page blobs. For more information, see [Understanding Block Blobs, Append Blobs, and Page Blobs](http://msdn.microsoft.com/library/azure/ee691964.aspx).
  82. To upload blobs in to a container, you can use the `azure storage blob upload`. By default, this command uploads the local files to a block blob. To specify the type for the blob, you can use the `--blobtype` parameter.
  83. azure storage blob upload '~/images/HelloWorld.png' mycontainer myBlockBlob
  84. ### Download blobs from a container
  85. The following example demonstrates how to download blobs from a container.
  86. azure storage blob download mycontainer myBlockBlob '~/downloadImages/downloaded.png'
  87. ### Copy blobs
  88. You can copy blobs within or across storage accounts and regions asynchronously.
  89. The following example demonstrates how to copy blobs from one storage account to another. In this sample we create a container where blobs are publicly, anonymously accessible.
  90. azure storage container create mycontainer2 -a <accountName2> -k <accountKey2> -p Blob
  91. azure storage blob upload '~/Images/HelloWorld.png' mycontainer2 myBlockBlob2 -a <accountName2> -k <accountKey2>
  92. azure storage blob copy start 'https://<accountname2>.blob.core.windows.net/mycontainer2/myBlockBlob2' mycontainer
  93. This sample performs an asynchronous copy. You can monitor the status of each copy operation by running the `azure storage blob copy show` operation.
  94. Note that the source URL provided for the copy operation must either be publicly accessible, or include a SAS (shared access signature) token.
  95. ### Delete a blob
  96. To delete a blob, use the below command:
  97. azure storage blob delete mycontainer myBlockBlob2
  98. ## Create and manage file shares
  99. Azure File storage offers shared storage for applications using the standard SMB protocol. Microsoft Azure virtual machines and cloud services, as well as on-premises applications, can share file data via mounted shares. You can manage file shares and file data via the Azure CLI. For more information on Azure File storage, see [Get started with Azure File storage on Windows](storage-dotnet-how-to-use-files.md) or [How to use Azure File storage with Linux](storage-how-to-use-files-linux.md).
  100. ### Create a file share
  101. An Azure File share is an SMB file share in Azure. All directories and files must be created in a file share. An account can contain an unlimited number of shares, and a share can store an unlimited number of files, up to the capacity limits of the storage account. The following example creates a file share named **myshare**.
  102. azure storage share create myshare
  103. ### Create a directory
  104. A directory provides an optional hierarchical structure for an Azure file share. The following example creates a directory named **myDir** in the file share.
  105. azure storage directory create myshare myDir
  106. Note that directory path can include multiple levels, *e.g.*, **a/b**. However, you must ensure that all parent directories exists. For example, for path **a/b**, you must create directory **a** first, then create directory **b**.
  107. ### Upload a local file to directory
  108. The following example uploads a file from **~/temp/samplefile.txt** to the **myDir** directory. Edit the file path so that it points to a valid file on your local machine:
  109. azure storage file upload '~/temp/samplefile.txt' myshare myDir
  110. Note that a file in the share can be up to 1 TB in size.
  111. ### List the files in the share root or directory
  112. You can list the files and subdirectories in a share root or a directory using the following command:
  113. azure storage file list myshare myDir
  114. Note that the directory name is optional for the listing operation. If omitted, the command lists the contents of the root directory of the share.
  115. ### Copy files
  116. Beginning with version 0.9.8 of Azure CLI, you can copy a file to another file, a file to a blob, or a blob to a file. Below we demonstrate how to perform these copy operations using CLI commands. To copy a file to the new directory:
  117. azure storage file copy start --source-share srcshare --source-path srcdir/hello.txt --dest-share destshare --dest-path destdir/hellocopy.txt --connection-string $srcConnectionString --dest-connection-string $destConnectionString
  118. To copy a blob to a file directory:
  119. azure storage file copy start --source-container srcctn --source-blob hello2.txt --dest-share hello --dest-path hellodir/hello2copy.txt --connection-string $srcConnectionString --dest-connection-string $destConnectionString
  120. ## Next Steps
  121. Here are some related articles and resources for learning more about Azure Storage.
  122. - [Azure Storage Documentation](https://azure.microsoft.com/documentation/services/storage/)
  123. - [Azure Storage REST API Reference](https://msdn.microsoft.com/library/azure/dd179355.aspx)
  124. [Image1]: ./media/storage-azure-cli/azure_command.png