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
CreateIfNotExistsAsync
for each sub-directory, first?cheers!
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.
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.
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();
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With