Total Page Preview:   000000014597

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

In this article, we will leared how to move 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
 
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" />
    <add key="BlobName" value="AzureBlobInputProduct.txt" />
  </appSettings>

 

C# Code:

using Microsoft.WindowsAzure;

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.IO;
using System.Threading.Tasks;
 
//move container with blob one storage account to another storage account
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"));
             
            //Get source blob name from app.config
            string blobName = CloudConfigurationManager.GetSetting("BlobName");
 
            //Create container into blob if not exists
            destinationContainer.CreateIfNotExists();
 
            Console.WriteLine("Started moving blob: "  + blobName + " from container " + sourceContainer.Name +"  to "+ destinationContainer.Name);
 
            CloudBlockBlob sourceBlob = sourceContainer.GetBlockBlobReference(blobName);
            CloudBlockBlob targetBlob = destinationContainer.GetBlockBlobReference(blobName);
 
             //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);
              }
 
 
            Console.WriteLine(blobName + " blob moved has been successful.");
            Console.Read();
        }
    }
}
 

OUTPUT:

Blob Move One Storage Account To Another
 
Source Container:
All Blob Move One Storage Account To Another
 
Destination Container:
Blob Move 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

Fitzsimmons
16-Oct-2020
Hi there, I check your new stuff on a regular basis. Your humoristic style is witty, keep doing what you're doing!
Vanhoose
25-Feb-2021
Some terrific pictures. Impressive shades.
Cardell
09-Jul-2021
Hello, yeah this paragraph is actually good and I have learned lot of things from it concerning blogging. thanks.

                           
                           

                           

                           

Facebook User: