Azure Storage - Blob Copy














You will probably find this post useful, if you need to rename or copy blobs (like virtual machine page blobs or block blobs). If renaming is what you are after, you are out of luck. At the moment, Azure storage does not expose functionality to rename blobs. The workaround is to copy the blob and give it a new name. The good news is that you do not need to download the files locally (blobs can be in the TBs scale) i.e. Azure Blob Storage supports direct copy between subscriptions and even accounts.



Searching the web for documentation and examples can be tedious as newer versions of the Azure storage SDK are not backwards compatible!



The following is a simple code for copying blobs programmatically using the SDK version 7.x:

// Parses a connection string and returns a CloudStorageAccount created from the connection string.
var destinationStorageAccount = CloudStorageAccount.Parse(Your Destination Storage Account Connection String);
var destinationCloudBlobClient = destinationStorageAccount.CreateCloudBlobClient();
// Returns a reference to a CloudBlobContainer object with the specified name.
var destinationContainer = destinationCloudBlobClient.GetContainerReference(Destination Container Name);
destinationContainer.CreateIfNotExists();
CloudBlob destBlob = destinationContainer.GetBlockBlobReference(Destination Blob Name);
// We assume the source is accessible with the proper SAS
destBlob.StartCopy(new Uri(Source Blob));



To generalize this to a method, we can have a signature such as the following:

public void CopyBlob(CloudStorageAccount sourceStorageAccount, CloudStorageAccount destinationStorageAccount, string containerName, string blobName)











Also, see this stackoverflow answer from Apr 12 at 12:29, with regards to waiting for the long operation to finish:



while (targetCloudBlob.CopyState.Status == CopyStatus.Pending)

{

   Thread.Sleep(500);
}





As for using a tool to copy, you have many options like the free AzCopy or paid Cloud Berry.





Good luck!




Comments