Quantcast
Channel: Active questions tagged https - Stack Overflow
Viewing all articles
Browse latest Browse all 1529

How to alter Javascript code from SFTP to HTTPS to transfer file

$
0
0

I have SFTP code using Javascript for transfering file from our local on-prem server to cloud server (in this case is from ServiceNow MID Server to AWS S3). But I am required to transfer by using HTTPS protocol (port : 443). Question, by using below information,

  1. Can I reused same script but just changing port from 22 to 443?
  2. If cant, then how I can alter the existing code from SFTP into HTTPS?

Endpoint : sn-bucket-stg.vpce-xxx-xxx.s3.us-east-1.vpce.amazonaws.comPort : 443Username : sn-aws-userPassword : xxxxxxxxxObjective : Transfer file A.pdf from Folder C:/AWS to S3 Bucket sn-bucket-stg/SNFolder

SFTP code :

var SFTP_Test = Class.create();SFTP_Test.prototype = {    initialize: function() {        this.Properties = Packages.java.util.Properties;        this.StringUtil = Packages.com.glide.util.StringUtil;        this.BufferedWriter = Packages.java.io.BufferedWriter;        this.File = Packages.java.io.File;        this.FileWriter = Packages.java.io.FileWriter;        this.FileReader = Packages.java.io.FileReader;        this.Encrypter = new Packages.com.glide.util.Encrypter();        this.FileOutputStream = Packages.java.io.FileOutputStream;        this.FileInputStream = Packages.java.io.FileInputStream;        this.BufferedReader = Packages.java.io.BufferedReader;        this.InputStreamReader = Packages.java.io.InputStreamReader;        this.OutputStraem = Packages.java.io.OutputStream;        this.OutputStreamWriter = Packages.java.io.OutputStreamWriter;        this.BufferedOutputStream = Packages.java.io.BufferedOutputStream;        this.Thread = Packages.java.lang.Thread;        this.targetServer = "sn-bucket-stg.vpce-xxx-xxx.s3.us-east-1.vpce.amazonaws.com";        this.targetPort = "22";        this.targetUsername = "sn-aws-user";        this.targetPassword = "xxxxxxxxxxx";        this.targetPath = "SNFolder";        this.FILE_PATH = "C:/AWS";        this.FILE_NAME = "A.pdf";        this.TARGETFILENAME = "SN_A.pdf";        this.PROCESSED_PATH = "C:/SNFolder/Backup";        this.FILE_EXTENSION = "pdf";    },    FileTransfer: function() {        try {            var localFileName = this.FILE_PATH +"/" + this.FILE_NAME;            var processedFileName = this.PROCESSED_PATH +"/" + this.TARGETFILENAME;            this.log("Copying from local file : " + localFileName);            this.sftpFile(this.targetServer, this.targetUsername, this.targetPassword, localFileName, this.targetPath + this.TARGETFILENAME, processedFileName);        } catch (e) {            this.log("Error : " + e);        }    },    sftpFile: function(hostName, userName, password, localFileName, remoteFileName, processedFileName) {        if (!this.targetPort) {            this.targetPort = 22;        }        var authPassword = new Packages.com.glide.util.Encrypter().decrypt(password);    var jsch = new Packages.com.jcraft.jsch.JSch();    var jschSession = jsch.getSession(userName, hostName);    jschSession.setPassword(authPassword);    var config = this.Properties();    config.put("StrictHostKeyChecking", "no");    jschSession.setConfig(config);    jschSession.connect();    var channelSftp = jschSession.openChannel("sftp");    channelSftp.connect();            try {                var inputFile = new this.File(localFileName);                if (this.FILE_EXTENSION == 'xlsx') {                    var inputStream = this.FileInputStream(inputFile);                    var outputStream = this.FileOutputStream(processedFileName);                    var byteRead;                    while ((byteRead = inputStream.read()) != -1) {                        outputStream.write(byteRead);                    }                } else {                    var fileReader = this.FileReader(inputFile); // A stream that connects to the text file                    var bufferedReader = this.BufferedReader(fileReader); // Connect the FileReader to the BufferedReader                    //create new file                    // Creates a FileOutputStream                    var outputFile = this.FileOutputStream(processedFileName);                    // Creates an OutputStreamWriter                    var output = this.OutputStreamWriter(outputFile);                    var line;                    while ((line = bufferedReader.readLine()) != null) {                        // Writes string to the file                        output.write(line);                        output.write("\r\n");                    }                    // Closes the reader                    bufferedReader.close();                    // Closes the writer                    output.close();                    this.log('copied file to '+ processedFileName);                }                //}                //END                channelSftp.put(processedFileName, remoteFileName);                this.log("File has successfully copied to target path");            } catch (e) {                this.log('Error: '+ e);            }        channelSftp.exit();            try {                // kill connection                jschSession.disconnect();            } catch (e) {                this.log('Error: '+ e);            }        //}    },    type: 'SFTP_Test'};

Viewing all articles
Browse latest Browse all 1529

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>