简体   繁体   中英

how to bind to a container in order to iterate through blobs

Is it possible to iterate through the blobs inside of a container?

Currently, I've added this attribute:

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

However, I've not found any documentation on how to iterate through the the blobs inside of the inputContainer .

Below is the basic sample for this.

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

function.Proj

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

You should be able to use 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;
}

You could use ListBlobsSegmentedAsync to get blobs. I bind the container with this : [Blob("firstcontainer")]CloudBlobContainer inputContainer .

And with below code to get the blob list:

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

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

This is my test result, you could have a try. The blob name with / means it's a directory .

在此处输入图片说明

Hope this could help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM