I have a SFTP server hosting some files. How to hit my SFTP server from Catalyst and upload the files to Catalyst filestore?

I have a SFTP server hosting some files. How to hit my SFTP server from Catalyst and upload the files to Catalyst filestore?

1. Create a catalyst project and initialize the project in your local machine. You can refer to the help documentation for the same here.
2. Install Catalyst CLI in order to work with Catalyst in your local machine. You can refer to the help documentation for the same here.
2. Create an advancedIO function from the Catalyst CLI while initializing the project.
3. Navigate to your function directory(~/{project_name}/functions/{function_name}) in your terminal
4. Install the necessary npm packages that are used in the function by executing the commands below

  1. npm install ssh2-sftp-client
5. Paste the given code. Modify your hostname, port number, username and password. Navigate back to your project directory.

  1. "use strict";
  2. const Client = require('ssh2-sftp-client');
  3. module.exports = async(req, res) => {
  4.   connectToSFTPServer();
  5.   res.write("hit");
  6.   res.end();
  7. };
  8. async function connectToSFTPServer() {
  9.     const sftp = new Client();
  10.     const remoteHost = ''; //replace your SFTP host 
  11.     const remotePort = ;  //replace your port number (default is 22 for SFTP)
  12.     const remoteUsername = ''; //replace your SFTP server username
  13.     const remotePassword = ''; //replace your SFTP server password
  14.   
  15.     try {
  16.         await sftp.connect({
  17.           host: remoteHost,
  18.           port: remotePort,
  19.           username: remoteUsername,
  20.           password: remotePassword
  21.         });
  22.          console.log('Connected to SFTP server');
  23.          // Perform SFTP operations here
  24.          const remoteFilePath = '{your_remote_path}';
  25.          const files = await getFilesInDirectory(sftp, remoteFilePath);
  26.          console.log('Files in the remote directory:');
  27.          files.forEach((file) => {
  28.             console.log(file);
  29.           });
  30.         // Disconnect from the SFTP server
  31.         await sftp.end();
  32.         console.log('Disconnected from SFTP server');
  33.       } catch (err) {
  34.         console.error('Error:', err.message);
  35.       }}
  36.   async function getFilesInDirectory(client, remotePath) {
  37.     const entries = await client.list(remotePath);
  38.     const files = [];
  39.     for (const entry of entries) {
  40.       if (entry.type === 'd') {
  41.         // If the entry is a directory, recursively call the function
  42.         const subdirectoryPath = `${remotePath}/${entry.name}`;
  43.         const subdirectoryFiles = await getFilesInDirectory(client, subdirectoryPath);
  44.         files.push(...subdirectoryFiles);
  45.       } else if (entry.type === '-') {
  46.         // If the entry is a regular file, add it to the files array
  47.         files.push(entry.name);
  48.       }}
  49.     return files;
  50.   }

6. Run catalyst serve in order to test your function in local debugging.
7. Run catalyst deploy in order to deploy your function to the development environment. You can hit your function by using the URL generated while deploying the function. You can refer the help documentation for the same here.


      Catalyst Community