Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 757
Pure.Krome Avatar asked Sep 19 '16 14:09

Pure.Krome


People also ask

How do I move files from one directory to another in Azure?

Select the Azure Blob storage and click on continue. Select the source file format, in my case it is CSV file, then click on continue. Then give a name to the dataset, then select linked service that we have created before, then select source folder path, then select none for import schema, and then click on ok.

How do I move folders in Azure?

In Source Control Explorer, select the item that you want to move, open its shortcut menu, and choose Move. In the Move dialog box, either manually type the destination for the item in the To box, or choose Browse to use the Browse for Folder dialog box. Choose OK.


3 Answers

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 image 109
Ryan Langton Avatar answered Sep 19 '22 00:09

Ryan Langton


Like this:

public static void MoveTo(this CloudFile source, CloudFileDirectory directory)
{
    var target = directory.GetFileReference(source.Name);
    target.StartCopy(source);
    source.Delete();
}
like image 40
Jerry Nixon Avatar answered Sep 21 '22 00:09

Jerry Nixon


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.

like image 24
sguler Avatar answered Sep 19 '22 00:09

sguler