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:

In the previous post, we created the Azure Storage Blob Container and uploaded a file to it.

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

static void Main(string[] args)
{
    BlobServiceClient serviceClient = new BlobServiceClient(CloudConfigurationManager.GetSetting("StorageConnection"));
    BlobContainerClient container = serviceClient.GetBlobContainerClient("images-backup");
    container.CreateIfNotExists(PublicAccessType.Blob);

    BlockBlobClient blockBlob = container.GetBlockBlobClient("mikepic.png");

    //lines added
    BlockBlobClient copyBlockBlob = container.GetBlockBlobClient("mikepic-backup.png");

    copyBlockBlob.StartCopyFromUri(blockBlob.Uri);
    //end lines added
    Console.ReadLine();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

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: