Search…
SFTP
In-script SFTP client.
1
const sftp = require('sftp')
Copied!
The SFTP modules allows scripts to access an SFTP server.
To enable SFTP for your environment, please contact support.

module SFTP

create(options)

Creates a new connection and Client.
Arguments
  • options { Object }
    • host { String } Hostname or IP address of the server.
    • port { Number = 22 } Port number of the server.
    • username { String } Username for authentication.
    • password { String } Password for password-based user authentication.
    • privateKey { String } String containing a private key for key-based or host-based user authentication.
    • passphrase { String } The passphrase to decrypt an encrypted private key.
    • localHostname { String } Along with localUsername and privateKey, set this to a non-empty string for host-based user authentication.
    • localUsername { String } Along with localHostname and privateKey, set this to a non-empty string for host-based user authentication.
    • hostHash { String = md5 } A valid hash algorithm to check against the host fingerprint.
    • hostFingerprint { String } Use together with hostHash to verify the host fingerprint.
Returns
  • { sftp.Client } A new Client instance.

list()

List current script connections.
Returns
  • { Client[] } Active connections for this script.

class sftp.Client

SFTP Client. To create a Client, please use sftp.create(options).

chmod(path, mode)

Modify permissions.
Arguments
  • path { String } The resource to modify.
  • mode { String } A string containing octal numbers.

close()

Closes the connection to free up resources in order to open another connection. Connections are automatically closed when a script exits.

delete(path)

Delete a resource.
Arguments
  • path { String } The resource to delete.

exists(path)

Checks for the existence of a filesystem resource.
Arguments
  • path { String } The path to check.
Returns
  • { Boolean } true if the resource exists.

get(path)

Retrieve a file.
Arguments
  • path { String } The path to read.
Returns
  • { Buffer } A buffer containing the file data.

list(path)

Read the contents of a remote directory.
Arguments
  • path { String } The path to read.
Returns
  • { Object[] } A list of directory contents.
    • type { String } Entry type.
    • name { String } Entry name.
    • size { Number } Size in bytes.
    • modifyTime { Number } Modified time in milliseconds.
    • accessTime { Number } Last access time in milliseconds.
    • rights { Object }
      • user { String } User access rights (rwx).
      • group { String } Group access rights (rwx).
      • other { String } Other access rights (rwx).
    • owner { Number } Owner uid.
    • group { Number } Group gid.

mkdir(path)

Create a directory.
Arguments
  • path { String } The directory to create.

put(path, data)

Upload a file.
Arguments
  • path { String } The path to write.
  • data { Buffer } A buffer containing the file data.

rename(path, to)

Rename a resource.
Arguments
  • path { String } The resource to rename.
  • to { String } The target resource name.

stat(path)

Read a filesystem resource's stats.
Arguments
  • path { String } The path to read.
Returns
  • { Object } A resource stats object.
    • mode { Number } Mode for the resource.
    • permissions { String } Resource permissions.
    • size { Number } Size in bytes.
    • modifyTime { Number } Modified time in milliseconds.
    • accessTime { Number } Last access time in milliseconds.
    • owner { Number } Owner uid.
    • group { Number } Group gid.

Examples

1
const sftp = require('sftp'),
2
conn = sftp.create({
3
host: 'sftp.example.org',
4
port: 22,
5
username: 'foo',
6
password: 'this is a passphrase.'
7
}),
8
buffer = new Buffer('ok!'),
9
home = '/home/foo'
10
11
if (!conn.exists(`${home}/test`)) {
12
conn.mkdir(`${home}/test`)
13
}
14
if (!conn.exists(`${home}/test/test.txt`)) {
15
conn.put(`${home}/test/test.txt`, buffer)
16
}
17
conn.close()
Copied!
Last modified 2mo ago