Skip to content

Instantly share code, notes, and snippets.

@chgeuer
Created August 24, 2012 13:06
Show Gist options
  • Select an option

  • Save chgeuer/3450349 to your computer and use it in GitHub Desktop.

Select an option

Save chgeuer/3450349 to your computer and use it in GitHub Desktop.
Copy blobs in Azure
using System;
using System.Data.Services.Client;
using System.Data.Services.Common;
using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Threading;
namespace CopyBlobsViaAPI
{
class Program
{
public static void StartCopy(CloudStorageAccount srcAccount, string srcContainerName, string srcBlobName,
CloudStorageAccount destAccount, string destContainerName, string destBlobName)
{
var srcClient = srcAccount.CreateCloudBlobClient();
var srcContainer = srcClient.GetContainerReference(srcContainerName);
var srcBlob = srcContainer.GetPageBlobReference(srcBlobName);
var destClient = destAccount.CreateCloudBlobClient();
var destContainer = destClient.GetContainerReference(destContainerName);
var destBlob = destContainer.GetPageBlobReference(destBlobName);
var readPolicy = new SharedAccessBlobPolicy() {
Permissions = SharedAccessBlobPermissions.List | SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddDays(1) };
var srcUri = string.Format("{0}{1}", srcBlob.Uri.AbsoluteUri, srcBlob.GetSharedAccessSignature(readPolicy));
Console.WriteLine("Initiate copy fromn {0} to {1}", srcUri, destBlob.Uri.AbsoluteUri);
destBlob.StartCopyFromBlob(new Uri(srcUri));
// destBlob.StartCopyFromBlob(srcBlob);
}
public static string GetCopyStatus(
CloudStorageAccount destAccount, string destContainerName, string destBlobName)
{
var destClient = destAccount.CreateCloudBlobClient();
var destContainer = destClient.GetContainerReference(destContainerName);
var destBlob = destContainer.GetPageBlobReference(destBlobName);
destBlob.FetchAttributes();
return string.Format("{0}: {1}", destBlob.CopyState.Status.ToString(), destBlob.CopyState.StatusDescription);
}
static void Main(string[] args)
{
var srcAccountName = "sourceazureblobaccount";
var srcAccountKey = "SourceAzureBlobAccount//IDhkjdshfkjhdskjfhdskjhfkjhdsjfhdsjkhfjkhdshfjshfkjhsjdkhfkjsh==";
var destAccountName = "destinationazureblobaccount";
var destAccountKey = "Destination/Azure//Blob//Account/Key/97329uoi32usklfdshfjjhhjhjdshfkjshfkjshkjfhskjhfj==";
var srcAccount = new CloudStorageAccount(new StorageCredentialsAccountAndKey(srcAccountName, srcAccountKey), useHttps: true);
var destAccount = new CloudStorageAccount(new StorageCredentialsAccountAndKey(destAccountName, destAccountKey), useHttps: true);
var blobs = new string[] { "somevhd-3333222344.vhd" }.ToList();
var srcContainerName = "vhds";
var destContainerName = "vhds";
blobs.ForEach(b => StartCopy(srcAccount, srcContainerName, b, destAccount, destContainerName, b));
while (true)
{
Thread.Sleep(TimeSpan.FromSeconds(10));
blobs.ForEach(b => Console.WriteLine("{0}: {1}", b, GetCopyStatus(destAccount, destContainerName, b)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment