通过azure函数以同步方式同时处理多个文件

我有一个Azure函数,该函数从blob容器中提取名称和文件URL,然后将该信息发送到另一个函数来处理这些文件(将它们解压缩并保存在Datalake中)

对于斑点的扩张:

string storageConnectionString = @"myconnstring";
CloudStorageaccount storageaccount = CloudStorageaccount.Parse(storageConnectionString);
CloudBlobClient blobClient = storageaccount.CreatecloudBlobClient();
CloudBlobContainer container = blobClient.getcontainerReference("Container");

IEnumerable<IListBlobItem> blobs = new IListBlobItem[0];

foreach (IListBlobItem blobItem in container.ListBlobs())
     {                
        if (blobItem is CloudBlobDirectory)
           {
              CloudBlobDirectory directory = (CloudBlobDirectory)blobItem;
              blobs = directory.ListBlobs(true);                    
           }

      }

 await ProcessBlobs(blobs); 

以及处理灯泡的功能:

public static async Task ProcessBlobs(IEnumerable<IListBlobItem> blobs)
    {

        var tasks = blobs.Select(currentblob =>
        {
            string FileUrl = currentblob.Uri.ToString();
            string FileName = currentblob.Uri.Segments.Last();
            //string content = "{ \"fileUri\": \""+ currentblob.Uri.ToString()+ "\",\"fileName\": \""+ currentblob.Uri.Segments.Last()+"\"}";

            var values = new Dictionary<string,string>
                {
                    { "fileUri",currentblob.Uri.ToString() },{ "fileName",currentblob.Uri.Segments.Last() }
                };
            var content = new FormUrlEncodedContent(values);
            string baseURL = @"https://<afu>.azurewebsites.net/api/process_zip_files_by_http_trigger?code=45"; ;
            //string urlToInvoke = string.Format("{0}&name={1}",baseURL,FileUrl,FileName);


            return RunAsync(baseURL,content);
        });
        await Task.WhenAll(tasks);
    }

    public static async Task RunAsync(string i_URL,FormUrlEncodedContent content)
    {
        var response = await client.PostAsync(i_URL,content);
        var responseString = await response.Content.ReadAsStringAsync();
        log.info(responseString);
    }

函数RunAsync异步处理文件。

我的问题是: 通常可以以同步方式并行处理blob吗?您是否有更好,更简单的想法来实现我的目标?

hanjunwei88122188 回答:通过azure函数以同步方式同时处理多个文件

这是Durable Functions的最佳用例之一。具体来说,Fan-Out/Fan-In Pattern

您总共需要4个功能

  1. GetBlobList Activity Function
    在这里可以获取要处理的Blob列表。您只会得到斑点列表,而不是实际的斑点。

  2. ProcessBlob Activity Function
    此函数采用Blob路径,获取Blob并对其进行处理。

  3. Orchestrator Function
    该函数调用GetBlobList函数,循环返回的Blob路径列表,并为每个Blob路径调用ProcessBlob函数。

  4. 启动器功能(Client Function
    此功能仅触发编排的运行。这通常是HTTP触发的函数。

如果您不熟悉耐用功能,则最好遍历quickstart doc以了解所需的不同类型的功能。

本文链接:https://www.f2er.com/3153687.html

大家都在问