Developers » Scripting Code Samples » FTP in Scripting


FTP in Scripting

Quick Links

Notice: We are currently working to improve this content. Once revisions are finalized, we will add the article to the Recently Updated Articles section on the product's Documentation Home page. Learn more.

We enable you to use FTP to upload and download data from Workflow and we support these protocols: 

  • SFTP
  • FTP
  • FTPS

Code to connect to the FTP site for upload, download, moving files and closing a connection is the same for all protocols. The protocol to be used is defined in these parameters: 

  • server
  • username
  • password
  • port: default 21
  • protocol: FTP || FTPS || SFTP, default FTP
  • authentication: PASSWORD || PRIVATE_KEY, default PASSWORD
  • encryption: SSL || PASSIVE, default PASSIVE
  • mode: ACTIVE || PASSIVE, default PASSIVE
  • isImplicit: default false

A basic case might require the user to configure the server, username, and password, if the other defaults are acceptable. When you don't provide a parameter, we use the default value.

The following examples are provided for SFTP.

SFTP Using Private Key

[Back to top]

This option only exists for the SFTP protocol.

Define SFTP Parameters as a Map

[Back to top]

SFTP parameters can be defined in the script as a Map <String, String>

def params = [
"server" : "", 
"protocol": "SFTP",
"port": "22",
"username" : "root",
"authentication": "PRIVATE_KEY"

Define SFTP Parameters in Custom Parameters

[Back to top]

Workflow enables you to create custom parameters by going to Setup > System Settings > Organization Settings and clicking the Custom Parameters tab. Once you configure your custom parameter, you can access it using a script. 

For the following example: 

Parameter Name Example
mySFTP.protocol SFTP
mySFTP.port 22
mySFTP.username root
mySFTP.authentication PRIVATE_KEY

You can use the following code to access the parameter:
// prefix of ftp parameters
def ftpClientName = "mySFTP"

Access PPK Key

[Back to top]

A PPK key is stored as an attachment on a case and you can use the following script to access the key:

// ppk file is attached to a case
def caze = resp.getCase("Authentification-Private-1");
def ppkFile = null;

for (def att : caze.getAttachments()) {
if (att.getName().endsWith("ppk")) {
ppkFile = att.getFile();

logger.debug('PPK Key', ppkFile);
logger.debug('Workflow Online Help Example', '\n');

PPK Key is Defined in Script

[Back to top]

One script will hold the PPK key, such as in the following example: 

Script system_id: ppk_holder

Code Example: 

return '''PuTTY-User-Key-File-2: ssh-rsa
Encryption: none
Comment: rsa-key-20150312
Public-Lines: 6
Private-Lines: 14
Private-MAC: eb2b013e3f98af5798569d4aab471aa399cf521f
'''.replace('\n', '\r\n');
// replace() is needed because of how groovy handles multi line strings. Possibly could be different for other keys

Use the following code in the script in which you are working with SFTP:

def privateKey = resp.script.execute('ppk_holder');
def ppkFile = resp.newFile('privateKey.ppk', privateKey);

Connect to SFTP Using the PPK Key

[Back to top]

Connect to SFTP using the map and PPK file using the following code line: 

def ftpClient = resp.ftpConnect(params, ppkFile);

Connect to SFTP Using ftpClientName and the PPK File

[Back to top]

def ftpClient = resp.ftpConnect(ftpClientName, ppkFile);

Working with SFTP  

Upload and Download Files

[Back to top]

def pdfFile = null;

def caze = resp.getCase("Approvals-Europe-123");

for (def att : caze.getAttachments()) {
if (att.getName().endsWith("pdf")) {
pdfFile = att.getFile();

def file2 = ftpClient.upload(pdfFile, "/root/sftp_test/pedja", "pdf_test.pdf", true, true);
if (file2 == null) {
logger.debug('Upload file', 'Upload failed!\n\r');
} else {
logger.debug('Upload file', 'Upload successful!\n\r');
def file3 ="/root/sftp_test/pedja", "pdf_test.pdf");

ftpClient.upload(file3, "/root/sftp_test/pedja", "pdf_test_copy.pdf", true, true);
if (file3 != null) {
logger.debug('Downoladed file', 'Downloaded file: ' + file3.toString() + '\n\r');
} else {
logger.debug('Uploaded file', 'file3 is null');

Move Files

[Back to top]

ftpClient.move("/home/pmaric/Public/testing", "test_file.txt", "/home/pmaric/Public/testing/test3", "MOVED - test_file.txt",
true, true);

// When the overwrite and createDirectories parameters are not explicitly provided in the method call, assumed values are false 
ftpClient.move("/home/pmaric/Public/testing", "test_file_1.txt", "/home/pmaric/Public/testing/test3", "MOVED - test_file_1.txt");

Close Connection

[Back to top]


Important: It is best practice to close your connection at the end of your FTP session.

Related Article