Skip to main content

SFTP

SSH File Transfer Protocol (SFTP) gives you a secure file transfer capability. Wrapped around SSH2 which provides a high level convenience abstraction as well as a Promise based API.

Tagsshell
Network Connection needs

This integration needs network access to the server where the service is running.

See the Network access page for details about how to achieve that.


tip

This integration works with SSH File Transfer Protocol (SFTP). If you need to use another FTP protocol, take a look at our FTP integration.

Credential configuration

To configure this credential, you need the host, port and username to connect to the FTP server. Then you'll need a password or the private key depending on how you want to connect.

Optionally, you can set any of the extra config parameters you can see here.

Here is an example of a filled credential configuration form in YepCode:

SFTP Snippets available in Editor

note

The title is the triggering text for YepCode to autocomplete the script.

Integration

New integration from credential
const sftpClient = await yepcode.integration.sftp('credential-slug');
New integration from plain authentication data (username and password)
const Client = require('ssh2-sftp-client');

const options = {
host: 'hostname',
port: portNumber,
username: 'username',
password: 'password',
};

const sftpClient = new Client();
await sftpClient.connect(options);
New integration from plain authentication data (private key)
const Client = require('ssh2-sftp-client');

const options = {
host: 'hostname',
port: portNumber,
username: 'username',
privateKey: 'privateKey',
};

const sftpClient = new Client();
await sftpClient.connect(options);

Directory Listing

Directory listing

sftpClient.list('remoteDirPath')
.then((data) => {
console.log(data);
})
.catch(console.error)
.finally(() => sftpClient.end());

Check if Directory Exists

Test if remote file or directory exists
sftpClient.exists('remoteDirPath')
.then((exists) => {
console.log(exists);
})
.catch(console.error)
.finally(() => sftpClient.end());

Object Stats

Return the attributes associated with the object pointed to by path
sftpClient.stat('remoteDirPath')
.then((data) => {
console.log(data);
})
.catch(console.error)
.finally(() => sftpClient.end());

Retrieve a File

Retrieve a file from a remote server
const Stream = require('stream');
const echoStream = new Stream.Writable({
write: function (chunk, encoding, next) {
console.debug('echoStream:', chunk.toString());
next();
}
});

sftpClient.get('remoteDirPath', echoStream)
.catch(console.error)
.finally(() => sftpClient.end());

Upload Data

Upload data from stream to remote server
const Stream = require('stream');
const readableStream = new Stream.Readable({
read() {
// Create your readable stream
}
});

sftpClient.put(readableStream, 'remoteDirPath')
.catch(console.error)
.finally(() => sftpClient.end());

Append Input Data

Append the input data to an existing remote file
sftpClient.append(Buffer.from('Hello world'), 'remoteFilePath')
.catch(console.error)
.finally(() => sftpClient.end());

Delete a File

Delete a file on the remote server
sftpClient.delete('remoteFilePath')
.catch(console.error)
.finally(() => sftpClient.end());

Create a New Directory

Create a new directory on remote server
sftpClient.mkdir('remoteFilePath', true)
.catch(console.error)
.finally(() => sftpClient.end());

Rename a File or Directory

Rename a file or directory from fromPath to toPath
const from = '/remote/path/to/old.txt';
const to = '/remote/path/to/new.txt';

sftpClient.rename(from, to)
.catch(console.error)
.finally(() => sftpClient.end());

POSIX Rename

POSIX rename
const from = '/remote/path/to/old.txt';
const to = '/remote/path/to/new.txt';

sftpClient.posixRename(from, to)
.catch(console.error)
.finally(() => sftpClient.end());

Current Directory

Current remote working directory
sftpClient.cwd()
.then((directory) => {
console.log(`Remote working directory is ${directory}`);
})
.catch(console.error)
.finally(() => sftpClient.end());