How to copy all the Azure Storage Blobs from a Container to the other container by C#.NET !.
Tags: azure, csharp
Published: 2021-11-29 07:31:53

There are so many ways to copy all the blobs from one AZURE Storage Container to a new one, but since I am an C# addict, lets do it by a simple C#.NET project in a very basic .NET 6 template console app in 1 minute. 

First of all, create a .NET 6 Console application. Then follow the steps.

1- Install the package by Nugget
https://www.nuget.org/packages/WindowsAzure.Storage

2- Create a simple C# class and call it : CopyConfigs.cs, then copy and paste the context.

```
 public class CopyConfigs
    {
        public string? SourceStorageConnectionString { get; set; }
        public string? DestinationStorageConnectionString { get; set; }
        public string? SourceContainer { get; set; }
        public string? DestinationContainer { get; set; }

    }

```

3- Create another C# class and call it : AZContainerTool.cs, then copy and paste the context.

```
 public class AZContainerTool
    {
        private CopyConfigs Conf;

        public AZContainerTool(CopyConfigs conf)
        {
            this.Conf = conf;
        }

        public async Task CopyBlobsFromSourceContainertodestinationContainer()
        {
            
            CloudStorageAccount sourceStorageConnectionString = CloudStorageAccount.Parse(Conf .SourceStorageConnectionString);
            CloudStorageAccount destinationStorageConnectionString = CloudStorageAccount.Parse(Conf.DestinationStorageConnectionString);

            //
            CloudBlobClient sourceCloudBlobClient = sourceStorageConnectionString.CreateCloudBlobClient();
            CloudBlobClient targetCloudBlobClient = destinationStorageConnectionString.CreateCloudBlobClient();

            //
            CloudBlobContainer sourceContainer = sourceCloudBlobClient.GetContainerReference(Conf.SourceContainer);
            CloudBlobContainer destinationContainer = targetCloudBlobClient.GetContainerReference(Conf.DestinationContainer);

            //Creates the Container if it was not Exists
            await destinationContainer.CreateIfNotExistsAsync();

            Console.WriteLine("Started moving.... all blob: " + sourceContainer.Name + "  to " + destinationContainer.Name);


            BlobResultSegment segment = await sourceContainer.ListBlobsSegmentedAsync(null);
            List<IListBlobItem> list = new List<IListBlobItem>();
            list.AddRange(segment.Results);

            // Move each blob 
            foreach (IListBlobItem blob in list)
            {
                Uri thisBlobUri = blob.Uri;

                var blobName = Path.GetFileName(thisBlobUri.ToString());
                Console.WriteLine("moving blob: " + blobName);

                CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference(blobName);
                CloudBlockBlob targetBlob = destinationContainer.GetBlockBlobReference(blobName);

                await targetBlob.StartCopyAsync(sourceBlob);

            }
        }
    }

```
4- Go back to program.cs and follow as I have explained below.

Set the connection string from the AZURE Portal and then set the source and Destination Container. It was "demo" and "demonew" for me.
```
CopyConfigs Conf = new CopyConfigs
{
    SourceContainer = "demo",
    DestinationContainer = "demonew",
    SourceStorageConnectionString = "",
    DestinationStorageConnectionString = ""
};


AZContainerTool AZCopy = new AZContainerTool(Conf);
await AZCopy.CopyBlobsFromSourceContainertodestinationContainer();



Console.WriteLine("All blob moved has been successful.");
Console.Read();
```
5- You are an F5 away from running the code.


You can always find me at : AliKolahdoozan@Gmail.com

Wish you luck...
Ali