SFTP
SFTP
The SFTP module is responsible for accessing an SFTP (SSH File Transfer Protocol) server and handling files and directories securely via SSH.
Methods
connect
Method responsible for connecting to an SFTP server. This connection is used for every other action of the module. It supports authentication by user and password or by private key.
const connection = await SFTP.connect.v1_0_0({
host: "sftp.servidor.com",
port: 22,
user: "usuario",
password: "senha"
})
Required parameters
host: String - expects the address of the SFTP server (IP or hostname).port: Number - expects the port number of the SFTP server. The default port is22.
Optional parameters
user: String - expects the user name for the authentication on the server.password: String - expects the user password for the authentication on the server.privateKey: String - expects the full path of the private key file on the local computer for the SSH key authentication.passphrase: String - expects the secret phrase used to decrypt the private key, if it is protected. It can only be used together withprivateKey.readyTimeout: Number - time limit in milliseconds to establish the connection.keepaliveInterval: Number - interval in milliseconds to send keep-alive packets, keeping the connection active during periods of inactivity.
Return
The constant or variable created, such as the connection of the example shown earlier, receives the SFTP connection instance, which must be passed as the connection parameter in every other method of the SFTP module.
list
Action responsible for listing all the content of a directory on the SFTP server.
const list = await SFTP.list.v1_0_0({
connection: connection,
path: '/documentos'
})
Required parameters
connection: expects the connection instance returned by theconnectmethod.path: String - expects a string with the path of the directory whose content will be listed. Use/for the root directory.
Return
The constant or variable created, such as the list of the example shown earlier, receives an object with the following properties:
list.count: Number - returns the total of items found in the directory.list.items: Array - array with the items found. Each item has the following properties:items[n].isDirectory: Boolean - indicates whether the item is a directory.items[n].isFile: Boolean - indicates whether the item is a file.items[n].name: String - name of the file or directory.items[n].path: String - full path of the item on the server.items[n].size: Number - size in bytes of the file or directory.items[n].date: Date - date of the last modification.
createDir
Action responsible for creating a directory on the SFTP server.
const createDir = await SFTP.createDir.v1_0_0({
connection: connection,
path: '/documentos/novapasta',
recursive: true
})
Required parameters
connection: expects the connection instance returned by theconnectmethod.path: String - expects a string with the full path of the directory to be created.recursive: Boolean - indicates whether the intermediate folders of the path must be created automatically if they do not exist.
Return
The constant or variable created receives an object with the following properties:
createDir.created: String - indicates the result of the operation:createdif the directory was created, oralready existsif it already existed.createDir.path: String - full path of the directory on the server.
deleteDir
Action responsible for removing a directory on the SFTP server.
const deleteDir = await SFTP.deleteDir.v1_0_0({
connection: connection,
path: '/documentos/pastadeletar',
recursive: true
})
Required parameters
connection: expects the connection instance returned by theconnectmethod.path: String - expects a string with the full path of the directory to be removed.recursive: Boolean - indicates whether the content of the directory (subfolders and files) must be removed automatically. Without this option, the removal fails if the directory is not empty.
Return
This method has no return.
getFile
Action responsible for downloading a file from the SFTP server to the local computer.
const getFile = await SFTP.getFile.v1_0_0({
connection: connection,
remotePath: '/documentos/relatorio.pdf',
localPath: 'C:\\Usuários\\Roberty\\Downloads\\relatorio.pdf'
})
Required parameters
connection: expects the connection instance returned by theconnectmethod.remotePath: String - expects a string with the full path of the file on the SFTP server, including name and extension.localPath: String - expects a string with the full path where the file will be saved on the local computer, including name and extension.
Return
This method has no return.
putFile
Action responsible for uploading a file from the local computer to the SFTP server.
const putFile = await SFTP.putFile.v1_0_0({
connection: connection,
localPath: 'C:\\Usuários\\Roberty\\documentos\\relatorio.pdf',
remotePath: '/documentos/relatorio.pdf'
})
Required parameters
connection: expects the connection instance returned by theconnectmethod.localPath: String - expects a string with the full path of the file on the local computer, including name and extension.remotePath: String - expects a string with the full destination path on the SFTP server, including name and extension.
Return
This method has no return.
deleteFile
Action responsible for removing a file on the SFTP server.
const deleteFile = await SFTP.deleteFile.v1_0_0({
connection: connection,
path: '/documentos/arquivo.pdf'
})
Required parameters
connection: expects the connection instance returned by theconnectmethod.path: String - expects a string with the full path of the file to be removed on the server, including name and extension.
Return
This method has no return.
move
Action responsible for renaming or moving a file or directory on the SFTP server.
const move = await SFTP.move.v1_0_0({
connection: connection,
oldPath: '/documentos/relatorio-antigo.pdf',
newPath: '/arquivos/relatorio.pdf'
})
Required parameters
connection: expects the connection instance returned by theconnectmethod.oldPath: String - expects a string with the current path of the file or directory on the server.newPath: String - expects a string with the new path of the file or directory on the server. Provide the same directory with a different name to rename, or a different directory to move.
Return
This method has no return.
disconnect
Action responsible for closing the active connection with the SFTP server.
const disconnect = await SFTP.disconnect.v1_0_0({
connection: connection
})
Required parameters
connection: expects the connection instance returned by theconnectmethod.
Return
This method has no return.