Set Up and Configure an SFTP
You can create a client to connect to your Secure File Transfer Protocol (SFTP) server and perform various operations on your files. This article explains how to add an SFTP Connection to the Custom Cloud Data Source (CCDS) and how to include the SFTP connection in your script. See Set up PGP Encryption for information about encrypting and decrypting your files.
Before you Begin
- New to CCDS? See Concept: CCDS Overview and Set up a Custom Cloud Data Source (CCDS)
- Required Permissions:Integration DeveloperwithinAdministration > Permission Set
Navigation
From the nav menu, go to Set Up and Configure an SFTP
Create a new SFTP connection
- Create anSFTP Connectionfrom thepane in Design Integrations.Credentials
- Enter a name for the connection and clickCreate.
- Enter the host expression in the connection form. Connections use port 22 if you don't specify them. We support ports:
- 22
- 1224
- 2222
- 5022
- 5031
- 5122
- 10022
- 20022
- Choose your authentication method.
- For private key authentication, no password is required.
- For basic authentication, enter a password.
- For private key authentication, send the public key to the client server administrator for installation.
- SelectTest Connectionfor the first time.
For private key or basic authentication:
- When you selectTest Connectionfor the first time, theFingerprintfield populates. This enables server verification for future connections. The client server administrator can verify theFingerprintconfirming that you are connected to the right server.
- After runningTest Connectionfor the first time, click your SFTP connection in theCredentialspane to reload the connection and view the fingerprint field.
- When you selectTest Connectionor use the SFTP connection in a script, the fingerprint from the server must match or the connection fails.
- Select your Custom Cloud Data Source
- Select a CCDS or create a CCDS fromData Sourcesin Design Integrations.
- Add an SFTP Connection to the Custom Cloud Data Source
- Select the Designer Settings tab for your CCDS.
- Drag and drop theSFTP Connectionparameter fromAdditional Settingsin theSettings Componentpane.
- Configure the SFTP connection in the Designer Settings tab. The setting name is the handle you will use inside your script. For example, 'sftpserver'.
- Add the SFTP Connection in your Script
- Create a client using ai.sftp.createClient() and provide the name of your SFTP setting. For example:const client = ai.sftp.createClient('mysftp').After connecting successfully, a client object is created.
- Test your Connection from theActionspane to run your script. You can find find test results of your script inLogsin theActionspane.
SFTP methods for File Upload and Download
Use the following methods to create a client, upload and download encrypted files to an SFTP server and encrypt or decrypt them with PGP. See ai.sftp.
- createClient(sftpConnection)client = ai.sftp.createClient('mysftp');
- uploadFile(Writer, uploadedFileName)client.uploadFile(writer,'./archive/datasample.csv.asc');
- downloadFile(filename)client.downloadFile('./archive/datasample.csv.asc');
- Disconnect the clientclient.disconnect();
- Free the system resources consumed by the file Reader object.reader.cleanUp();
Example to Connect and Download files from an SFTP server
function testConnection(context) { let client; try { client = ai.sftp.createClient('mysftp'); } catch(e) { ai.log.logError('An error occurred connecting to the server: ' + e); return false; } finally { if (client) client.disconnect(); } return true; } function importData(context) { const rowset = context.getRowset(); const client = ai.sftp.createClient('mysftp'); const reader = client.downloadFile('./someData.csv'); try { reader.readLine(); let line = reader.readLine(); while (line) { let cells = line.split(','); rowset.addRow(cells); line = reader.readLine(); } } catch(e) { ai.log.logError('An error occurred downloading the file: ' + e); } finally { client.disconnect(); reader.cleanUp(); } }