how to create a sub folder in azure storage account and copy the files by using azure function App

3.5k views Asked by At

I have two folders in my storage account Source and destination folders I want to add Demo folder in my Source folder and I want to copy my source folder files to Demo folder and delete all my source folder files after copying in to Archieve folder

  1. how to create a folder inside another folder
  2. how to copy files from one folder to another folder

how to add a folder I am passing in this way:

 var directory2 = container.GetDirectoryReference("csvfile / Source Folder / Archieve Folder");
        CloudBlockBlob blockBlob2 = directory2.GetBlockBlobReference("empchange1.csv");
  1. containername-csvfile
  2. Folder-Source
  3. SubFolder-Archieve
2

There are 2 answers

0
Peter Bons On BEST ANSWER

There is no such thing as a (physical) folder in Azure Blob Storage. There is only the concept of virtual folders. By using a / in the name you are creating a virtual folder. For example, a blob named virtualfolder/myblob.ext is placed in a virtual folder named virtualfolder.

If you want to create a virtual subfolder just put the name in the blob and use the '/' as delimiter like this:

virtualfolder/subfolder/myblob.ext

The notion of virtual folders is mentioned also if you take a look at the docs for cloudblobdirectory

Represents a virtual directory of blobs, designated by a delimiter character.

That said, moving a blob to a different folder would therefore the same as renaming a blob to, for example otherfolder/myblob.ext. Unfortunately there is no API that allows you to rename a blob. The solution is to download the blob contents and upload it with a different name.

0
Alex Gordon On

Here's an example of moving a blob from one container to another. Notice how it is a 2 step process, and notice the bindings that makes this declarative and simple to understand. I'm moving a blob from in to out:

using BlobMover.Models;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.IO;
using System.Threading.Tasks;

namespace BlobMover
{
    public static class BlobMover
    {
        [StorageAccount("Connection")]
        [FunctionName("BlobMover")]
        public static async Task Run(
            [QueueTrigger("%BlobMover:TriggerQueue%")] BlobMessage msg,
            [Blob("{source}-error/{name}", FileAccess.Write)] CloudBlockBlob error,
            [Blob("{destination}/{name}", FileAccess.Write)] CloudBlockBlob @out,
            [Blob("{source}/{name}", FileAccess.Read)] CloudBlockBlob @in,
            ILogger log)
        {
            var trackingId = Guid.NewGuid();

            await @out.StartCopyAsync(@in);
            await @in.DeleteAsync();
        }
    }
}