Search code examples
c#azureazure-storage

Copying one Azure blob to another blob in Azure Storage Client 2.0


In the old 1.7 storage client there was a CloudBlob.CopyFromBlob(otherBlob) method, but it does not seem to be present in the 2.0 version. What is the recommended best practice for copying blobs? I do see a ICloudBlob.BeginStartCopyFromBlob method. If that is the appropriate method, how do I use it?


Solution

  • Gaurav Mantri has written a series of articles on Azure Storage on version 2.0. I have taken this code extract from his blog post of Storage Client Library 2.0 – Migrating Blob Storage Code for Blob Copy

    CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer sourceContainer = cloudBlobClient.GetContainerReference(containerName);
    CloudBlobContainer targetContainer = cloudBlobClient.GetContainerReference(targetContainerName);
    string blobName = "<Blob Name e.g. myblob.txt>";
    CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference(blobName);
    CloudBlockBlob targetBlob = targetContainer.GetBlockBlobReference(blobName);
    targetBlob.StartCopyFromBlob(sourceBlob);