Understanding Blob Storage - Part Three - Async Blob Copy

Here, in this article, we will see how to copy blobs from one container to another inside Azure Storage using an asynchronous copy operation. Here we are going to copy the file img1.jepg from images container to a newly created container called images-copy.

Here, in this article, we will see how to copy blobs from one container to another inside Azure Storage using an asynchronous copy operation. You can read previous parts of this "Understanding Blob Storage" here:

 
Here, we are going to copy the file img1.jepg from images container to a newly-created container called images-copy.

Step 1

Copy the following code to the main function. In this code, we are creating one more container reference to a container called images-copy and creating it if it doesn’t exist. We are also defining an asynchronous call back after the copy operation is completed. Here, we are using the URI of the blob to initiate the copy.

  1. CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnection"));  
  2. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();  
  3. CloudBlobContainer container = blobClient.GetContainerReference("images");  
  4. CloudBlobContainer copycontainer = blobClient.GetContainerReference("images-copy");  
  5. copycontainer.CreateIfNotExists(BlobContainerPublicAccessType.Blob);  
  6. CloudBlockBlob blockBlob = container.GetBlockBlobReference("img1.jpeg");  
  7. CloudBlockBlob blockBlobCopy = copycontainer.GetBlockBlobReference("img1.jpeg");  
  8. var cb = new AsyncCallback(x => Console.WriteLine("blob copy completed"));  
  9. blockBlobCopy.BeginStartCopy(blockBlob.Uri, cb, null);  
  10. Console.ReadKey();  

Azure 

Step 2

Run your application and you can see a message on the screen when the file is created and if you refresh the containers list in the blob storage on the portal, you can see a new container created with the file copied inside the container.

Azure 
 
Azure 
 
Azure