It is necessary to establish an FTP connection to Windows Server using the Node.js platform, cut out a specific folder (along with the folders and files located in it) and paste it into a specific directory on a server running Linux.
Is it possible to implement the described task? If yes, what NPM packages to use and / or what program code is suitable for solving the problem?
You can use ssh2-sftp-client package from npm
const Client = require('ssh2-sftp-client');
const config = {
host: 'example.com',
port: 22,
username: 'red-don',
password: 'my-secret'
};
let sftp = new Client;
sftp.connect(config)
.then(() => {
return sftp.list('/path/to/remote/dir');
})
.then(async data => {
console.log(data);
for(let file of data) {
//you can check if file is directory or file
if(file.type == 'd') {
//recursively read files
}else {
//download file
const remoteFilename = '/path/to/remote/dir/' + data.name;
const localFilename = '/path/to/local/files/' + data.name;
sftp.get(remoteFilename).then((stream) => {
stream.pipe(fs.createWriteStream(localFilename));
});`enter code here`
}
}
//delete directory recursively after all files are download
return client.rmdir(/path/to/remote/dir, true);
})
.then(() => {
sftp.end();
})
.catch(err => {
console.error(err.message);
});
const ftp = require('basic-ftp');
ftp_connect()
async function ftp_connect() {
const client = new ftp.Client();
client.ftp.verbose = true;
try {
await client.access({
host: 'host',
port: '21',
user: 'domain\\username',
password: 'password',
secure: false
})
console.log(await client.list())
}
catch(err) {
console.log(err)
}
client.close()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.