Comment on page
SFTP
In-script SFTP client
const sftp = require('sftp')
The Secure File Transfer Protocol (SFTP) modules allow scripts to access an SFTP server.
Note: You must have access to an FTP server and know the following values:
host, port, username, and password.
Tip: To enable SFTP for your environment, please contact support.
Creates a new connection and client.
Arguments
options
{ Object }host
{ String } Server domain name or IP address.port
{ Number =22
} SFTP server port.username
{ String } Username for authentication.password
{ String } Password for 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 withlocalUsername
andprivateKey
, set this to a non-empty string for host-based user authentication.localUsername
{ String } Along withlocalHostname
andprivateKey
, set this value 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 withhostHash
to verify the host fingerprint.
Returns
- { sftp.Client } A new client instance.
Lists current script connections.
Returns
- { Client[] } Active connections for this script.
SFTP client. To create a client, use
sftp.create(options)
.Modifies permissions.
Arguments
path
{ String } The resource to modify.mode
{ String } A string containing octal numbers.
Closes the connection with the server.
Note: Connections are automatically closed when a script exits, which frees up resources to open another connection.
Deletes a resource.
Arguments
path
{ String } The resource to delete.
Checks for the existence of a filesystem resource.
Arguments
path
{ String } The path to check.
Returns
- { Boolean } True if the resource exists.
Retrieves a file.
Arguments
path
{ String } The path to read.
Returns
- { Buffer } A buffer containing the file data.
Reads 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.
Creates a directory.
Arguments
path
{ String } The directory to create.
Uploads a file.
Arguments
path
{ String } The path to write.data
{ Buffer } A buffer containing the file data.
Renames a resource.
Arguments
path
{ String } The resource to rename.to
{ String } The target resource name.
Reads 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.
const sftp = require('sftp'),
conn = sftp.create({
host: 'sftp.example.org',
port: 22,
username: 'foo',
password: 'this is a passphrase.'
}),
buffer = new Buffer('ok!'),
home = '/home/foo'
if (!conn.exists(`${home}/test`)) {
conn.mkdir(`${home}/test`)
}
if (!conn.exists(`${home}/test/test.txt`)) {
conn.put(`${home}/test/test.txt`, buffer)
}
conn.close()
Last modified 1yr ago