1

我是 azure web 函数的新手,我似乎无法在同一个项目中找到有关多个触发器的任何文档。我已经创建了我的解决方案,并且已经创建了一个很好的TimerTrigger,它工作正常。

此触发器从 ftp 目录下载文件,然后将文件上传到我们的 azure 存储帐户。代码如下所示:

[DependencyInjectionConfig(typeof(DependencyInjectionConfig))]
public class FtpTrigger
{
    [FunctionName("EOrderTimerTrigger")]
    public static async Task Run(
        [TimerTrigger("*/15 * * * * *")]TimerInfo myTimer, 
        TraceWriter log,
        [Inject]IConfig config,
        [Inject]SmartFileClient smartFileClient,
        [Inject]SettingsHandler settingsHandler,
        [Inject]StorageHandler storageHandler)
    {
        if (myTimer.IsPastDue)
            log.Info("Timer is running late!");

        log.Info($"Listing directories from { config.FtpUrl }");

        var accounts = await smartFileClient.ListDirectories(config.FtpPath);

        if (!accounts.Any())
        {
            log.Warning($"There are no files waiting to be processed for any account");
            return;
        }

        foreach (var account in accounts)
        {
            try
            {
                log.Info($"Retrieving settings for { account }");
                var url = $"{config.ApiBaseUrl}/{config.ApiSettingsPath}";
                var settings = await settingsHandler.GetAsync(url, account);

                log.Info($"Find all order files for { account }");
                var fileNames = await smartFileClient.ListFiles($"{config.FtpPath}/{account}", settings.OrderFileSuffix.Replace(".", ""));

                if (!fileNames.Any())
                {
                    log.Warning($"No files to process for { account }");
                    continue;
                }

                log.Info($"Get a list of files awaiting to be processed for { account }");
                var awaiting = await storageHandler.ListAsync(config.StorageProcessingContainer, account);

                foreach(var fileName in fileNames)
                {
                    log.Info($"Finding any files awaiting to be processed in the storage account for { account }");
                    var friendlyName = Regex.Replace(fileName, "[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled); ;
                    var match = awaiting.Any(m => m.Equals(friendlyName));
                    if (match)
                    {
                        log.Warning($"File ({fileName}) already awaiting to be processed for { account }");
                        continue;
                    }

                    log.Info($"Download { fileName } from the ftp directory for { account }");
                    var bytes = await smartFileClient.DownloadFile($"{config.FtpPath}/{account}", fileName);

                    log.Info($"Upload { fileName } to the Azure Storage account for { account }");
                    await storageHandler.UploadAsync(friendlyName, bytes, config.StorageProcessingContainer, account);

                    log.Info($"Delete { fileName } from the ftp directory for { account }");
                    if (!await smartFileClient.DeleteFile($"{config.FtpPath}/{account}", fileName))
                        log.Error($"Failed to delete { fileName } from the ftp directory for { account }");
                }

            } catch (Exception ex)
            {
                log.Error($"{ ex.Message }");
            }
        }

        log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
    }
}

现在我想添加第二个触发器。这个触发器将是一个BlobTrigger。我将它添加到我的项目中并运行它,即使创建了文件,它也从未触发过。所以我意识到我一定做错了什么。

有人能告诉我如何在一个项目中有多个触发器吗?如果做不到;什么是替代方案?

4

1 回答 1

0

实际上,您可以在同一个 azure 函数项目中添加多个触发器。

我将它添加到我的项目中并运行它,即使创建了文件,它也从未触发过。

你可以关注这篇文章

[FunctionName("Function2")]
        public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "blobconnect")]Stream myBlob, string name, TraceWriter log)
        {
            log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
        }

注意:blob 触发器路径 samples-workitems/{name} 中的字符串 {name} 创建了一个绑定表达式,您可以在函数代码中使用它来访问触发 blob 的文件名。有关详细信息,请参阅本文后面的 Blob 名称模式。

你只需要修改samples-workitems你的container name. local.hosting.json并在文件中设置 blob 连接。

于 2018-06-01T10:00:15.487 回答