TIP

💡 Learn more : Azure storage account overview.

Copy Azure Storage Blobs and Files via C#

Last week we've reviewed the following options with Azure Storage :

Today, we are going to copy Azure Storage Blobs (and Files) via C#. Go ahead and open the Azure Portal and open the C# app that we worked with earlier.

The goal of this exercise is to copy a file inside our Azure Storage Container to a new file. So for example, our Azure Storage Container only contains one file now:

We are going to copy a new file inside of it with the name mikepic-backup.png.

Now that we've created the Azure Storage Blob Container, we'll upload a file to it. We'll build off our previous post and add the following lines of code to upload a file off our local hard disk:

static void Main(string[] args)
{
    var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnection"));
    var myClient = storageAccount.CreateCloudBlobClient();
    var container = myClient.GetContainerReference("images-backup");
    container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);

    var blockBlob = container.GetBlockBlobReference("mikepic.png");
//lines added
    var copyBlockBlob = container.GetBlockBlobReference("mikepic-backup.png");

    var cb = new AsyncCallback(x => Console.WriteLine("copy completed"));
    copyBlockBlob.BeginStartCopy(blockBlob, cb, null);
//end lines added

    Console.ReadLine();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

If we run the application and switch over to our Storage Account and navigate inside the container, we'll see our file has copied successfully: