【问题标题】:listing all blobs in container列出容器中的所有 blob
【发布时间】:2018-06-10 21:22:31
【问题描述】:

我正在尝试为容器中的所有 blob 创建一个函数。我接受了帮助 How to get hold of all the blobs in a Blob container which has sub directories levels(n levels)?,它似乎使用了一个不再存在的重载。我在附加字段 prefix 和 operationContext 中添加了默认值:

static internal async Task<List<string>> ListBlobNames(string ContainerName)
{

    BlobContinuationToken continuationToken = null;
    bool useFlatBlobListing = true;
    BlobListingDetails blobListingDetails = BlobListingDetails.None;
    var blobOptions = new BlobRequestOptions();
    CloudBlobContainer container = Container(ContainerName);
    var operationContext = new OperationContext();
    var verify = container.GetBlobReference("A_Valid_Name.jpg");
    var verify2 = container.GetBlobReference("NotAName.jpg");

    using (var a = await verify.OpenReadAsync()) ;
    //using (var a = await verify2.OpenReadAsync());   // doesn't work since it doesn't exist

    return (await container.ListBlobsSegmentedAsync("", useFlatBlobListing, blobListingDetails, null, continuationToken, blobOptions, operationContext))
            .Results.Select(s => s.Uri.LocalPath.ToString()).ToList();
}

最后一行给了我一个例外:

StorageException:请求的 URI 不代表服务器上的任何资源。

然后我创建了verfiy 和verify2 变量来测试我的容器是否有效。 verify 引用了一个有效的 blob,verify2 引用了一个无效的 blob 名称。使用未注释的第二个 using 语句运行代码在第二个 using 语句中给我一个错误。这表明verify 有效,因此容器有效。

【问题讨论】:

  • 那么...您确定您正确复制了代码吗?您的注释行 not 引用 verify2 - 它引用 refer2(未定义的变量)。
  • 谢谢。为了清楚起见,我更改了变量名称。我忘了重命名时代码被注释了
  • 有人能解释一下否决票吗?有什么要补充的吗?

标签: asp.net-core azure-storage azure-blob-storage


【解决方案1】:

这对我有用...

String myContainerName = "images";

CloudBlobContainer blobContainer = blobClient.GetContainerReference(myContainerName);

CloudBlockBlob blockBlob; 

blockBlob = blobContainer.GetBlockBlobReference("NotAName.jpg");

bool verify2 = await blockBlob.ExistsAsync();
if (!verify2)
{
 // the blob image does not exist
 // do something 
}

【讨论】:

  • 验证部分是为了确保容器正常工作的测试。这不是问题所在。我试图列出 blob
【解决方案2】:

我正在尝试为容器中的所有 blob 创建一个函数。

您可以利用 Azure 存储客户端库并安装包 WindowsAzure.Storage,然后您可以按照教程 List blobs in pages asynchronously 来实现您的目的。为了测试,我刚刚创建了我的 .Net Core 控制台应用程序,如下所示:

static void Main(string[] args)
{
    MainAsync(args).GetAwaiter().GetResult();
}

static async Task MainAsync(string[] args)
{   
    // Retrieve storage account from connection string.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse("{your-storage-connection-string}");

    // Create the blob client.
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    string containerName = "images";
    // Retrieve reference to a previously created container.
    CloudBlobContainer container = blobClient.GetContainerReference(containerName);

    if (await container.ExistsAsync())
        await ListBlobsSegmentedInFlatListing(container);
    else
        Console.WriteLine($"Your container with the name:{containerName} does not exist!!!");

    Console.WriteLine("press any key to exit...");
    Console.ReadLine();
}

ListBlobsSegmentedInFlatListing:

async public static Task ListBlobsSegmentedInFlatListing(CloudBlobContainer container)
{
    //List blobs to the console window, with paging.
    Console.WriteLine("List blobs in pages:");

    int i = 0;
    BlobContinuationToken continuationToken = null;
    BlobResultSegment resultSegment = null;

    //Call ListBlobsSegmentedAsync and enumerate the result segment returned, while the continuation token is non-null.
    //When the continuation token is null, the last page has been returned and execution can exit the loop.
    do
    {
        //This overload allows control of the page size. You can return all remaining results by passing null for the maxResults parameter,
        //or by calling a different overload.
        resultSegment = await container.ListBlobsSegmentedAsync("", true, BlobListingDetails.All, 10, continuationToken, null, null);
        if (resultSegment.Results.Count<IListBlobItem>() > 0) { Console.WriteLine("Page {0}:", ++i); }
        foreach (var blobItem in resultSegment.Results)
        {
            Console.WriteLine("\t{0}", blobItem.StorageUri.PrimaryUri);
        }
        Console.WriteLine();

        //Get the continuation token.
        continuationToken = resultSegment.ContinuationToken;
    }
    while (continuationToken != null);
}

测试:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-18
    • 2015-11-10
    • 2019-07-28
    • 1970-01-01
    • 2021-04-24
    • 2022-12-17
    • 2019-07-21
    • 2015-12-07
    相关资源
    最近更新 更多