How To Create Azure Storage Blobs in PowerShell
Upload and transfer files to the Azure cloud in a few easy steps.
- By Adam Bertram
One of the most basic, common activities when using a cloud service like Microsoft Azure is to simply upload some files. Even though the cloud seems like some complicated entity in which you can't relate traditional terms like "file server" to, that's simply not true. At the most basic level, you can do simple transfer files from your computer into Azure just as you would with a traditional file copy. It's not quite that simple, but you get the picture.
If you're first getting started understanding Azure terminology you might see references to blobs, or binary large objects. In a roundabout way, this is simply a fancy term for files. Whenever you transfer a file from your computer into Azure, it technically now becomes a blob -- but don't get too caught up on the terminology. For now, just think of a blob as a file.
To create Azure storage blobs I'll use PowerShell. There are a number of different ways to upload files into Azure but for demonstration purposes, I will use PowerShell.
First of all, I'm going to assume you already have an Azure subscription. This is a requirement, obviously. Next, storage blobs are located in Azure storage accounts. You must have a storage account created in your subscription in order to upload files into Azure. Once you've got a subscription and a storage account created you are now able to begin uploading files into Azure.
Before you can upload files into Azure you must first create a storage container. To do this, you'll need to log into the portal using your credentials. Once in the portal, you'll now need to browse to your storage accounts. Once you arrive at your storage accounts, you'll see a window called Containers. Containers can be thought of as folders. You will need to create a container to storage your files in.
To create a container, simply click on the Containers window and you'll be prompted to create a new container with an access type. For now, I'm going to choose a private access type. I'm doing this because I don't want my files being publicly accessible. (Note: Storage containers are case sensitive! All container names must be in all lowercase.)
Once you've got the container built you now have a spot to upload your files. Unfortunately, you cannot upload files directly in the Azure portal. Instead, I'll use PowerShell, as I mentioned earlier.
To do this, first ensure you have the Azure PowerShell module installed. Once you do, you will have a cmdlet available to you called Set-AzureStorageBlobContent. This cmdlet allows you to specify a file on your local file system, an Azure container and the name of what your blob will be called once it arrives in the Azure cloud.
This cmdlet is very easy to use. For example, perhaps I have a file called C:\MyFile.txt on my local C drive. I've previously created a storage container called mycontainer previously in the Azure portal in my storage account called adbdemostorageaccount. Once it's in Azure, I'll want it to keep the same name. To do this, I would run the Set-AzureStorageBlobContent using the appropriate parameters.
Because the Set-AzureStorageBlobContent command doesn't know which storage account to upload to, I'll first need to retrieve that with the Get-AzureRmStorageAccount cmdlet and pipe it to our Set cmdlet as shown below.
Get-AzureRmStorageAccount –Name adbstorageaccount | Set-AzureStorageBlobContent –Container mycontainer –File C:\MyFile.txt –Blob MyFile.txt
When you start the upload you will immediately see a handy progress bar:
When it's completed you will get a summary of the file upload:
You've now uploaded your first file to Azure and created your first Azure blob!
Adam Bertram is an independent consultant, technical writer, trainer and presenter. Adam specializes in consulting and evangelizing all things IT automation mainly focused around Windows PowerShell. Adam is a Microsoft Windows PowerShell MVP, 2015 powershell.org PowerShell hero and has numerous Microsoft IT pro certifications. He is a writer, trainer and presenter and authors IT pro course content for Pluralsight. He is also a regular contributor to numerous print and online publications and presents at various user groups and conferences. You can find Adam at adamtheautomator.com or on Twitter at @adbertram.