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
- npm install ssh2-sftp-client
5. Paste the given code. Modify your hostname, port number, username and password. Navigate back to your project directory.
- "use strict";
- const Client = require('ssh2-sftp-client');
- module.exports = async(req, res) => {
- connectToSFTPServer();
- res.write("hit");
- res.end();
- };
- async function connectToSFTPServer() {
- const sftp = new Client();
- const remoteHost = ''; //replace your SFTP host
- const remotePort = ; //replace your port number (default is 22 for SFTP)
- const remoteUsername = ''; //replace your SFTP server username
- const remotePassword = ''; //replace your SFTP server password
-
- try {
- await sftp.connect({
- host: remoteHost,
- port: remotePort,
- username: remoteUsername,
- password: remotePassword
- });
- console.log('Connected to SFTP server');
- // Perform SFTP operations here
- const remoteFilePath = '{your_remote_path}';
- const files = await getFilesInDirectory(sftp, remoteFilePath);
- console.log('Files in the remote directory:');
- files.forEach((file) => {
- console.log(file);
- });
- // Disconnect from the SFTP server
- await sftp.end();
- console.log('Disconnected from SFTP server');
- } catch (err) {
- console.error('Error:', err.message);
- }}
- async function getFilesInDirectory(client, remotePath) {
- const entries = await client.list(remotePath);
- const files = [];
- for (const entry of entries) {
- if (entry.type === 'd') {
- // If the entry is a directory, recursively call the function
- const subdirectoryPath = `${remotePath}/${entry.name}`;
- const subdirectoryFiles = await getFilesInDirectory(client, subdirectoryPath);
- files.push(...subdirectoryFiles);
- } else if (entry.type === '-') {
- // If the entry is a regular file, add it to the files array
- files.push(entry.name);
- }}
- return files;
- }
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.