a SFTP client for node.js, a wrapper for ssh2
npm install ssh2-sftp-client
let Client = require('ssh2-sftp-client');
let sftp = new Client();
sftp.connect({
host: '127.0.0.1',
port: '8080',
username: 'username'
password: '******'
}).then(() => {
return sftp.list('/pathname');
}).then((data) => {
console.log(data, 'the data info');
}).catch((err) => {
console.log(err, 'catch error');
});
the connection to server config pls see ssh2 client event.
list of methods: all the methods will return a Promise;
Retrieves a directory listing.
sftp.list(romoteFilePath)
directory info:
type: // file type(-, d, l)
name: // file name
size: // file size
modifyTime: // file timestamp of modified time
accessTime: // file timestamp of access time
rights: {
user:
group:
other:
},
owner: // user ID
group: // group ID
get a new readable stream for path. The encoding is passed to Node Stream (https://nodejs.org/api/stream.html) and it controls how the content is encoded. For example, when downloading binary data, 'null' should be passed (check node stream documentation). Defaults to 'utf8'.
sftp.get(remoteFilePath, [useCompression], [encoding]);
upload a file. it can be localPath
or Buffer
or Stream
.
sftp.put(localFilePath, remoteFilePath, [useCompression], [encoding]);
sftp.put(Buffer, remoteFilePath, [useCompression], [encoding]);
sftp.put(Stream, remoteFilePath, [useCompression], [encoding]);
create a new directory.
// recursive default is false, if true, it will create directory recursive
sftp.mkdir(remoteFilePath, recursive);
remove the directory or file.
// recursive default is false, if true, it will remove directory recursive even if is not empty
sftp.rmdir(localPath, recursive);
delete file.
sftp.delete(remoteFilePath);
rename/remove remoteSourcePath to remoteDestPath.
sftp.remove(remoteSourcePath, remoteDestPath);
connection config you will see here
- fix: add encoding control support for binary stream
- fix: multi image upload
- change: remove `this.client.sftp` to `connect` function