WebJob触发器的Blob路径名提供程序

时间:2016-01-15 12:04:53

标签: azure azure-storage azure-storage-blobs azure-webjobs azure-webjobssdk

我有一个放在WebJob项目中的以下测试代码。在“cBinary / test1 /”存储帐户中创建(或更改)任何blob后触发它。

代码有效。

public class Triggers
{
    public void OnBlobCreated(
        [BlobTrigger("cBinary/test1/{name}")] Stream blob, 
        [Blob("cData/test3/{name}.txt")] out string output)
    {
       output = DateTime.Now.ToString();
    }
}

问题是:如何摆脱丑陋的硬编码const字符串“cBinary / test1 /”和“”cData / test3 /“?

硬编码是一个问题,但我需要创建并维护一些动态创建的字符串(blob目录) - 取决于支持的类型。更重要的是 - 我需要在几个地方使用这个字符串值,我不想复制它。

我希望将它们放在某种配置提供程序中,例如,根据某些枚举构建blob路径字符串。

怎么做?

1 个答案:

答案 0 :(得分:7)

您可以实施INameResolver动态解析QueueNames和BlobNames。您可以添加逻辑来解析那里的名称。下面是一些示例代码。

public class BlobNameResolver : INameResolver
{
    public string Resolve(string name)
    {
        if (name == "blobNameKey")
        {
            //Do whatever you want to do to get the dynamic name
            return "the name of the blob container";
        }
    }
}

然后你需要在Program.cs

中将其连接起来
class Program
{
    // Please set the following connection strings in app.config for this WebJob to run:
    // AzureWebJobsDashboard and AzureWebJobsStorage
    static void Main()
    {
        //Configure JobHost
        var storageConnectionString = "your connection string";
        //Hook up the NameResolver
        var config = new JobHostConfiguration(storageConnectionString) { NameResolver = new BlobNameResolver() };

        config.Queues.BatchSize = 32;

        //Pass configuration to JobJost
        var host = new JobHost(config);
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }
}

最后在Functions.cs

public class Functions
{
    public async Task ProcessBlob([BlobTrigger("%blobNameKey%")] Stream blob)
    {
        //Do work here
    }
}

还有一些信息here.

希望这会有所帮助。