Share:
Developers » Scripting Code Samples » FTP in Scripting

PREVIOUS  |  NEXT

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" : "128.199.41.251", 
"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.server 128.199.41.251
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
AAAAB3NzaC1yc2EAAAABJQAAAQEAxSzWW4W2c1tvwfyRNYXJu5lid0hVo201HfdM
ew8P+1iICUWHjuX5IN0012fBuIoXd/wL740MSvU1OeXX5Hqi+yeTkjuqoo7QrqBs
4SC0PMbYVU2LMjWNiwvTmDq4tVTwBshrybS524YNvn3nusemgE9CbXF40A6tHmoM
7Av+r15sI4xg9Y3Y/BBfeOERAdfksieERanijOzrHXs/34lAH/X0wrdK7r/GSnQx
fFhs1IonZEHZFslgTGfPzQ0LM1VR60ntnHphmxEemkhMRtMIBqr7UUJfaOXic/4t
TtposaL3wwwyR5nu5r+u6BB6K7x/vk6pCsqFX9UG5nslJ7QpEw==
Private-Lines: 14
AAABAB/5bt5otc2Ecvzc2UbzGctk+zXwDeMmd1D3q4mTvWcHcAGA5YzLWNTlmd29
zGMdT+nxhWUdyqRfHWNVtE6QbXTV/EE6G6uodN4MLVTwi+4uFT5D7Q8PmmmTNxHE
VU3WbBzGQ5EkOseriDEsueEtislpzIBzRAYQN7++K5vmRPnXrPp3ev3Iys1jJvUo
dsLyisK2ozQLd4uaduaOZ+ThlGGfV9p7ULuxpjoaynJMEYmJyI9GXf2th2gb8vrh
z8KhQs7TiRuCPIVO+FZ4WP92OZYBMrJdvd6gL3c14oquLU8UhCngqI0N2ZqRrXU/
KELQbfEurmse+riBwGD1UCOTs20AAACBAOVoO5YvJZGDPWE8ZvQHwTtQ6pqx/F/L
hOphBhYd4hVOZb4Zq2sdfg9C+ZZUxVMhc8UOpdHyB6jjpYCtSNZBcHEYhW/otq
oxM0kECRwiQuCHr+xkadqNNeVi8PJHoAVXXxT1tD9j+rre2QnlI9rlQF9Gs4SkCp
/yVveArToFORAAAAgQDcCBqpoWhk25o+e9qlbZ5wvqd+yij7OdojhAb+aOLCd47u
OxNuIm0YWIV2zMi0Wdu7XxRVx/wneoKQaecKyw7Jk3VCcS4LpQcP+yc8OGKAu5sd
Bo9bQ7aVC9uz6IPXSaRSz34c8yOcAeS+WcIE/LOFvmUKPevlRAsUdm+sI1pYYwAA
AIAPk1AYAJL3S2bCBhEyPpasdx7i4VuiH6beVMfwYBRQ1bvOJ8Bf3YjerwqcMWe
VOpnSeNu3w27YgS0y3cmt6/jjHMHnxFhQ2MpD3v1ylXcx3U+k+Ep487xQxdQo8E4
S2NB5jP91f74Srv6uMqXvcdaYTRONCLobBoyXj6EyzIzCw==
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 = ftpClient.download("/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]

ftpClient.close(); 


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


Related Article