Skip to main content
Indiana Wesleyan University Support Knowledge Base

SFTP Methods

Overview

Lists available options and methods for transferring files over SFTP using ExportManager.

Details

FTP methods become available when your export inherits the FtpExport subclass.  Inherit this subclass if your export will need to use FTP functionality to deliver its files to a remote vendor.

FTP functionality is achieved using the WinSCP NuGet package.

Setup

FtpOptions

This property is a SessionOptions object and most of the following properties are wrapping its properties for convenience.  It contains other properties not listed here you may wish to make use of.

FtpEnumerationOptions

Flags Enum used representing a set of EnumarionOptions values.  See Session.EnumerationRemoteFiles.  This had to be duplicated because the Session is not created until an operation occurs, and is consequently hidden.

UserName

String value for username to use when logging in to the remote server

HostName

String value of the remote server host name

PortNumber

Integer value of the remote server host port to which connection should be made.
Default:  depends on Protocol

Protocol

Enum value representing a WinSCP.Protocol value.  See SessionOptions Class.  The enum is duplicated as IWU.ExportManager.FtpProtocol because including WinSCP libraries directly in your script causes compiler problems that are extremely difficult to work around.
Default:  FtpProtocol.Sftp

SshHostKeyFingerprint

String value of the Host fingerprint for the remote server (for security purposes).  If the remote server key changes, WinSCP will no longer connect to it until you update it with the correct key.  This is to prevent the remote server address being redirected to a malicious FTP server without your knowledge.

SshPrivateKeyFile

String value indicating the path to and file name of a private key file to use when connecting to the remote server

SecurePassword

Write-only string property used to set the login password to the remote server.  This property wraps the SecurePassword property of SessionOptions which is a .NET Framework SecureString object that cannot easily be written-to or read-from.

filesUploaded

Integer value indicating the number of files that successfully uploaded to the remote server

filesFailed

Integer value indicating the number of files that failed to upload to the remote server

Execution

FtpLoginTest()

This method returns true or false indicating whether a successful connection and login could be made to the remote server.

UploadFromFiles()

This method uploads all files in the working set to the specified path on the remote server mirroring the files and directories within the working set in that location.

/// <summary>
/// Uploads the working set of files to the destination server from disk
/// </summary>
/// <param name="remotePath">Remote path to place working file set in on the SFTP server. Example: /subdir</param>
/// <param name="localPath">Local subdirectory of the working set to send to the SFTP server.</param>
public void UploadFromFiles( string remotePath="/", string localPath = "" )

UploadFromBuffer()

This method is not implemented.

ListRemoteFiles()

              This method will get a list of files on the remote system as a List<String>.  The string in the list is the full path.

/// <summary>
/// Get a List of remote file names.    
/// </summary>
/// <param name="path">Full path to root remote directory to start enumeration in.</param>
/// <param name="mask">Windows wildcard to filter files. To select all files, use null.</param>
/// <param name="options">Enumeration options set
///    *FtpEnumerationOptions.None - Enumerate matching files only. Do not recurse.
///    *FtpEnumerationOptions.AllDirectories - Recurse into subdirectories.
///    *FtpEnumerationOptions.MatchDirectories - Enumerate also matching directories. Cannot 
///         be combined with FtpEnumerationOptions.EnumerateDirectories. 
///    *FtpEnumerationOptions.EnumerateDirectories - Enumerate also all directories. 
///          Must be used with FtpEnumerationOptions.AllDirectories. 
///          Cannot be combined with FtpEnumerationOptions.MatchDirectories.
/// </param>
/// <returns>A List of remote files names as strings representing matched files (and directories, if EnumerationOptions.MatchDirectories or EnumerationOptions.EnumerateDirectories option is used).
///          or an empty list if session could not connect.
/// </returns>
/// <exception cref="System.ArgumentException">Thrown when options has an invalid combination</exception>
public List<string> ListRemoteFiles(string path, string mask, FtpEnumerationOptions options)

Example

Script

This example imports some files into the working set, checks if it can log in to the remote server and logs an error if not.  Then it uploads the files and directories in the working set (regardless of whether the login test succeeded) and finally archives the working set of files.

public FtpSample( string[] args ) : base( args )
{
    // SFTP options
    HostName              = "localhost";
    UserName              = "apptest";
    SecurePassword        = "test123";
    SshHostKeyFingerprint = "ssh-rsa 3072 10:45:9c:8c:18:19:3b:d6:63:5a:14:97:d2:d2:dd:f0";
    SshPrivateKeyFile     = "mykey.ppk";
    Protocol              = FtpProtocol.Sftp;
}

public override void Run()
{
    // Import some files into a subdir called extras in the working set - wildcards supported
    ImportFiles( "extras", false, null, @"C:\Users\Public\Pictures\Sample Pictures\*s.jpg" );

    // Upload files via SFTP
    if ( !FtpLoginTest() )
        log.Error( "Uh oh!" );

    // Files will appear in /SampleExportUploads/extras/ on the remote server
    UploadFromFiles( "/SampleExportUploads/" );

    // Zip up files and keep the past 30 days in the archive directory
    ArchiveFiles( daysToKeep: 30, filesToKeep: 0 );
}

 Log output

Expected ExportManager.log output:

2016-09-14 13:49:50.0248 INFO -------------------------------------------------------------------------------
2016-09-14 13:49:50.0918 INFO IWU Export Manager launched
2016-09-14 13:49:50.1108 INFO -------------------------------------------------------------------------------
2016-09-14 13:49:50.1228 TRACE OS=Microsoft Windows NT 6.1.7601 Service Pack 1  Cores=4  64bit=True
    Machine=IWU71563  Domain=IWUNET  User=konrad.willmert
    Command=ExportManager  FtpSample
-------------------------------------------------------------------------------
2016-09-14 13:49:50.1678 DEBUG Export "BasicSample" found
2016-09-14 13:49:50.2058 DEBUG Export "ColleagueSample" found
2016-09-14 13:49:50.2238 DEBUG Export "ColleagueTest" found
2016-09-14 13:49:50.2418 DEBUG Export "FileSample" found
2016-09-14 13:49:50.2558 DEBUG Export "FtpSample" found
2016-09-14 13:49:50.2738 DEBUG Export "SampleExport" found
2016-09-14 13:49:51.7308 INFO Executing "FtpSample"
2016-09-14 13:49:59.1878 DEBUG "FtpSample" export process executed in 8,894ms
2016-09-14 13:49:59.2078 DEBUG IWU.ExportManager.Exports.FtpSample FileManager@C:\Users\konrad.willmert\Source\Repos\IWUExportManager\IWUExportManager\bin\Release\data\FtpSample: 0 data files, 0 other files ColleagueConnection mocha.task@DEV with database "coll18_dev" and HOLD dir "\\ERPapp\d$\datatel\ftp\coll18_dev\data\_HOLD_" targeting Sftp://apptest@localhost:0 with fingerprint: ssh-rsa 3072 10:45:9c:8c:18:19:3b:d6:63:5a:14:97:d2:d2:dd:f0 using key file: mykey.ppk
2016-09-14 13:49:59.2278 DEBUG ExportManager total run time: 9,203ms
2016-09-14 13:49:59.2348 INFO Exiting. 

Expected FtpSample.log output:

2016-09-14 13:49:51.8138 INFO -------------------------------------------------------------------------------
2016-09-14 13:49:51.8198 INFO IWU.ExportManager.Exports.FtpSample initializing.
2016-09-14 13:49:51.8198 INFO -------------------------------------------------------------------------------
2016-09-14 13:49:52.5848 WARN TEST MODE is enabled. No actual data files will be exported/uploaded. Instead, write access will be tested. Override with "notest".
2016-09-14 13:49:52.6048 DEBUG Data directory: C:\Users\konrad.willmert\Source\Repos\IWUExportManager\IWUExportManager\bin\Release\data\FtpSample
2016-09-14 13:49:52.6048 DEBUG Archive directory: C:\Users\konrad.willmert\Source\Repos\IWUExportManager\IWUExportManager\bin\Release\archive\FtpSample
2016-09-14 13:49:52.6238 INFO Importing file(s) into working set.
2016-09-14 13:49:52.6238 DEBUG Created directory: C:\Users\konrad.willmert\Source\Repos\IWUExportManager\IWUExportManager\bin\Release\data\FtpSample\extras
2016-09-14 13:49:52.6468 DEBUG Copied "C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg" to "C:\Users\konrad.willmert\Source\Repos\IWUExportManager\IWUExportManager\bin\Release\data\FtpSample\extras\Hydrangeas.jpg"
2016-09-14 13:49:52.6468 DEBUG Copied "C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg" to "C:\Users\konrad.willmert\Source\Repos\IWUExportManager\IWUExportManager\bin\Release\data\FtpSample\extras\Penguins.jpg"
2016-09-14 13:49:52.6758 DEBUG Copied "C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg" to "C:\Users\konrad.willmert\Source\Repos\IWUExportManager\IWUExportManager\bin\Release\data\FtpSample\extras\Tulips.jpg"
2016-09-14 13:49:52.6808 INFO Successfully copied 3 file(s).
2016-09-14 13:49:52.6808 DEBUG Successfully copied 1,994,007 bytes in 3 file(s) in 52ms
2016-09-14 13:49:52.7008 INFO Connecting to FTP server: localhost
2016-09-14 13:49:56.1118 INFO Successfully connected FTP server "localhost" as "apptest"
2016-09-14 13:49:56.2438 INFO Uploading working set to SFTP server.
2016-09-14 13:49:56.2588 DEBUG Connecting to FTP server: localhost
2016-09-14 13:49:58.2248 INFO Successfully uploaded: C:\Users\konrad.willmert\Source\Repos\IWUExportManager\IWUExportManager\bin\Release\data\FtpSample\_test.txt
2016-09-14 13:49:58.2358 INFO Successfully uploaded 1 files in 1,961ms
2016-09-14 13:49:58.5658 INFO Archiving working set.
2016-09-14 13:49:59.1398 DEBUG Archive created: C:\Users\konrad.willmert\Source\Repos\IWUExportManager\IWUExportManager\bin\Release\archive\FtpSample\FtpSample 2016-09-14 13.49.58.zip
2016-09-14 13:49:59.1718 DEBUG Working set deleted.
2016-09-14 13:49:59.1718 INFO Expiring old archives. 
  • Was this article helpful?