Last active
May 18, 2022 08:41
-
-
Save saggineumann/8925ab1c7b03991defa20f73798c902f to your computer and use it in GitHub Desktop.
sftp.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let Client = require('ssh2-sftp-client'); | |
class SFTPClient { | |
constructor() { | |
this.client = new Client(); | |
} | |
async connect(options) { | |
console.log(`Connecting to ${options.host}:${options.port}`); | |
try { | |
await this.client.connect(options); | |
} catch (err) { | |
console.log('Failed to connect:', err); | |
} | |
} | |
async disconnect() { | |
await this.client.end(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async downloadFile(remoteFile, localFile) { | |
console.log(`Downloading ${remoteFile} to ${localFile} ...`); | |
try { | |
await this.client.get(remoteFile, localFile); | |
} catch (err) { | |
console.error('Downloading failed:', err); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async listFiles(remoteDir, fileGlob) { | |
console.log(`Listing ${remoteDir} ...`); | |
let fileObjects; | |
try { | |
fileObjects = await this.client.list(remoteDir, fileGlob); | |
} catch (err) { | |
console.log('Listing failed:', err); | |
} | |
const fileNames = []; | |
for (const file of fileObjects) { | |
if (file.type === 'd') { | |
console.log(`${new Date(file.modifyTime).toISOString()} PRE ${file.name}`); | |
} else { | |
console.log(`${new Date(file.modifyTime).toISOString()} ${file.size} ${file.name}`); | |
} | |
fileNames.push(file.name); | |
} | |
return fileNames; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sftp.js | |
// | |
// Use this sample code to connect to your SFTP To Go server and run some file operations using Node.js. | |
// | |
// 1) Paste this code into a new file (sftp.js) | |
// | |
// 2) Install dependencies | |
// npm install ssh2-sftp-client@^8.0.0 | |
// | |
// 3) Run the script | |
// node sftp.js | |
// | |
// Compatible with Node.js >= v12 | |
// Using ssh2-sftp-client v8.0.0 | |
let Client = require('ssh2-sftp-client'); | |
class SFTPClient { | |
constructor() { | |
this.client = new Client(); | |
} | |
async connect(options) { | |
console.log(`Connecting to ${options.host}:${options.port}`); | |
try { | |
await this.client.connect(options); | |
} catch (err) { | |
console.log('Failed to connect:', err); | |
} | |
} | |
async disconnect() { | |
await this.client.end(); | |
} | |
async listFiles(remoteDir, fileGlob) { | |
console.log(`Listing ${remoteDir} ...`); | |
let fileObjects; | |
try { | |
fileObjects = await this.client.list(remoteDir, fileGlob); | |
} catch (err) { | |
console.log('Listing failed:', err); | |
} | |
const fileNames = []; | |
for (const file of fileObjects) { | |
if (file.type === 'd') { | |
console.log(`${new Date(file.modifyTime).toISOString()} PRE ${file.name}`); | |
} else { | |
console.log(`${new Date(file.modifyTime).toISOString()} ${file.size} ${file.name}`); | |
} | |
fileNames.push(file.name); | |
} | |
return fileNames; | |
} | |
async uploadFile(localFile, remoteFile) { | |
console.log(`Uploading ${localFile} to ${remoteFile} ...`); | |
try { | |
await this.client.put(localFile, remoteFile); | |
} catch (err) { | |
console.error('Uploading failed:', err); | |
} | |
} | |
async downloadFile(remoteFile, localFile) { | |
console.log(`Downloading ${remoteFile} to ${localFile} ...`); | |
try { | |
await this.client.get(remoteFile, localFile); | |
} catch (err) { | |
console.error('Downloading failed:', err); | |
} | |
} | |
async deleteFile(remoteFile) { | |
console.log(`Deleting ${remoteFile}`); | |
try { | |
await this.client.delete(remoteFile); | |
} catch (err) { | |
console.error('Deleting failed:', err); | |
} | |
} | |
} | |
(async () => { | |
const parsedURL = new URL(process.env.SFTPTOGO_URL); | |
const port = parsedURL.port || 22; | |
const { host, username, password } = parsedURL; | |
//* Open the connection | |
const client = new SFTPClient(); | |
await client.connect({ host, port, username, password }); | |
//* List working directory files | |
await client.listFiles("."); | |
//* Upload local file to remote file | |
await client.uploadFile("./local.txt", "./remote.txt"); | |
//* Download remote file to local file | |
await client.downloadFile("./remote.txt", "./download.txt"); | |
//* Delete remote file | |
await client.deleteFile("./remote.txt"); | |
//* Close the connection | |
await client.disconnect(); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async uploadFile(localFile, remoteFile) { | |
console.log(`Uploading ${localFile} to ${remoteFile} ...`); | |
try { | |
await this.client.put(localFile, remoteFile); | |
} catch (err) { | |
console.error('Uploading failed:', err); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment