问题描述
我正在尝试使用 Azure 函数将文件保存到 FTP.json是这样的:
I am trying to save files to FTP by using an Azure Function. The json is this:
{
"type": "apiHubFile",
"name": "outputFile",
"path": "{folder}/ps-{DateTime}.txt",
"connection": "ftp_FTP",
"direction": "out"
}
功能代码是这样的:
public static void Run(string myEventHubMessage, TraceWriter log, string folder, out string outputFile)
{
var model = JsonConvert.DeserializeObject<PalmSenseMeasurementInput>(myEventHubMessage);
folder = model.FtpFolderName;
outputFile = $"{model.Date.ToString("dd.MM.yyyy hh:mm:ss")};{model.Concentration};{model.Temperature};{model.ErrorMessage}";
log.Info($"C# Event Hub trigger Save-to-ftp function saved to FTP: {myEventHubMessage}");
}
我得到的错误是这样的:
The error I get is this:
函数 ($SaveToFtp) 错误:Microsoft.Azure.WebJobs.Host:错误索引方法'Functions.SaveToFtp'.Microsoft.Azure.WebJobs.Host:无法将参数文件夹"绑定到字符串类型.确定参数绑定支持类型.如果您使用绑定扩展(例如 ServiceBus、Timers 等)确保您已调用启动代码中扩展的注册方法(例如config.UseServiceBus()、config.UseTimers() 等).
Function ($SaveToFtp) Error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.SaveToFtp'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'folder' 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.).
如果我将 {folder} 替换为文件夹名称,它会起作用:
If I replace {folder} with a folder name it works:
"path": "psm/ps-{DateTime}.txt"
为什么?难道不能从代码中改变路径吗?
Why? Is it not possible to change the path from the code?
推荐答案
文件夹
是你的函数的输入参数,它不会影响输出绑定.
folder
is an input parameter of your function, it can't affect the ouput binding.
{folder}
语法的意思是运行时会尝试在您的输入项中找到 folder
属性,并绑定到它.
What {folder}
syntax means is that the runtime will try to find folder
property in your input item, and bind to it.
因此请尝试以下方法:
public static void Run(PalmSenseMeasurementInput model, out string outputFile)
{
outputFile = $"{model.Date.ToString("dd.MM.yyyy hh:mm:ss")};{model.Concentration};{model.Temperature};{model.ErrorMessage}";
}
带有function.json
:
{
"type": "apiHubFile",
"name": "outputFile",
"path": "{FtpFolderName}/ps-{DateTime}.txt",
"connection": "ftp_FTP",
"direction": "out"
}
您可以阅读更多这里,在绑定表达式和模式"和绑定到绑定表达式中的自定义输入属性"部分.
You can read more here, in "Binding expressions and patterns" and "Bind to custom input properties in a binding expression" sections.
这篇关于Azure Functions 错误 - 无法将参数绑定到字符串类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!