如何迭代CloudBlobDirectory复制数据?

问题描述 投票:0回答:1

我写了下面的代码来遍历第二代存储Blob

 CloudStorageAccount sourceAccount = CloudStorageAccount.Parse(sourceConnection);
 CloudStorageAccount destAccount = CloudStorageAccount.Parse(destConnection);
 CloudBlobClient sourceClient = sourceAccount.CreateCloudBlobClient();
 CloudBlobClient destClient = destAccount.CreateCloudBlobClient();
 CloudBlobContainer sourceBlobContainer = sourceClient.GetContainerReference(sourceContainer);

 // Find all blobs that haven't changed since the specified date and time
 IEnumerable<ICloudBlob> sourceBlobRefs = FindMatchingBlobsAsync(sourceBlobContainer, transferBlobsNotModifiedSince).Result;



    private static async Task<IEnumerable<ICloudBlob>> FindMatchingBlobsAsync(CloudBlobContainer blobContainer, DateTime transferBlobsNotModifiedSince)
    {
        List<ICloudBlob> blobList = new List<ICloudBlob>();
        BlobContinuationToken token = null;

        // Iterate through the blobs in the source container
        do
        {
            BlobResultSegment segment = await blobContainer.ListBlobsSegmentedAsync(prefix: "", currentToken: token);
            foreach (CloudBlobDirectory VARIABLE in segment.Results)
            {
                BlobResultSegment segment2 = await VARIABLE.ListBlobsSegmentedAsync(currentToken: token);
                foreach (CloudBlobDirectory VARIABLE2 in segment2.Results)//Bad coding
                {
                    //how do I get children count ?
                }
            }

        }while (token != null);
     }

这将仅迭代2个级别,但不会动态迭代直到内部级别。我在下面的层次结构中有斑点

  --Container
    --FolderA
      --FolderAA
        --FolderAA1
          --File1.txt
          --File2.txt              
        --FolderAA2
          --File1.txt
          --File2.txt
        --FolderAA3
     --FolderAB
       --File8.txt
     --FolderAC
       --File9.txt

此层次结构是动态的

如何循环和复制Blob内容。

注:我不想使用CLI命令进行复制。因为一旦开始复制,我将没有任何控制权。

更新

在这里找到一些样本:https://csharp.hotexamples.com/examples/Microsoft.WindowsAzure.Storage.Blob/CloudBlobContainer/ListBlobsSegmented/php-cloudblobcontainer-listblobssegmented-method-examples.html

c# azure blob
1个回答
1
投票

请参阅下面的示例代码:

class Program
{
    static void Main(string[] args)
    {
        var storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true");
        var client = storageAccount.CreateCloudBlobClient();
        var container = client.GetContainerReference("test");
        var blobs = FindMatchingBlobsAsync(container).GetAwaiter().GetResult();
        foreach (var blob in blobs)
        {
            Console.WriteLine(blob.Name);
        }
        Console.WriteLine("-------------------------------------");
        Console.WriteLine("List of all blobs fetched. Press any key to terminate the application.");
        Console.ReadKey();
    }

    private static async Task<IEnumerable<ICloudBlob>> FindMatchingBlobsAsync(CloudBlobContainer blobContainer)
    {
        List<ICloudBlob> blobList = new List<ICloudBlob>();
        BlobContinuationToken token = null;

        // Iterate through the blobs in the source container
        do
        {
            BlobResultSegment segment = await blobContainer.ListBlobsSegmentedAsync(prefix: "", useFlatBlobListing: true, BlobListingDetails.None, 5000, token, new BlobRequestOptions(), new OperationContext());
            token = segment.ContinuationToken;
            foreach(var item in segment.Results)
            {
                blobList.Add((ICloudBlob)item);
            }
        } while (token != null);
        return blobList;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.