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

后端 未结 6 1268
梦谈多话
梦谈多话 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:10

    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.

    0 讨论(0)
  • 2020-12-20 17:12

    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.

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2020-12-20 17:17

    Like this:

    public static void MoveTo(this CloudFile source, CloudFileDirectory directory)
    {
        var target = directory.GetFileReference(source.Name);
        target.StartCopy(source);
        source.Delete();
    }
    
    0 讨论(0)
  • 2020-12-20 17:22

    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.

    0 讨论(0)
  • 2020-12-20 17:27

    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.

    0 讨论(0)
提交回复
热议问题