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.

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.

SFTP snippets available in YepCode 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 local system to remote server
const Stream = require('stream');
const readableStream = new Stream.Readable({
read() {}
});

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());