An extension for WebJobs which uses FluentFTP to support:
- Ftp Trigger on a new
FtpFile
,FtpFile[]
,FtpStream
andFtpStream[]
. - Ftp Bindings on:
IAsyncCollector
,IFtpClient
,FtpFile
andFtpStream
.
You can install from NuGet using the following command in the package manager window:
Install-Package WebJobs.Extensions.Ftp
Or via the Visual Studio NuGet package manager or if you use the dotnet
command:
dotnet add package WebJobs.Extensions.Ftp
- Custom Azure Function Extension - Part 1 - Triggers
- Custom Azure Function Extension - Part 2 - Bindings
Add a FtpConnection
property with the url to the FTP server.
{
"Values": {
"FtpConnection": "ftp://testuser:mypwd@localhost"
}
}
[FunctionName("FtpTriggerFtpFile")]
public static void RunFtpTriggerFtpFile(
[FtpTrigger(Connection = "FtpConnection", Folder = "inbox", PollingInterval = "30s")] FtpFile ftpItem,
ILogger log)
{
log.LogInformation($"{ftpItem.GetType()} {ftpItem.Name} {ftpItem.FullName} {ftpItem.Size} {ftpItem.Content?.Length}");
}
[FunctionName("FtpTriggerFtpStream")]
public static async Task RunFtpTriggerFtpStream(
[FtpTrigger(Connection = "FtpConnection", Folder = "inbox")] FtpStream ftpStream,
ILogger log)
{
log.LogInformation($"{ftpStream.GetType()} {ftpStream.Name} {ftpStream.FullName} {ftpStream.Size} {ftpStream.Stream?.Length}");
}
[FunctionName("FtpBindingFtpFile")]
public static void RunBindingFtpFile(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
[Ftp(Connection = "FtpConnection", Folder = "inbox")] out FtpFile item,
ILogger log)
{
string msg = req.Query["message"];
log.LogInformation($"Received message {msg}");
item = new FtpFile
{
Name = "stef1.txt",
Content = Encoding.UTF8.GetBytes(msg)
};
}
[FunctionName("FtpBindingFtpStream")]
public static void RunBindingFtpStream(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
[Ftp(Connection = "FtpConnection", Folder = "inbox")] out FtpStream item,
ILogger log)
{
string msg = req.Query["message"];
log.LogInformation($"Received message {msg}");
item = new FtpStream
{
Name = "stef1.txt",
Stream = new MemoryStream(Encoding.UTF8.GetBytes(msg))
};
}
[FunctionName("BindingIAsyncCollector")]
public static async Task RunBindingIAsyncCollector(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
[Ftp(Connection = "FtpConnection", Folder = "inbox")] IAsyncCollector<FtpFile> collector,
ILogger log)
{
string msg = req.Query["message"];
log.LogInformation($"Received message {msg}");
var item = new FtpFile
{
Name = "stef2.txt",
Content = Encoding.UTF8.GetBytes(msg)
};
await collector.AddAsync(item);
}
The following example shows how to combine a trigger and a binding to process and delete a file.
The IFtpClient
is exposed here, which allows full access to the FTP server.
[FunctionName("FtpTriggerSampleWithClient")]
public static void RunFtpTriggerSampleWithClient(
[FtpTrigger(Connection = "FtpConnection", Folder = "inbox", PollingIntervalInSeconds = 30, IncludeContent = false)] FtpFile ftpItem,
[Ftp(Connection = "FtpConnection", Folder = "inbox")] IFtpClient client,
ILogger log)
{
// Do some processing with the FtpFile ...
client.DeleteFile(ftpItem.FullName);
}