Treat local, FTP and SFTP files the same way. 100% Node.js, no other software needed.
Visit the Project on GitHub https://github.com/Mylogo/ftp-sftp
Example FTP/SFTP Client using this library: https://github.com/Mylogo/ ftp-sftp-example
Contents
Installation
npm i ftp-sftp
Overview
class hierarchy
; ; ; ; ;
Instantiate a FileSystem
const localFileSystem = ;const ftpFileSystem = await FtpFileSystem;const sftpFileSystem = await SftpFileSystem;
API
FileSystem
// Returns the directory content. See FileInfo below: Promise<FileInfo>; // Inserts a file from a stream. You can get one from get(): Promise<any>; // Gets a file stream. Can be inserted into put(): Promise<NodeJSReadableStream>; // Creates a directory. recursive=true will also create subdirectories: Promise<any>; // Deletes a directory. recursive=true will also delete subdirectories: Promise<any>; // Deletes a filedeletepath: string: Promise<any>; // Can be used to move a file or just to rename it: Promise<any>;
FileInfo
// Gets the file name (does not include path): string // Gets the file size: number // returns true if it's a directory, false otherwise: boolean
Examples
// constants for all examplesconst host = '127.0.0.1';const port = 21; // or 22 for SFTPconst user = 'root';const password = 'password';
Listing items inside a directory
using Promise/then/catch
// You could just replace FtpFileSystem with SftpFileSystem for SFTP instead of FTPFtpFileSystem// Or: SftpFileSystem.create(host, port, user, password) ;
using Promise/then/catch
Upload file from local file system
var localFileSystem = ; SftpFileSystem// Or: FtpFileSystem.create(host, port, user, password) ;
Transfer file from FTP server to another SFTP server
using await
const ftpFileSystem = await FtpFileSystem;const sftpFileSystem = await SftpFileSystem; const readStream = await ftpFileSystem;await sftpFileSystem;
Create and delete directory
using await
const ftpFileSystem = await FtpFileSystem; // Create the directoryawait ftpFileSystem; // Create directory and sub-directories (recursively: true)await ftpFileSystem; // Delete the newly created directory (Only works on empty directories)await ftpFileSystem; // Delete directory with content (recursively: true)await ftpFileSystem;