SFTP Example using PeopleCode
By Chris Malek | Wed, Mar 14, 2012
Starting in PeopleTools 8.51, there is native support for sftp (secure) for putting and getting files from an sftp server. There were some key improvements to the URL page that need to be setup properly. Additionally, you have to pass the attachment functions a url object so the password decryption will happen correctly. This will make sense shortly.
Setup the URL
First we need to setup the url object correctly. First we setup the url with an “sftp://” scheme.
Then we use the URL Properties sub-page to set some important properties on the URL. In this example we are using a password authentication scheme to the sftp server. There is some support for key-based authentication.
NOTE: It seems that these new user name and password fields do not migrate with projects when you migrate the URL.
PeopleCode SFTP Example
Now lets look at a simple Application Engine PeopleCode example that will do the following:
- Get the current directory where the application engine is keeping its working files.
- See the How to get files to show up in the Process Monitor for an explanation.
- Create a simple “hello world” file
- using sftp, we will “put” the file on the server.
- If the url definition had some other scheme like ftps or https this same code would work.
Local File &f; Local string &directory; SQLExec("SELECT PRCSOUTPUTDIR FROM PS_CDM_LIST WHERE PRCSINSTANCE = :1", PT_OPT_AET.PROCESS_INSTANCE, &directory); Local string &fileName = "superImportantFile.txt"; &f = GetFile((&directory | "/" | &fileName), "w", %FilePath_Absolute); &f.WriteLine("Hello World @ " | %Datetime); &f.Close(); /* The Important thing here is to pass the URL object to putattachment */ /* which causes some magic to happen with password decryption */ /* and the file transfer */ Local integer &return = PutAttachment(URL.NU_FILE_ATTACHMENTS, &fileName, (&directory | "/" | &fileName)); If &return = 0 Then /* file should have go through */ Else /* something went wrong */ End-If;