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 -> \\\\Sh
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.
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.
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);
}
Like this:
public static void MoveTo(this CloudFile source, CloudFileDirectory directory)
{
var target = directory.GetFileReference(source.Name);
target.StartCopy(source);
source.Delete();
}
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.
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.