Class wwSftpClient

This class provides SFTP (Secure FTP over SSH) support for connecting to SFTP servers.

DO wwSftpClient
DO wwutils  && for UI helpers only

*** Using Rebex Tiny SFTP Server for local testing
lcServer = "127.0.0.1"
lnPort = 23
lcUsername = "tester"
lcPassword = "password"

CLEAR

? "***  wwSftpClient Test***"
loFtp  = CREATEOBJECT("wwsFtpClient")
loFtp.cLogFile = "c:\temp\ftp.log" && verbose log - leave empty
loFtp.lIgnoreCertificateErrors = .T.   && self-signed cert not installed

loFtp.cServer = lcServer
loFtp.nPort = lnPort
loFtp.cUsername = lcUsername
loFtp.cPassword = lcPassword


*** Optional Progress Events - clas below
loFtp.oProgressEventObject = CREATEOBJECT("SFtpClientProgressEvents")


IF !loFtp.Connect()
	? loFtp.cErrorMsg
	RETURN
ENDIF
? "Connected to " + lcServer	


IF !loFtp.DownloadFile("SubFolder/Sail-Big.jpg", "c:\temp\Sail-Big.jpg")
	? loFtp.cErrorMsg
	RETURN
ENDIF	
? "Downloaded " + "SubFolder/Sail-Big.jpg"

lcUploadFile = "SubFolder/Sail-Big-UPLOADED" + SYS(2015) + ".jpg"
IF !loFtp.UploadFile("c:\temp\Sail-Big.jpg", lcUploadFile)
	? loFtp.cErrorMsg
	RETURN
ENDIF
? "Uploaded " + lcuploadFile


*** provide a folder name (no wildcards)
loCol = loFtp.ListFiles("/SubFolder")
IF ISNULL(loCol)
   ? loFtp.cErrorMsg
   RETURN
ENDIF

? TRANSFORM(loCol.Count ) + " matching file(s)"
? loFtp.cErrorMsg
FOR EACH loFile IN loCol FOXOBJECT
   ? loFile.Name
   
   IF ( AT("Sail-Big-UPLOADED",loFile.Name) = 1)
	   *? loFtp.oBridge.ToJson(loFile)  && for kicks print out as json
	   IF loFtp.DeleteFile(loFile.FullName)
	      ? "*** Deleted " + loFile.FullName
	   ENDIF
   ENDIF
ENDFOR

loFtp.Close()
RETURN


DEFINE class SFtpClientProgressEvents as Custom

*** You can set this to .T. to abort an upload or download
lCancelDownload = .F.

FUNCTION OnFtpBufferUpdate(lnPercent, lnDownloadedBytes, lcRemotePath, lcMode)
  lcMsg = lcMode + ": " + TRANSFORM(lnPercent) + "% complete. " + lcRemotePath + " - " + TRANSFORM(lnDownloadedBytes) + " bytes"
  ? "*** " + lcMsg
ENDFUNC

ENDDEFINE 

Remarks

Dependencies

This API uses a .NET library to provide FTP functionality.

  • wwIpstuff.dll
  • wwDotnetBridge.dll
  • Renci.sshNet.dll

Class Members

MemberDescription

OnFtpBufferUpdate

OnFtpBufferUpdate

Function: Event that fires whenever the buffer is updated by downloadfile or uploadfile. Pass: Return: lcMode: upload download

ChangeDirectory

Changes the active directory on the FTP server. This affects how relative paths are handled.

o.ChangeDirectory(lcFtpPath)

Close

Closes the FTP connections.

o.Close()

Connect

This establishes the initial connection to the server. The connection needs to stay open for any of the FTP operation commands.

o.Connect(lcServer, lcUsername, lcPassword)

CreateDirectory

Creates a new directory on the server.

o.CreateDirectory(lcFtpPath)

DeleteDirectory

Deletes a directory from the FTP server.

o.DeleteDirectory(lcFtpPath)

DeleteFile

Deletes a file from the server.

o.DeleteFile(lcFile)

DownloadFile

Downloads a file from the FTP server to a local file.

o.DownloadFile(lcFtpPath, lcTargetFile)

ExecuteCommand

FtpCommand

Function: Issue an arbitrary FTP Command Assume: Connection must exist with FtpOpen Pass: Return:

o.ExecuteCommand(lcCommand)

ExecuteDownloadCommand

ExecuteDownloadCommand

Function: Issue an arbitrary FTP Command Assume: Connection must exist with FtpOpen Pass: Return:

o.ExecuteDownloadCommand(lcCommand)

Exists

Checks to see if a remote file exists.

o.Exists(lcRemoteFile)

ListFiles

Retrieves a directory listing from the server with all files returned as a collection.

o.ListFiles(lcFtpPath)

RenameFile

Renames a file on the server which allows for fast copying.

o.RenameFile(lcOldName, lcNewName)

UploadFile

Uploads a file to the server from a local file.

o.UploadFile(lcLocalFile, lcFtpPath)

cErrorMsg

Error message set after a method call that fails

cftpserver

The FTP server to connect to

cftpsource

The Source file to upload from the client or download from the server.

cftptarget

The file to download to from the server or upload to on the server.

cHttpProxyByPass

cHttpProxyName

cHttpProxyPassword

cHttpProxyPort

cHttpProxyUsername

cLogFile

Optional log file that receives ftp log output

cPassword

Password for the connection

cPrivateKeyFile

Optional Private key file for sign on

cPrivateKeyFilePassword

Optional Private key file password for sign on

cUsername

Username for the connection

lcanceldownload

Cancel flag that can be set in OnFTPBufferUpdate to let wwFTP know to stop downloading a file from the Internet.

lEnableProgressEvents

lIgnoreCertificateErrors

Allows you to ignore certificate errors NOT RECOMMENDED: Fix your Certs!

lpassiveftp

Flag that sets whether to use passive FTP.

lUseTls

Tsl used for this request (FTPS protocol: FTP over TLS)

nftpworkbuffersize

nPort

The port to run the FTP connection on

nTimeout

Connection timeout in seconds

oBridge

wwDotnetBridge instance

oFtpClient

.NET wwFtpClient instance

oProgressEventObject

Requirements

Assembly: wwsftpclient.prg

© West Wind Technologies, 1996-2024 • Updated: 06/25/24
Comment or report problem with topic