Azure Storage - Blob Copy
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
Post a Comment