<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1678611822423757&amp;ev=PageView&amp;noscript=1">
Defrag This

| Read. Reflect. Reboot.

How to Copy Files to Azure Blob Storage

Adam Bertram| February 08 2019

| IT insights, security, Cloud Computing, Automation

When working with Microsoft Azure, you'll inevitably come to a point to where you need to access material stored locally on premise.

This might be a virtual disk in VHD format to use for Azure's IaaS service, a few PowerShell scripts you need executing on your Azure virtual machines or maybe just some configuration files for your Azure websites. Regardless, for your Azure resources to access these files, they'll need to be located in an Azure storage account.

The Set-AzureStorageBlobContent cmdlet

There are a couple of ways to transfer files stored locally into your Microsoft Azure storage account. I'll be doing this via the Set-AzureStorageBlobContent PowerShell cmdlet using the newer Azure Resource Manager (ARM) resources. The Set-AzureStorageBlobContent is available in the Azure PowerShell module, so you'll need to ensure you get this module downloaded and available for use first. You'll also need an Azure subscription as well as a storage account to store your files. In this example, I'll assume you already have a storage container pre-created.

Once you meet these prerequisites, you can then use the Set-AzureStorageBlobContent cmdlet to transfer your local files and convert them into blob storage automatically.

New cloud-based file transfer tools have made finding a secure solution tough.  See this video to learn what to look for. 

Authenticate an Account

To get started you'll first need to authenticate your Azure subscription, which you can do using the Add-AzureRmAccount cmdlet. This will prompt you for a username and password, granting you the token necessary to make changes to your Azure subscription.

Once you've authenticated your Azure subscription, you'll need to specify a storage account in which to create your Azure storage blob. Your local files will automatically turn into blob storage once the file gets transferred to Azure. To specify a storage account, you can use the Get-AzureRmStorageAccount cmdlet. Below, I have two storage accounts available to me:

 Get-AzureRmStorageAccount | select storageaccountname

Now you need to specify a storage container inside of one of these storage accounts. You can do this by passing the storage account object directly to the Get-AzureStorageContainer cmdlet.

 $storageContainer = Get-AzureRmStorageAccount | where {$_.StorageAccountName -eq 'adbdemostorageaccount'} | Get-AzureStorageContainer

Where's It Going?

You can see I've assigned this storage container to a variable, allowing me to quickly pass the object to the Set-AzureStorageBlobContent cmdlet. Once I have the storage container, I then need to define the local file path and the destination path. To do this, I'll use these all as parameters to the Set-AzureStorageBlobContent cmdlet.

 $FilePath = 'C:\Users\Adam\MyFile.txt'
 $BlobName = 'MyFile.txt'
 $storageContainer | Set-AzureStorageBlobContent –File $FilePath –Blob $BlobName

You can see that I've defined a text file that was stored in the C:\Users\Adam folder and made the blob the same name as the file. But this is unnecessary. During the copy you can change the name, but I typically keep it the same name for simplicity.

(Note: If you need to upload a VHD to an Azure storage account, do NOT use Set-AzureStorageBlobContent. I've had issues with corruption when this happens. Always use the Add-AzureRmVhd cmdlet instead.)

By using this method, you can easily copy files to your Azure storage account. However, it's always good practice to create reusable code when writing scripts with PowerShell. This is why I've created a function to ease this process called Copy-AzureItem. Feel free to download a copy and use it for yourself. It has personally saved me a lot of time, and supports VHDs as well.

 

Topics: IT insights, security, Cloud Computing, Automation

Leave a Reply

Your email address will not be published. Required fields are marked *

THIS POST WAS WRITTEN BY Adam Bertram

Adam Bertram is a 20-year veteran of IT. He’s currently an automation engineer, blogger, independent consultant, freelance writer, author, and trainer. Adam focuses on DevOps, system management, and automation technologies as well as various cloud platforms. He is a Microsoft Cloud and Datacenter Management MVP and efficiency nerd that enjoys teaching others a better way to leverage automation.

Free Trials

Getting started has never been easier. Download a trial today.

Download Free Trials

Contact Us

Let us know how we can help you. Focus on what matters. 

Send us a note

Subscribe to our Blog

Let’s stay in touch! Register to receive our blog updates.