How to Copy Files to Azure Blob Storage

How to Copy Files to Azure Blob Storage

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 copied to 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.

 

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

Define Paths to Copy Files

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.

 

Related Posts


Comments
Comments are disabled in preview mode.
Thanks for subscribing! Loading animation