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
class FileSystem;
class LocalFileSystem extends FileSystem;
class FtpFileSystem extends FileSystem;
class SftpFileSystem extends FileSystem;
class FileInfo;
Instantiate a FileSystem
const localFileSystem = new LocalFileSystem();
const ftpFileSystem = await FtpFileSystem.create(host, port, user, password);
const sftpFileSystem = await SftpFileSystem.create(host, port, user, password);
API
FileSystem
list(path: string): Promise<FileInfo[]>;
put(src: NodeJS.ReadableStream, toPath: string): Promise<any>;
get(path: string): Promise<NodeJS.ReadableStream>;
mkdir(path: string, recursive: boolean): Promise<any>;
rmdir(path: string, recursive: boolean): Promise<any>;
delete(path: string): Promise<any>;
rename(oldPath: string, newPath: string): Promise<any>;
FileInfo
getName(): string
getSize(): number
isDirectory(): boolean
Examples
const host = '127.0.0.1';
const port = 21;
const user = 'root';
const password = 'password';
Listing items inside a directory
using Promise/then/catch
FtpFileSystem.create(host, port, user, password)
.then(ftpFileSystem => {
ftpFileSystem.list('/home')
.then(files => {
console.log("Files:", files);
}).catch(err => {
console.log("Could not retrieve directory /home", err);
})
}).catch(err => {
console.log("Error while connecting to FTP server:", err);
});
using Promise/then/catch
Upload file from local file system
var localFileSystem = new LocalFileSystem();
SftpFileSystem.create(host, port, user, password)
.then(sftpFileSystem => {
localFileSystem.get('/Users/dennis/catpic.jpeg')
.then(readStream => {
sftpFileSystem.put('/home/uploaded_catpic.jpeg', readStream)
.then(() => {
console.log("Important file was uploaded successfully, meow!")
})
})
}).catch(err => {
console.log("Error while uploading file:", err);
});
Transfer file from FTP server to another SFTP server
using await
const ftpFileSystem = await FtpFileSystem.create(host, port, user, password);
const sftpFileSystem = await SftpFileSystem.create(hort, port, user, password);
const readStream = await ftpFileSystem.get('/home/catpic.jpeg');
await sftpFileSystem.put(readStream, '/var/catpic.jpeg');
Create and delete directory
using await
const ftpFileSystem = await FtpFileSystem.create(host, port, user, password);
await ftpFileSystem.mkdir('/home/catpics');
await ftpFileSystem.mkdir('/home/even/more/catpics', true);
await ftpFileSystem.rmdir('/home/catpics');
await ftpFileSystem.rmdir('/home/even/more/catpics', true);