如何迭代CloudBlobDirectory复制数据? [英] How do I iterate CloudBlobDirectory to copy the data?

查看:49
本文介绍了如何迭代CloudBlobDirectory复制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

此层次结构是动态的

如何循环和复制Blob内容.

How do I loop and copy the blob content.

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

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

更新

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

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

推荐答案

请参见下面的示例代码:

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

这篇关于如何迭代CloudBlobDirectory复制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆