Skip to main content
Adaptive Planning
Last Updated: 2023-06-23
Set Up and Configure an SFTP

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

Navigation

Compass.png From the nav menu, go to
Integration
Design Integrations

Set Up and Configure an SFTP

Create a new SFTP connection

  1. Create an
    SFTP Connection
    from the
    Credentials
    pane in Design Integrations.
  2. Enter a name for the connection and click
    Create
    .
  3. 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
  4. Choose your authentication method.
    • For private key authentication, no password is required.
    • For basic authentication, enter a password.
  5. For private key authentication, send the public key to the client server administrator for installation.
  6. Select
    Test Connection
    for the first time.
For private key or basic authentication:
  • When you select
    Test Connection
    for the first time, the
    Fingerprint
    field populates. This enables server verification for future connections. The client server administrator can verify the
    Fingerprint
    confirming that you are connected to the right server.
  • After running
    Test Connection
    for the first time, click your SFTP connection in the
    Credentials
    pane to reload the connection and view the fingerprint field.
  • When you select
    Test Connection
    or 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 from
Data Sources
in Design Integrations.
Add an SFTP Connection to the Custom Cloud Data Source
  1. Select the Designer Settings tab for your CCDS.
  2. Drag and drop the
    SFTP Connection
    parameter from
    Additional Settings
    in the
    Settings Component
    pane.
  3. 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
  1. 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.
  2. Test your Connection from the
    Actions
    pane to run your script. You can find find test results of your script in
    Logs
    in the
    Actions
    pane.

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 client
    client.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(); } }