13

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!

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647

6 Answers6

12

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.

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
sguler
  • 286
  • 1
  • 8
10

Like this:

public static void MoveTo(this CloudFile source, CloudFileDirectory directory)
{
    var target = directory.GetFileReference(source.Name);
    target.StartCopy(source);
    source.Delete();
}
Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233
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.

Jason Hogg - MSFT
  • 1,369
  • 9
  • 10
  • 3
    This is a Critical Feature for my corporate code bases. I can't reliably "port" on-prem apps where the concept of MOVE to sub directory is a critical pat of the architecture and workflow of the app. The Real Punch to the Gut is that it clearly exists in the SMB interfaces. Arrrrgh. The alternate solution would be able to mount temporarily a SMB drive in an Azure function app, then I could use traditional File IO concepts via System.IO and SMB 3 - all in Azure -which is my real goal. – Sql Surfer Jan 18 '19 at 17:37
  • The Irony. I am 100 percent Azure except for my on prem server farm that exists only for my Azure apps to come back down to from the cloud over a VPN to do file access and MOVE operations between directories on the same drive and share. I am PaaS - we like having zero VM's in Azure. – Sql Surfer Jan 18 '19 at 17:41
3

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);
}
Ryan Langton
  • 6,294
  • 15
  • 53
  • 103
1

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.

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
David Makogon
  • 69,407
  • 21
  • 141
  • 189
  • "_With normal file I/O operations_" <- are you saying that ... using `System.File.IO` .. I can use methods from that namespace? I'm actually using the official Storage SDK and using methods from that, though and want to stick with the Storage SDK library. – Pure.Krome Sep 19 '16 at 14:23
  • @Pure.Krome - yes - that's exactly what I'm saying. To quote from [this article](https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-files/) : "*You can issue standard file commands from the command prompt, or view the mounted share and its contents from File Explorer. You can also run code within the virtual machine that accesses the file share using standard Windows file I/O APIs, such as those provided by the System.IO namespaces in the .NET Framework.*" – David Makogon Sep 19 '16 at 14:30
  • Sweet - I thought you said that. All of what you said is for when you're trying to access the Files Storage via a mount .. which is a virtual disk (which is SMB). That's not what i'm trying to do. I don't have a mount. Nor do I want one. I _can't_ use anything from `System.File.IO` because I don't have a local mount. Secondly, even if I did have that, SMB is slower than using the REST endpoints (ref: http://stackoverflow.com/questions/37581971/azure-file-storage-smb-slow-to-list-files-in-directory). So - I'm trying to figure out how to do this via the SDK (as the OP mentions). – Pure.Krome Sep 19 '16 at 14:37
  • Ok. So just to be clear: an SMB mount isn't the same as a virtual disk (vhd). And your original question is tagged as Azure File. So I may have misinterpreted the question. Sorry about that. – David Makogon Sep 19 '16 at 14:40
  • Np. And yeah, and I ment virtual disk .. like c/d/z, etc. (abstract concept) . .. not a real VHD. again, sorry for the poor use of words. – Pure.Krome Sep 19 '16 at 14:46
  • Have you looked into REST API for Azure Files? https://msdn.microsoft.com/en-us/library/azure/mt427372.aspx – Mine Tanrinian Demir - MSFT Sep 19 '16 at 15:43
-1

Azure blob sub directors are a virtual feature in that they don't physically exist, the name of the blob/file contains the full path. Because of that you don't have to explicitly "create" the directory.

I don't think that an atomic "rename" method exists for Azure blobs/files... To get around it you would have to copy (with the new name) and then delete the original.

Scott Perham
  • 2,410
  • 1
  • 10
  • 20
  • 1
    This guidance applies to blobs directly, not to Azure File Storage (which implements an SMB share on top of blob storage, and is what the OP was specifically asking about). – David Makogon Sep 19 '16 at 14:15
  • I have just tried with an Azure File Storage, and Renaming the File with the destination directory in the file path caused it to appear as expected inside that directory on Windows Explorer on my laptop and also on my Azure Portal Storage Account's explorer. https://learn.microsoft.com/en-us/dotnet/api/azure.storage.files.shares.sharefileclient.rename – Tony Mar 29 '23 at 19:09