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

后端 未结 6 1287
梦谈多话
梦谈多话 2020-12-20 16:58

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         


        
6条回答
  •  甜味超标
    2020-12-20 17:13

    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);
    }
    

提交回复
热议问题