Dynamic output file name of ApiHubFile Azure Function binding (one drive, drop box etc)(ApiHubFile Azure 函数绑定的动态输出文件名(一个驱动器,投递箱等))
问题描述
我有一个带有计时器触发器的 Azure 函数,然后我想生成一个具有动态(在运行时定义)名称和内容的文件并将其保存到例如OneDrive.
I have an Azure Function with Timer Trigger, and then I want to generate a file with dynamic (defined in runtime) name and contents and save it to e.g. OneDrive.
我的功能码:
public static void Run(TimerInfo myTimer, out string filename, out string content)
{
filename = $"{DateTime.Now}.txt";
content = $"Generated at {DateTime.Now} by Azure Functions";
}
还有function.json
:
{
"bindings": [
{
"name": "myTimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 */5 * * * *"
},
{
"type": "apiHubFile",
"name": "content",
"path": "{filename}",
"connection": "onedrive_ONEDRIVE",
"direction": "out"
}
],
"disabled": false
}
虽然失败了,但
Error indexing method 'Functions.TimerTriggerCSharp1'. Microsoft.Azure.WebJobs.Host:
Cannot bind parameter 'filename' to type String&. Make sure the parameter Type
is supported by the binding. If you're using binding extensions
(e.g. ServiceBus, Timers, etc.) make sure you've called the registration method
for the extension(s) in your startup code (e.g. config.UseServiceBus(),
config.UseTimers(), etc.).
推荐答案
你可以这样做:
#r "Microsoft.Azure.WebJobs.Extensions.ApiHub"
using System;
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host.Bindings.Runtime;
public static async Task Run(TimerInfo myTimer, TraceWriter log, Binder binder)
{
log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
var fileName = "mypath/" + DateTime.Now.ToString("yyyy-MM-ddThh-mm-ss") + ".txt";
var attributes = new Attribute[]
{
new ApiHubFileAttribute("onedrive_ONEDRIVE", fileName, FileAccess.Write)
};
var writer = await binder.BindAsync<TextWriter>(attributes);
var content = $"Generated at {DateTime.Now} by Azure Functions";
writer.Write(content);
}
还有function.json
文件:
{
"bindings": [
{
"name": "myTimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "10 * * * * *"
},
{
"type": "apiHubFile",
"name": "outputFile",
"connection": "onedrive_ONEDRIVE",
"direction": "out"
}
],
"disabled": false
}
您实际上并不需要 function.json
中的 apiHubFile
声明,但由于我今天注意到的一个错误,它应该仍然存在.我们会修复这个错误.
You shouldn't really need the apiHubFile
declaration in your function.json
but because of a bug I noticed today it should still be there. we will fix that bug.
这篇关于ApiHubFile Azure 函数绑定的动态输出文件名(一个驱动器,投递箱等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ApiHubFile Azure 函数绑定的动态输出文件名(一个驱动器,投递箱等)
基础教程推荐
- 如何激活MC67中的红灯 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01