In this post I will show you how to create blob storage in Azure with PowerShell.
Azure blob storage is great for storing large amount of data that is accessible from all around the world through HTTP or HTTPS.
I mainly use it for storing scripts that I use often at different customers instead of storing them locally on my computer or on one of many USB-sticks that I have a tendency to lose or misplace every other month. (Thank god for encryption and common sense not to save certain files and documents this way)
Install Azure PowerShell
If you haven’t already, make sure to install Azure PowerShell according to the following link: Install Azure PowerShell
Short version, installing from the PowerShell Gallery:
# Install the Azure Resource Manager modules from the PowerShell Gallery Install-Module AzureRM # Install the Azure Service Management module from the PowerShell Gallery Install-Module Azure
Connect to Azure
When Azure PowerShell has been installed, we need to connect to our subscription in Azure.
To do this, we run the following command:
Add-AzureRmAccount
Then enter your credentials in the following window that will appear. (Sorry for Swedish image)
Next, we need to specify which subscription to use for this. To get a list of all available subscriptions linked to your account run the following command:
Get-AzureRmSubscription | select SubscriptionId
Select the appropriate “SubscriptionId” from the list and run the following command:
Set-AzureRmContext -SubscriptionID "Your SubscriptionId"
Create the Resource Group
The first thing we need is a resource group. This is created by running the following command:
New-AzureRmResourceGroup -Name Storage -Location "westeurope"
In the above example, I’ve created a Resource Group called “Storage” in the west Europe region.
Create the Storage account and Blob storage
Create the Storage Account
Now that we have a resource group we can continue to create our storage account and our blob storage.
We do this by running the following line:
New-AzureRmStorageAccount -ResourceGroupName "Storage" -AccountName "guidestomsfiles" -Location "westeurope" -Type "Standard_LRS"
As you can see, I created the storage account in the resource group we created earlier, giving it the name “guidestomsfiles”, placed it in the same region as earlier and specified it to be a standard locally-redundant storage.
Create the storage container
With our storage account created, we now need to get the storage account key. We need this to be able to create our container.
To get the two keys that are available, run the following line:
$storagekey = Get-AzureRmStorageAccountKey -ResourceGroupName "Storage" -Name "guidestomsfiles"
In the above line, I specify the name of the resource group and the name of the storage account.
Next, we need to save the context that we will run in a variable before we can create the blob container.
$context = New-AzureStorageContext -StorageAccountName "guidestomsfiles" -StorageAccountKey $storagekey.Key1 -Protocol Http
Again, we specify the name of our storage account, the key that we stored earlier in “$storagekey” and which protocol to use.
When this is done, we can create the blob storage with the following line:
New-AzureStorageContainer -Name "files" -Permission Blob -Context $context
In the above example I named the blob container “files”, specified it to be a Blob and then pointed to “$context” for context.
Uploading and downloading
Well, so now we have a blob storage where we can upload and download files to and from. But how do we do that?
Uploading files
In this example I have a file called “File1.txt” located in “C:\temp” that I want to upload. To do this we run the following line:
Get-AzureRmStorageAccount -ResourceGroupName "Storage" -Name "guidestomsfiles" | Set-AzureStorageBlobContent –Container files -File C:\temp\File1.txt
First we get the resource group and the storage account and then pipe to the container called “files” that we created earlier and specify the file.
Now we have uploaded our first file to our blob storage!
If we want to upload the content of an entire folder to the container called “files”, we run the following line:
Get-ChildItem –Path C:\temp\* | Set-AzureStorageBlobContent -Container "files"
Downloading files
To download a single file we need to run the following:
$storagekey = Get-AzureRmStorageAccountKey -ResourceGroupName "Storage" -Name "guidestomsfiles" $context = New-AzureStorageContext -StorageAccountName "guidestomsfiles" -StorageAccountKey $storagekey.Key1 $FileName = "File1.txt" Get-AzureStorageBlobContent -Blob $FileName -Container files -Destination "C:\temp\blob" -Context $context
In the above example, we download the file “File1.txt” to “C:\temp\blob”
To download the entire blob to the local drive, we need to run the following:
$storagekey = Get-AzureRmStorageAccountKey -ResourceGroupName "Storage" -Name "guidestomsfiles" $context = New-AzureStorageContext -StorageAccountName "guidestomsfiles" -StorageAccountKey $storagekey.Key1 #Save all blobs in variable $blob = Get-AzureStorageBlob -Container files -Context $context #Download the blobs stored in $blob $blob | Get-AzureStorageBlobContent -Destination "C:\temp\blob" -Context $context
In the above example, we will save the content of the blob to “C:\temp\blob”
Using Microsoft Azure Storage Explorer
If for some reason you prefer a GUI, sometimes it could be useful, you can use Microsoft Azure Storage Explorer found HERE.
This tool allows you to upload and download files to your blob container and lists all available storage accounts in the same place.
I hope this guide will help you out there to get started with storing files in Azure.
Even though everything shown above can be done in the portal directly, I strongly advice you to use PowerShell instead. It might seem like it takes longer to do it this way, but when you have your first script in place, it’s just a matter of changing the name of variables to quickly setup new containers.
For more PowerShell guides, check the following link:
PowerShell Guides
Pingback: PowerShell Guides - A guide to Microsoft ProductsA guide to Microsoft Products