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:  141 
Posted On:  26/09/2019 12:43:43 
How we can Copy Blob from One Container to another Container through Azure Portal Function? 

 

In this article, I am going to show, how you can copy Blob from one container to another container in Azure Function Portal.

Below are the steps:

I have one: Storage Account =   saqar

Two Container inside this Storage Account

  1.  source

  2.  destination

2.png

Image 1.

I will create a Function App = testFunctionAppQAR

Inside this I will add a Blob Trigger function.

So that if any blob comes inside source container then it should copy current uploaded blob to destination container.

Step 1: Create a New Function App:

1.png

Image 2.

Click on Create.

3.png

Image 3.

4.png

Image 4.

5.png

Image 5.

Now add a new file project.json to download NuGet package.

6.png

Image 6.

Add below code in your project.json file:

    {
      "frameworks": {
           "net46":{
               "dependencies": {
                   "WindowsAzure.Storage": "7.0.0"
                                         }
                     }
              }
       }

 

7.png

Image 7.

Save it and check Log.

Now click on Integrate from left side:

8.png

Image 8.

Now Open run.csx file and do below code:

using System;
using System.IO;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
 
{
    log.Info("COPY BLOB- AZURE FUNCTION START....");

   
await CopyBlob(myBlob, log);
}
 
private async static Task CopyBlob(CloudBlockBlob myBlob, TraceWriter log)
{
    var storageAccountkey = "ncYwHsiu9tFxGhDBksgSbjIeEh4JvbJGNxU3yTztGOz9S8go8Gj7wSMVdLZnfcQkWuCo+lDEA9B2eoESHZrHig==";
    var connectionString = $"DefaultEndpointsProtocol=https;AccountName=saqar;AccountKey={storageAccountkey}";
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
 
    // destination blob client
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference("destination");

    // Check if destination container Exist or Not
    try
    {
        await container.CreateIfNotExistsAsync();

   
}
    catch(Exception e)
    {
        log.Error(e.Message);
    }
   
    // Get Reference of Blob
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(myBlob.Name);
    log.Info($"BlockBlob - destination name: {blockBlob.Name}");
   
    log.Info("Blob Starting Copy...");

    try
   {
       //copy to blob
        blockBlob.BeginStartCopy(myBlob, null, null);         
        log.Info("Blob Copied Succeeded.");       

    
}

    catch(Exception ex)
   {
        log.Error(ex.Message);
        log.Info("Error in Copying.");
    }
    finally
    {
        log.Info("========Copy Operation completed========");
    }
}

 

9.png

Image 9.

Now upload a file in source container and check destination container, whether it copied  blob or not.

10.png

Image 10.

11.png

Image 11.

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