using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.DataMovement;
using System;
using System.IO;
using System.Threading.Tasks;
namespace BlobCopyOneStorageToAnother
{
class Program
{
static void Main(string[] args)
{
//Get source and destination azure storage account connection string from app.config
CloudStorageAccount sourceStorageConnectionString = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("SourceStorageConnectionString"));
CloudStorageAccount destinationStorageConnectionString = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("DestinationStorageConnectionString"));
CloudBlobClient sourceCloudBlobClient = sourceStorageConnectionString.CreateCloudBlobClient();
CloudBlobClient targetCloudBlobClient = destinationStorageConnectionString.CreateCloudBlobClient();
//Get source and destination container name from app.config
CloudBlobContainer sourceContainer = sourceCloudBlobClient.GetContainerReference(CloudConfigurationManager.GetSetting("SourceContainer"));
CloudBlobContainer destinationContainer = targetCloudBlobClient.GetContainerReference(CloudConfigurationManager.GetSetting("DestinationContainer"));
//Create container into blob if not exists
destinationContainer.CreateIfNotExists();
Console.WriteLine("Started copying all blob: " + sourceContainer.Name +" to "+ destinationContainer.Name);
// Copy each blob
foreach (IListBlobItem blob in sourceContainer.ListBlobs(useFlatBlobListing: true))
{
//Get blob url
Uri thisBlobUri = blob.Uri;
//Get blob name
var blobName = Path.GetFileName(thisBlobUri.ToString());
Console.WriteLine("Copying blob: " + blobName);
CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference(blobName);
CloudBlockBlob targetBlob = destinationContainer.GetBlockBlobReference(blobName);
//copy blob frim source to destination
Task task = TransferManager.CopyAsync(sourceBlob, targetBlob, true /* isServiceCopy */);
}
Console.WriteLine("All blob copied has been successful.");
Console.Read();
}
}
}