Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Azure – Copy Blob from one container to another container

Article:
Viewed:  61 
Posted On:  22/09/2019 08:51:51 
How we can move Blob from one container to another container in Azure?  

In this article, I am going to show, how we can move blob from one container to another container.

For this, I am going to use a console application.

Open VS2017 -> File -> New Project ->

1.png

Image 1.

Now right click on Project Solution Explorer -> Manage NuGet

2.png

Image 2. 

Now open your App.config and add below keys:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup> 

  <appSettings>
    <add key="SourceStorageConnectionString" value="YourCS" />
    <add key="DestinationStorageConnectionString" value="YourCS" />
    <add key="SourceContainer" value="source" />
    <add key="DestinationContainer" value="destination" />
   </appSettings>

</configuration>

Get your Storage Account connection string from your Azure Portal.

3.png

Image 3.

Here I have a Storage account "storageaccountonetest"

Inside this in Blob I have 2 Container

  1.  source
  2. Destination

4.png

Image 4.

Now see files inside source container.

5.png

Image 5.

Currently there is no Blob in destination.

6.png

Image 6.

Now do below code in Program.cs file:

using System;
using System.Collections.Generic;

sing
System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.IO;
using Microsoft.Azure;
 
namespace ManageAzureBlob
{
    class Program
    {
        static void Main(string[] args)
        {
            //Get source azure storage account connection string from app.config
            CloudStorageAccount sourceStorageConnectionString =
                                                    CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(
"SourceStorageConnectionString")); 

            //Get destination azure storage account connection string from app.config
            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 Destination container into blob if not exists
            destinationContainer.CreateIfNotExists(); 

            // Loop over items within the container and output the length and URI.
            foreach (IListBlobItem item in sourceContainer.ListBlobs(null, false))
            {
                if (item.GetType() == typeof(CloudBlockBlob))
                {
                    CloudBlockBlob blob = (CloudBlockBlob)item;
                    Console.WriteLine("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri);
                    Console.WriteLine("Started moving blob: " + blob.Name + " from container " + sourceContainer.Name + "  to " + destinationContainer.Name);
                    CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference(blob.Name);
                    CloudBlockBlob targetBlob = destinationContainer.GetBlockBlobReference(blob.Name);
 
                    //copy blob from source to destination
                    targetBlob.StartCopy(sourceBlob);
 
                    //check if blob exists into destination container
                    if (targetBlob.Exists())
                    {
                        //Delete blob from source container
                        sourceBlob.Delete(DeleteSnapshotsOption.IncludeSnapshots);
                    }
                }
                else if (item.GetType() == typeof(CloudPageBlob))
                {
                    CloudPageBlob pageBlob = (CloudPageBlob)item;
                    Console.WriteLine("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri);
                    Console.WriteLine("Started moving blob: " + pageBlob.Name + " from container " +
                                                                                                                            sourceContainer.Name +
"  to " +destinationContainer.Name);
                    CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference(pageBlob.Name);
                    CloudBlockBlob targetBlob = destinationContainer.GetBlockBlobReference(pageBlob.Name);
 
                    targetBlob.StartCopy(sourceBlob);
                    if (targetBlob.Exists())
                    {
                        sourceBlob.Delete(DeleteSnapshotsOption.IncludeSnapshots);

      
             }
                }
                else if (item.GetType() == typeof(CloudBlobDirectory))
                {
                    CloudBlobDirectory directory = (CloudBlobDirectory)item;
                    Console.WriteLine("Directory: {0}", directory.Uri);
                }
            }
 
            Console.WriteLine(" Blob moved has been successfully.");
            Console.Read();

       
}
    }
}
 
Now run your application:

7.png

Image 7.

Now check your destination container.

8.png

Image 8.

View your Blob:

9.png

Image 9.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us