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