How do I iterate CloudBlobDirectory to copy the data?

爷,独闯天下 提交于 2021-02-11 13:44:35

问题


I have written below code to iterate through the Gen2 storage 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);
     }

This will iterate only 2 levels but not dynamically till the inner levels. I have blob in below hierarchy

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

This hierarchy is dynamic

How do I loop and copy the blob content.

Note: I do not want to use CLI commands to copy. Because I won't have any control once copy started.

Update

Found some samples here: https://csharp.hotexamples.com/examples/Microsoft.WindowsAzure.Storage.Blob/CloudBlobContainer/ListBlobsSegmented/php-cloudblobcontainer-listblobssegmented-method-examples.html


回答1:


Please see the sample code below:

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


来源:https://stackoverflow.com/questions/61793980/how-do-i-iterate-cloudblobdirectory-to-copy-the-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!