Total Page Preview:   000000009297

How to copy all blob one azure storage account container to another storage account container in c#

In this article, we will leared how to copy all blob one azure storage account container to another storage account container in c#.

WindowsAzure.Storage

Install WindowsAzure.Storage SDK for use blob storage account:

Install-Package WindowsAzure.Storage -Version 8.7.0
 
 
To get the binaries of this library as distributed by Microsoft, ready for use within your project you can also have them installed by the .NET package manager
Microsoft.Azure.Storage.DataMovement depend on WindowsAzure.Storage SDK.
 
Install-Package Microsoft.Azure.Storage.DataMovement
 
Once installed above SDK then folow below. 
 
Copy and Paste below code in App.config file and change connection string 
 
 <appSettings>
    <add key="SourceStorageConnectionString" value="connectionString" />
 
    <add key="DestinationStorageConnectionString" value="connectionString" />
 
    <add key="SourceContainer" value="source-container" />
 
    <add key="DestinationContainer" value="destination-container" />
  </appSettings>

 

C# Code:

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();
        }
    }
}
 

OUTPUT:

All Blob Copy One Storage Account To Another
 
All Blob Copy One Storage Account To Another

Thank You

About Author

Brijesh Kumar

Database Developer

I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall and Database Migration On-Premise to Azure Cloud.
LinkedIn : https://www.linkedin.com



Comments

shakir
02-Nov-2019
very good explain
Abney
05-Nov-2021
Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a little bit, but instead of that, this is great blog. A great read. I will definitely be back.

                           
                           

                           

                           

Facebook User: