簡體   English   中英

Azure 函數計時器觸發器是否可以迭代容器或目錄中的所有 blob?

[英]Can an Azure Function timer trigger iterate all blobs in a container or directory?

是否可以使用定期觸發的函數(定時器觸發器)遍歷和操作 Azure 存儲容器中的所有 blob。

路徑:{容器名稱}/{目錄名稱}/{文件名稱}

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace SampleNamespace
{
    public static class SampleFunction
    {
        [FunctionName("SampleFunction")]
        public static void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log)
        {
            // How to iterate an operate on all blobs?
        }
    }
}

您應該為 azure 功能安裝此 blob 存儲 nuget 包Microsoft.Azure.Storage.Blob 然后你可以使用像ListBlobs這樣的sync method而不是async method ListBlobsSegmentedAsync 最后,您可以編寫代碼來操作這些 blob。

代碼如下:

using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System;

namespace FunctionApp7
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            log.LogInformation("the blobs list:");

            var connectionString = "DefaultEndpointsProtocol=https;AccountName=xx;AccountKey=xxxx;EndpointSuffix=core.windows.net";
            var containerName = "test4";
            var directoryName = "sub1";

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            CloudBlobContainer container = blobClient.GetContainerReference(containerName);

            //list blobs in a container
            var blobs = container.ListBlobs(useFlatBlobListing: true);

            foreach (IListBlobItem item in blobs)
            {
                if (item.GetType() == typeof(CloudBlockBlob))
                {
                    CloudBlockBlob blob = (CloudBlockBlob)item;
                    //other operation.
                }
            }

            //list blobs in a directory
            CloudBlobDirectory directory = container.GetDirectoryReference(directoryName);
            var blobs_2 = directory.ListBlobs(useFlatBlobListing: true);

            foreach(IListBlobItem item in blobs_2)
            {
                if (item.GetType() == typeof(CloudBlockBlob))
                {
                    CloudBlockBlob blob = (CloudBlockBlob)item;
                    //other operation.
                }
            }


        }
    }
}

除了使用存儲 blob sdk(Ivan 提供)的方式,您可以使用 blob 綁定來獲取容器客戶端,然后執行列表操作。

你可以參考blob輸入用法,你會發現它支持綁定CloudBlobContainerCloudBlobDirectory類型。 你可以參考下面的代碼。

[FunctionName("Function1")]
public static async System.Threading.Tasks.Task RunAsync([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log,
            [Blob("test",Connection = "AzureWebJobsStorage")]CloudBlobContainer container)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            var segment = await container.ListBlobsSegmentedAsync(null);
            var blobs = segment.Results;
            foreach (var blob in blobs) {

                log.LogInformation(blob.GetType().ToString(), blob.ToString());
            }
        }

如果會返回所有的blob和blob目錄,下面是我的測試結果,所以需要判斷返回類型是CloudBlockBlob還是CloudBlobDirectory然后進行列表操作。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM