Socket
Socket
Sign inDemoInstall

ftp-sftp

Package Overview
Dependencies
21
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

ftp-sftp

Treat Local, FTP and SFTP file systems the same way.


Version published
Maintainers
1
Weekly downloads
132
decreased by-31.61%

Weekly downloads

Readme

Source

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

// Returns the directory content. See FileInfo below
list(path: string): Promise<FileInfo[]>;

// Inserts a file from a stream. You can get one from get()
put(src: NodeJS.ReadableStream, toPath: string): Promise<any>;

// Gets a file stream. Can be inserted into put()
get(path: string): Promise<NodeJS.ReadableStream>;

// Creates a directory. recursive=true will also create subdirectories
mkdir(path: string, recursive: boolean): Promise<any>;

// Deletes a directory. recursive=true will also delete subdirectories
rmdir(path: string, recursive: boolean): Promise<any>;

// Deletes a file
delete(path: string): Promise<any>;

// Can be used to move a file or just to rename it
rename(oldPath: string, newPath: string): Promise<any>;

FileInfo

// Gets the file name (does not include path)
getName(): string

// Gets the file size
getSize(): number

// returns true if it's a directory, false otherwise
isDirectory(): boolean

Examples

// constants for all examples
const host = '127.0.0.1';
const port = 21; // or 22 for SFTP
const 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 FTP
FtpFileSystem.create(host, port, user, password)
// Or: SftpFileSystem.create(host, port, user, password)
  .then(ftpFileSystem => {
    // List remote files
    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)
// Or: FtpFileSystem.create(host, port, user, password)
  .then(sftpFileSystem => {
    // Get the local file as a stream
    localFileSystem.get('/Users/dennis/catpic.jpeg')
      .then(readStream => {
        // Now, upload the file to the SFTP server
        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);

// Create the directory
await ftpFileSystem.mkdir('/home/catpics');

// Create directory and sub-directories (recursively: true)
await ftpFileSystem.mkdir('/home/even/more/catpics', true);


// Delete the newly created directory (Only works on empty directories)
await ftpFileSystem.rmdir('/home/catpics');

// Delete directory with content (recursively: true)
await ftpFileSystem.rmdir('/home/even/more/catpics', true);

Keywords

FAQs

Last updated on 31 Dec 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc