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.
- CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnection"));
- CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
- CloudBlobContainer container = blobClient.GetContainerReference("images");
- CloudBlobContainer copycontainer = blobClient.GetContainerReference("images-copy");
- copycontainer.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
- CloudBlockBlob blockBlob = container.GetBlockBlobReference("img1.jpeg");
- CloudBlockBlob blockBlobCopy = copycontainer.GetBlockBlobReference("img1.jpeg");
- var cb = new AsyncCallback(x => Console.WriteLine("blob copy completed"));
- blockBlobCopy.BeginStartCopy(blockBlob.Uri, cb, null);
- Console.ReadKey();
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.