Copying Files To And From Azure Blob Storage

I had a situation today where I had to copy a file from a build server on premise to a location where a VM in Azure could access it as part of an automated process.

You have a few options that I know about, some of which are:

  • Ideally you would connect to Azure using VPN and then treat the VMs like any other machine on the network and copy to a share but that was not a possible at the moment.
  • Next best option seemed like Azure File Service but the subscription I was using had to join the preview program first and I couldn’t know for sure how long it would take to activate.
  • Next option I looked at was using Azure Blob Storage. It turned out to work pretty well.

If you are going to copy files on an ad hoc basis or you want to avoid rolling your own PowerShell script and you don’t mind installing extra tools on your build server AzCopy is pretty straight forward to use. You can download the latest version of it here. AzCopy can be used with Azure File Storage, Blob Storage and Table Storage.

In Blob Storage your blobs are stored in containers, you can think of it almost like a folder/directory, to create one is pretty easy using PowerShell.

New-AzureStorageContainer -Name testfiles -Permission -Off

Note the container name must be lower case. In this example I set the permissions to the owner of the storage account only, but you can make it public if you have the requirement. In this case I’m copying all the files from a folder on my local machine “C:\TestUpload” to the “testfiles” container.

AzCopy /Source:C:\TestUpload
/Dest:https://YourStorageAccount.blob.core.windows.net/testfiles /DestKey:YourStorageKey

You can get your Blob URL and storage key the from the Azure Portal under the Storage tab.

In the end I went with PowerShell to avoid installing tools on the build servers plus the build process already used PowerShell. In my case the build process created the artifact we needed as a single zip file, thus the script was very simple.

Set-AzureStorageBlobContent -Container testfiles
-File C:\TestUpload\TestCases.zip -Blob TestCases.zip

If you wanted to copy all the files in a folder you would have to iterate over them and call Set-AzureStorageBlobContent on each one, a quick Google search showed multiple examples on how to do that. Using AzCopy makes this scenario easier since you can tell it to copy a folder and do it recursively as well.

On the Azure VM I had to download the file again as part of the script running the tests.

Get-AzureStorageBlobContent -Blob TestCases.zip -Container testfiles -Destination TestCases.zip

Very easy to do and it worked very well in this situation.

Note: if you have a large number of files to upload it is better to do them in parallel since it can be a bit slow.

Francois Delport

Published by

Francois Delport

I am a cloud and devops consultant, technology fan and previously a professional C# developer with a keen interest in system design and architecture. Currently I am involved in projects using Azure, the Microsoft stack and DevOps. I am based in Melbourne, Australia. Email: [email protected]

Leave a Reply

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