SFTP Downloader: A Comprehensive Guide
Foreword
In today’s reconciliation system development, sales orders are often paid first from various platforms, followed by reconciliation with the company’s store order data. This process requires a reliable and efficient way to retrieve payment data from different sources. While real-time data interfaces like Webservice, NVP, and RESTful API are relatively straightforward to implement, SFTP file download services pose a more significant challenge. To address this issue, we need to meet three essential conditions: support for password authentication, secure key authentication, and the ability to retrieve documents based on file name policies.
Choosing the Right SFTP Client Library
After conducting research, we discovered a comprehensive and easy-to-use SFTP client library: SFTP Downloader. This library, produced by ComponentPro, offers a powerful set of tools for developing SFTP-related applications. In addition to providing FTP client/server and SFTP client/server libraries, ComponentPro also offers Mail, Excel, SAML, and PDF libraries.
How to Package the SFTP Downloader
To implement the SFTP Downloader, we need to use the library and meet the three requirements mentioned earlier. The code structure for this implementation is as follows:
public class FileNamingStrategy : IFileNamingStrategy
{
public string GetFileRegexName()
{
return "test.csv";
}
}
How to Use the SFTP Downloader
To use the SFTP Downloader, follow these steps:
- Install the package: Install the
SFTP.Downloaderpackage using the following command:
Install-Package SFTP.Downloader -Version 1.0.2
**Note:** When using the package in actual applications, use the latest version.
2. **Define the file naming policy**: Create a class that implements the `IFileNamingStrategy` interface, such as `FileNamingStrategy`. This class should contain the logic for retrieving files based on file name policies.
3. **Configure the SFTP services**: Use the `AddSFtpServices` method to configure the SFTP services, including the naming strategy and authentication scheme. Choose one of the two authentication schemes: password or secure key.
* **Password authentication scheme**: Use the `UsePwdAuthTScheme` method to configure the password authentication scheme.
* **Secure key authentication scheme**: Use the `UseSecurityKeyAuthTScheme` method to configure the secure key authentication scheme.
4. **Inject the configured SFTP services**: Use the `AddSFtpServices` method to inject the configured SFTP services into the application.
5. **Use the file downloader**: Create an instance of the `IFilesDownloader` interface and use the `DownloadAsync` method to download files from the SFTP server.
**Example Usage**
Here's an example of how to use the SFTP Downloader:
```csharp
public class Engine
{
private readonly ILogger _logger;
private readonly IFilesDownloader _downloader;
public Engine(ILogger<Engine> logger, IFilesDownloader downloader)
{
_logger = logger;
_downloader = downloader;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
var files = await _downloader.DownloadAsync(1);
_logger.LogInformation($"The files downloaded: {files.Aggregate((x, y) => $"{x}, {y}")}");
}
}
You can also pass parameters options, such as:
var files = await _downloader.DownloadAsync(1, options: new SFtpOptions()
{
Host = "sftp-host",
UserName = "userName",
Password = "pwd",
RemoteDirectory = "/upload"
});
Conclusion
In this article, we introduced an implementation of SFTP file download using the SFTP Downloader library. We met the basic needs of password authentication, secure key authentication, and file name policies, and then encapsulated the SFTP file download into a simple and easy-to-use library. Finally, we showed how to use this library in actual applications.