How to move a file on Azure File Storage from one sub folder to another sub folder using the Azure Storage SDK?

25,162

Solution 1

This is documented in the Getting Started guide on Azure Storage Files reference.

What you need is the StartCopy method to copy the file from one location to another.

// Start the copy operation.
destinationFile.StartCopy(sourceFile);

And, yes, you will have to create the destination directory if it does not exist.

Solution 2

Like this:

public static void MoveTo(this CloudFile source, CloudFileDirectory directory)
{
    var target = directory.GetFileReference(source.Name);
    target.StartCopy(source);
    source.Delete();
}

Solution 3

Unfortunately we don't have move / rename functionality exposed through the REST API that the Client SDK's are dependent on. You can of course perform these functions via SMB. We do have these features on our backlog but don't have a timeline yet for implementation.

Solution 4

Here is an updated answer for Asp.net Core 3+ with the new blob API's. You can use a BlockBlobClient with StartCopyFromUriAsync and if you want to await completion WaitForCompletionAsync

var blobServiceClient = new BlobServiceClient("StorageConnectionString");
var containerClient = blobServiceClient.GetBlobContainerClient(container);
var blobs = containerClient.GetBlobs(BlobTraits.None, BlobStates.None, sourceFolder);

await Task.WhenAll(blobs.Select(async blob =>
{
    var targetBlobClient = containerClient.GetBlockBlobClient($"{targetFolder}/{blob.Name}");
    var blobUri = new Uri($"{containerClient.Uri}/{blob.Name}");
    var copyOp = await targetBlobClient.StartCopyFromUriAsync(blobUri);
    return await copyOp.WaitForCompletionAsync();
}).ToArray());

Or if you don't need to wait for completion and just want to "fire and forget".

foreach (var blob in blobs)
{
    var targetBlobClient = containerClient.GetBlockBlobClient($"{targetFolder}/{blob.Name}");
    var blobUri = new Uri($"{containerClient.Uri}/{blob.Name}");
    targetBlobClient.StartCopyFromUriAsync(blobUri);
}

Solution 5

An Azure Storage File share is an SMB-compatible share. So you should be able to make file copies/moves with normal file I/O operations. This is in contrast to direct blob manipulation, where you need to specifically create containers, initiate blob copies, etc. via the Storage API.

Share:
25,162
Pure.Krome
Author by

Pure.Krome

Just another djork trying to ply his art in this mad mad world. Tech stack I prefer to use: Laguage: C# / .NET Core / ASP.NET Core Editors: Visual Studio / VS Code Persistence: RavenDB, SqlServer (MSSql or Postgres) Source control: Github Containers: Docker & trying to learn K&'s Cloud Platform: Azure Caching/CDN: Cloudflare Finally: A Tauntaun sleeping bag is what i've always wanted spaces > tabs

Updated on July 25, 2022

Comments

  • Pure.Krome
    Pure.Krome almost 2 years

    I'm trying to figure out how to move a file in Azure File Storage from one location to another location, in the same share.

    E.g.

    source -> \\Share1\someFile.txt
    destination -> \\Share1\Foo\Bar\someFile.txt
    
    • Do I need to copy the file first, then delete the source?
    • What if the destination sub-directory aren't there? do I need to CreateIfNotExistsAsync for each sub-directory, first?

    cheers!