如何绑定到容器以遍历Blob

时间:2019-07-07 23:08:43

标签: c# .net azure-functions

是否可以遍历容器内部的斑点?

当前,我添加了此属性:

[Blob("%MyFunc:InputContainer%")]CloudBlobContainer inputContainer

但是,我还没有找到有关如何遍历inputContainer内部blob的任何文档。

3 个答案:

答案 0 :(得分:1)

下面是该示例的基本示例。

#r "Microsoft.WindowsAzure.Storage"

using System;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.Extensions.Logging;

public static void Run(Stream myBlob, CloudBlobContainer container,ILogger log)
{
log.LogInformation($"Container name: {container.Name}");
var blob= container.GetBlockBlobReference("Bill.pdf");
log.LogInformation($"Blob size: {blob.StreamWriteSizeInBytes}");
log.LogInformation($"C# Blob trigger function processed {myBlob}");

}

function.json

{
"bindings": [
{
"connection": "AzureWebJobsStorage",
"path": "samples-workitems/{name}",
"name": "myBlob",
"type": "blobTrigger",
"direction": "in"
},
{
"name": "container",
"type": "blob",
"path": "output-images",
"connection": "AzureWebJobsStorage",
"direction": "in"
}
],
"disabled": false
}

功能。项目

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>  
<ItemGroup>
    <PackageReference Include="WindowsAzure.Storage" Version="9.3.3"/>
    <PackageReference Include="Newtonsoft.Json" Version="11.0.2"/>
</ItemGroup>

答案 1 :(得分:1)

您应该可以使用inputContainer.ListBlobsSegmentedAsync()

BlobResultSegment blobResultSegment = await container.ListBlobsSegmentedAsync(null);

// Iterate each blob
foreach (IListBlobItem item in blobResultSegment.Results)
{
    // cast item to CloudBlockBlob
    CloudBlockBlob blob = (CloudBlockBlob)item;
}

答案 2 :(得分:1)

您可以使用ListBlobsSegmentedAsync获取Blob。我将容器与此绑定:[Blob("firstcontainer")]CloudBlobContainer inputContainer

并使用以下代码来获取blob列表:

            BlobContinuationToken blobContinuationToken = null;
            var results = await inputContainer.ListBlobsSegmentedAsync(null, blobContinuationToken);

            foreach (IListBlobItem item in results.Results)
            {
                log.LogInformation(item.Uri.Segments.Last());
            }

这是我的测试结果,您可以尝试一下。带有/的Blob名称表示它是目录

enter image description here

希望这可以为您提供帮助。