且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何迭代CloudBlobDirectory复制数据?

更新时间:2023-11-27 19:11:22

请参见下面的示例代码:

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