Create connection between SFTP and NetSuite and download files from SFTP

 let hostKey ="AAAAB3NzaC1yc2EAAAADAQABAAABAQDyjG7EbpWZ6q8KMjg8J759PDKM+kKlvgx6/gGe44IiOnx9B3Z0WpgL/2WKtt8smKqO+Vk/ycd8NEvoA+/cZywgEMr0LaP7mr/etDtVWQmGB13iPRnnPra3hSU0S1jC9sXpPZ87TWjQZqyYMhTfSEqKOs32LcBh3dCGqu7i2+JwhOONSzXju7vAbLFMSYJVgNuBdX36s54eSQgm8bpHjXS4it1Mrg592qhJwmgp54DN/4u/k3Xv0aKGd7hPBoeW9BhuI3+lrb0Y1sUuXmkhAEyX/PjiWX3l1ykmlU7RkB9mSvcRlhw8Uk/yR3f70dk4S24PieMo+EtZ5tM4ZJsC+J6X";
                let connection = sftp.createConnection({
                    username: 'basware',
                    keyId: 'custkey55',
                    url: '13.210.100.30',
                    hostKey: hostKey,
                    port: 22,
                    hostKeyType: 'rsa',
                });

                let list = connection.list({
                    path: '/Customer Purchase Orders'
                });
                let ROOT_FOLDER = 974369;
                let numLines = list.length;
                let docArray = [];
                let newFolderId = 974370;
                for (var i = 0; i < numLines; i++) {
                    try {
                        var item = list[i];
                        if (!item.directory) {
                            // download the file from the SFTP server
                            var downloadedFile = connection.download({
                                directory: '/Customer Purchase Orders',
                                filename: item.name
                            });
                            downloadedFile.folder = ROOT_FOLDER;
                            var fileId = downloadedFile.save();

                            let fileIs = file.load({id: fileId});

                           // upload the file to 'Processed Orders' directory on the SFTP server
                            connection.upload({
                                directory: '/Processed Orders',
                                filename: item.name,
                                file:fileIs,
                                replaceExisting: true
                            });

                            let xmlContent = fileIs.getContents();
                            docArray.push(xmlContent);
                            // Load the file object
                            var xmlFile = file.load({
                                id: fileId
                            });

                            // Move the file to the new folder
                            xmlFile.folder = newFolderId;
                            // Save the updated file object
                            xmlFile.save();

                           //remove the files from SFTP
                            connection.removeFile({
                                path: '/Customer Purchase Orders/' + item.name,
                            });

                        }

Leave a comment

Your email address will not be published. Required fields are marked *