Azure Blob Trigger – 根据输入容器动态BlobOutput绑定名称

huangapple go评论55阅读模式
英文:

Azure Blob Trigger - Dynamic BlobOutput Binding Name Based on Input Container

问题

        [Function("Function1")]
        [BlobOutput("{input-container-name}-output/{name}", Connection = "ConnectionString1")]

        public string Run([BlobTrigger("test-samples-trigger/{name}", Connection = "ConnectionString1")] string myBlob,
            string name, string blobTrigger)
        {
            _logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} \n Data: {myBlob}");
            
            return myBlob;
        }
英文:
        [Function("Function1")]
        [BlobOutput("test-samples-output/{name}", Connection = "ConnectionString1")]

        public string Run([BlobTrigger("test-samples-trigger/{name}", Connection = "ConnectionString1")] string myBlob,
            string name, string blobTrigger)
        {
            _logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} \n Data: {myBlob}");
            
            return myBlob;
        }

I have a blob trigger set to 'test-samples-trigger/{name}'. I want to set the BlobOutput to use the input container name '{input-container-name}-output/{name}'. Is there a way to set the BlobOuput string to dynamically point to this location?

答案1

得分: 1

以下是翻译好的部分:

"After reproducing from my end, One way to achieve your requirement is to read variable using GetEnvironmentVariable where the value is read from local.settings.json. Below is the complete code that worked for me.

Function1.cs

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

namespace FunctionApp13
{
    public class Function1
    {
        [FunctionName("Function1")]
        public void Run([BlobTrigger("samples-workitems/{name}", Connection = "connstr")]Stream myBlob,
            [Blob("%outputContainer%/{name}", FileAccess.Write, Connection = "connstr")] Stream outputBlob, 
            string name, ILogger log)
        {
            log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");

            string outputContainer = Environment.GetEnvironmentVariable("outputContainer");

            outputBlob.Write();
        }
    }
}

local.settings.json

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "<ConnectionString>",
        "connstr": "<ConnectionString>",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "outputContainer": "sample"
    }
}

Results:

Azure Blob Trigger – 根据输入容器动态BlobOutput绑定名称

Azure Blob Trigger – 根据输入容器动态BlobOutput绑定名称"

英文:

After reproducing from my end, One way to achieve your requirement is to read variable using GetEnvironmentVariable where the value is read from local.settings.json. Below is the complete code that worked for me.

Function1.cs

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

namespace FunctionApp13
{
    public class Function1
    {
        [FunctionName(&quot;Function1&quot;)]
        public void Run([BlobTrigger(&quot;samples-workitems/{name}&quot;, Connection = &quot;connstr&quot;)]Stream myBlob,
            [Blob(&quot;%outputContainer%/{name}&quot;, FileAccess.Write, Connection = &quot;connstr&quot;)] Stream outputBlob, 
            string name, ILogger log)
        {
            log.LogInformation($&quot;C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes&quot;);

            string outputContainer = Environment.GetEnvironmentVariable(&quot;outputContainer&quot;);

            outputBlob.Write();
        }
    }
}

local.settings.json

{
    &quot;IsEncrypted&quot;: false,
  &quot;Values&quot;: {
    &quot;AzureWebJobsStorage&quot;: &quot;&lt;ConnectionString&gt;&quot;,
    &quot;connstr&quot;: &quot;&lt;ConnectionString&gt;&quot;,
    &quot;FUNCTIONS_WORKER_RUNTIME&quot;: &quot;dotnet&quot;,
    &quot;outputContainer&quot;: &quot;sample&quot;
  }
}

Results:

Azure Blob Trigger – 根据输入容器动态BlobOutput绑定名称

Azure Blob Trigger – 根据输入容器动态BlobOutput绑定名称

答案2

得分: 0

local.settings.json

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "YOUR_AZURE_STORAGE_CONNECTION_STRING",
        "ContainerName": "mycontainer"  // 默认容器名称
    }
}
英文:

local.setting.json

{
      &quot;IsEncrypted&quot;: false,
      &quot;Values&quot;: {
        &quot;AzureWebJobsStorage&quot;: &quot;YOUR_AZURE_STORAGE_CONNECTION_STRING&quot;,
        &quot;ContainerName&quot;: &quot;mycontainer&quot;  // Default container name
      }
    }

using System.Configuration;

public static void ProcessBlob(
    [BlobTrigger(&quot;%ContainerName%/{name}&quot;, Connection = &quot;AzureWebJobsStorage&quot;)] Stream myBlob,
    string name)
{
    // Your code here
}

huangapple
  • 本文由 发表于 2023年2月14日 19:23:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定