This library simplifies the task of adding background processing to your Microsoft Azure Functions and WebJobs. This SDK supports triggering a function when a new Ftp fits triggering a function le is uploaded. In addition these FtpBindings are supported: IAsyncCollector, IFtpClient, FtpFile and FtpStream.
$ dotnet add package WebJobs.Extensions.FtpAn extension for WebJobs which uses FluentFTP to support:
FtpFile, FtpFile[], FtpStream and FtpStream[].IAsyncCollector, IFtpClient, FtpFile and FtpStream.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);
}
Entity Framework Extensions and Dapper Plus are major sponsors and proud to contribute to the development of WebJobs.Extensions.Ftp.