title | description | services | author | ms.custom | ms.service | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|
Azure Quickstart - Create a blob in object storage using Azure PowerShell | Microsoft Docs |
In this quickstart, you use Azure PowerShell in object (Blob) storage. Then you use PowerShell to upload a blob to Azure Storage, download a blob, and list the blobs in a container. |
storage |
roygara |
mvc |
storage |
quickstart |
04/09/2018 |
rogarana |
The Azure PowerShell module is used to create and manage Azure resources from the PowerShell command line or in scripts. This guide details using PowerShell to transfer files between local disk and Azure Blob storage.
If you don't have an Azure subscription, create a free account before you begin.
This quickstart requires the Azure PowerShell module version 3.6 or later. Run Get-Module -ListAvailable AzureRM
to find the version. If you need to install or upgrade, see Install Azure PowerShell module.
[!INCLUDE storage-quickstart-tutorial-intro-include-powershell]
Blobs are always uploaded into a container. You can organize groups of blobs similar to the way you organize your files on your computer in folders.
Set the container name, then create the container using New-AzureStorageContainer, setting the permissions to 'blob' to allow public access of the files. The container name in this example is quickstartblobs.
$containerName = "quickstartblobs"
New-AzureStorageContainer -Name $containerName -Context $ctx -Permission blob
Blob storage supports block blobs, append blobs, and page blobs. VHD files used to back IaaS VMs are page blobs. Append blobs are used for logging, such as when you want to write to a file and then keep adding more information. Most files stored in Blob storage are block blobs.
To upload a file to a block blob, get a container reference, then get a reference to the block blob in that container. Once you have the blob reference, you can upload data to it by using Set-AzureStorageBlobContent. This operation creates the blob if it doesn't already exist, or overwrites it if it does already exist.
The following examples upload Image001.jpg and Image002.png from the D:\_TestImages folder on the local disk to the container you created.
# upload a file
Set-AzureStorageBlobContent -File "D:\_TestImages\Image001.jpg" `
-Container $containerName `
-Blob "Image001.jpg" `
-Context $ctx
# upload another file
Set-AzureStorageBlobContent -File "D:\_TestImages\Image002.png" `
-Container $containerName `
-Blob "Image002.png" `
-Context $ctx
Upload as many files as you like before continuing.
Get a list of blobs in the container using Get-AzureStorageBlob. This example shows just the names of the blobs uploaded.
Get-AzureStorageBlob -Container $ContainerName -Context $ctx | select Name
Download the blobs to your local disk. For each blob to be downloaded, set the name and call Get-AzureStorageBlobContent to download the blob.
This example downloads the blobs to D:\_TestImages\Downloads on the local disk.
# download first blob
Get-AzureStorageBlobContent -Blob "Image001.jpg" `
-Container $containerName `
-Destination "D:\_TestImages\Downloads\" `
-Context $ctx
# download another blob
Get-AzureStorageBlobContent -Blob "Image002.png" `
-Container $containerName `
-Destination "D:\_TestImages\Downloads\" `
-Context $ctx
The AzCopy utility is another option for high-performance scriptable data transfer for Azure Storage. You can use AzCopy to transfer data to and from Blob, File, and Table storage.
As a quick example, here is the AzCopy command for uploading a file called myfile.txt to the mystoragecontainer container from within a PowerShell window.
./AzCopy `
/Source:C:\myfolder `
/Dest:https://mystorageaccount.blob.core.windows.net/mystoragecontainer `
/DestKey:<storage-account-access-key> `
/Pattern:"myfile.txt"
Remove all of the assets you've created. The easiest way to do this is to delete the resource group. This also deletes all resources contained within the group. In this case, it removes the storage account and the resource group itself.
Remove-AzureRmResourceGroup -Name $resourceGroup
In this quickstart, you learned how to transfer files between a local disk and Azure Blob storage. To learn more about working with Blob storage using PowerShell, continue to How-to use Azure PowerShell with Azure Storage.
[!div class="nextstepaction"] Using Azure PowerShell with Azure Storage
- Microsoft Azure Storage Explorer is a free, standalone app from Microsoft that enables you to work visually with Azure Storage data on Windows, macOS, and Linux.