Ganni Ram
Ganni Ram

Reputation: 1

C# Azure storage from one blob copy file to another blob

How can I read a file to stream from one blob and upload to another blob? My requirement is to copy a file from one blob to another blob with different file name? In C#

Upvotes: 0

Views: 3742

Answers (3)

namal wijekoon
namal wijekoon

Reputation: 101

  • I would like to recommend another method other than the above to get this done.

  • That is azure functions. (serverless compute service).

  • As a prerequisite, you should have some knowledge in azure functions, creating them, deploying them. 1. What is azure function 2. create an Azure function app

  • And the following code snippet is the simplest and basic way to perform this action. (In here, when a user uploading a new file to the "demo" blob, the function will be triggered and read that uploaded file from the demo blob and copy to the "output" blob.)

     namespace Company.Function{
     public static class NamalFirstBlobTrigger
     {
         [FunctionName("NamalFirstBlobTrigger")]
         public static void Run([BlobTrigger("demo/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, 
         [Blob("output/testing.cs",FileAccess.Write, Connection = "AzureWebJobsStorage")]Stream outputBlob,
         string name, 
         ILogger log)
         {
             myBlob.CopyTo(outputBlob);
         }
     }}
    

Upvotes: 0

JuanK
JuanK

Reputation: 2094

The easiest way to achieve it is using "Azure Storage Data Movement Library" (you can get it thru nuget package).

This is a simple-sample to make the transfer:

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.DataMovement;
using System;

namespace BlobClient
{
    class Program
    {
        static void Main(string[] args)
        {
            const string storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=juanktest;AccountKey=loHQwke4lSEu1p2W3gg==";
            const string container1 = "juankcontainer";
            const string sourceBlobName = "test.txt";
            const string destBlobName = "newTest.txt";


            //Setup Account, blobclient and blobs
            CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString);
            CloudBlobClient blobClient = account.CreateCloudBlobClient();

            CloudBlobContainer blobContainer = blobClient.GetContainerReference(container1);
            blobContainer.CreateIfNotExists();

            CloudBlockBlob sourceBlob = blobContainer.GetBlockBlobReference(sourceBlobName);

            CloudBlockBlob destinationBlob = blobContainer.GetBlockBlobReference(destBlobName);

            //Setup data transfer
            TransferContext context = new TransferContext();
            Progress<TransferProgress> progress = new Progress<TransferProgress>(
                (transferProgress) => {
                        Console.WriteLine("Bytes uploaded: {0}", transferProgress.BytesTransferred);
                });

            context.ProgressHandler = progress;

            // Start the transfer
            try
            {
                TransferManager.CopyAsync(sourceBlob, destinationBlob, 
                    false /* isServiceCopy */, 
                    null /* options */, context);
            }
            catch (Exception e)
            {
                Console.WriteLine("The transfer is cancelled: {0}", e.Message);
            }

            Console.WriteLine("CloudBlob {0} is copied to {1} ====successfully====", 
                            sourceBlob.Uri.ToString(), 
                            destinationBlob.Uri.ToString());

            Console.ReadLine();
        }
    }
}

Note that "Azure Storage Data Movement Library" is very robust so you can track the transfer progress, cancel the operation or even suspend it to resume it later ;)

Upvotes: 9

Don Lockhart
Don Lockhart

Reputation: 914

One of the easiest ways to copy files is with the AzCopy utility.

Upvotes: 0

Related Questions