NetSuite SuiteScript 2.0 SFTP API Usage Sample-CarlZeng

Background: Created a test SuiteLet script, after correct credential, it will do: 1. Upload/Sent specific NS File Cabinet file to SFTP server.(NS -> S

Background:

Created a test SuiteLet script, after correct credential, it will do:
1. Upload/Sent specific NS File Cabinet file to SFTP server.(NS -> SFTP)
2. Get a specific file from SFTP server to NS File Cabinet.(SFTP -> NS)

Screen Shoot - GET:

Upload/Sent specific NS File Cabinet file to SFTP server.

Screen Shoot - POST:Upload/Sent specific NS File Cabinet file to SFTP server. Response

Sample Code

/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope SameAccount
*
* @Note: This’s a test script which will do:

* 1. Upload/Sent specific NS File Cabinet file to SFTP server.(NS ->
* SFTP)

* 2. Get a specific file from SFTP server to NS File Cabinet.(SFTP ->
* NS) */ define(
[ ‘N/error’, ‘N/sftp’, ‘N/file’, ‘N/runtime’, ‘N/search’, ‘N/transaction’, ‘N/ui/serverWidget’ ], /**
* @param {error}
* error
* @param {file}
* file
* @param {runtime}
* runtime
* @param {search}
* search
* @param {transaction}
* transaction
* @param {serverWidget}
* ui */
function(error, sftp, file, runtime, search, transaction, ui) { /**
* Definition of the Suitelet script trigger point.
*
* @param {Object}
* context
* @param {ServerRequest}
* context.request - Encapsulation of the incoming
* request
* @param {ServerResponse}
* context.response - Encapsulation of the Suitelet
* response
* @Since 2015.2 */
function onRequest(context) { var strSftpServerUrl = ‘123.112.249.93’; var strSftpDir = ‘Public’; // *nix command: ssh-keyscan -t rsa -p 22 123.112.249.93
var strHostKey = “AAAAB3NzaC1yc2EAAAABIwAAAQEAul/AnqfetlyUR6cMEEHNEsJWXk+9mo2nCSgkgU1sIwC91qoWHc/QQl+5fNYZnF93x60IQ6rjmeW6h5E44xLqZ2g6UtA6ZB1nkTZS41imbTUb+Apge0xyE32/XI8aMdaG9ZORvuRJARz4dwtnIHBwZUQBjvOpcdala0nObHiCasG1QKpTk0OYPqwRYijKHDfaW5oPmMIYrQWnLHQ8E6krL8a9gKHcwtsyJh/Ng6sEZdpVHL6ZE/8/EyTGOED14Qh1QIN3Lbk2RLPPO5tHYS6rMlozGqTRVwadaBtXV8jXfsrUGzciu3JnEO3HHXBT4ILeEGdqWpDSA5t8TC986PJ4yw==”; var strHostKeyType = ‘rsa’; var objRequest = context.request; if (objRequest.method === ‘GET’) { var form = ui.createForm({
title : ‘SFTP Transfer Sample’ }); var objUserFld = form.addField({
id : ‘custpage_username’,
type : ui.FieldType.TEXT,
label : ‘SFTP User Name’ });
objUserFld.breakType = ui.FieldBreakType.STARTCOL;
objUserFld.isMandatory = true;

                form
                        .addCredentialField({
                            id : 'custpage\_sftp\_password\_token',
                            label : 'SFTP Password',
                            restrictToScriptIds : \[ 'customscript\_pri\_sl\_sftptransfer\_test' \],
                            restrictToDomains : \[ strSftpServerUrl \],
                        });

                form.addSubmitButton({
                    label : 'Submit' });
                context.response.writePage(form);

            } else if (objRequest.method === 'POST') { var strSftpUserName = objRequest.parameters.custpage\_username; var passwordToken = objRequest.parameters.custpage\_sftp\_password\_token;
                log.debug({
                    title : 'New password token',
                    details : passwordToken \+ ', Sftp User: '
                            + strSftpUserName
                }); var connection = sftp.createConnection({
                    username : strSftpUserName,
                    passwordGuid : passwordToken,
                    url : strSftpServerUrl,
                    port : 22,
                    directory : strSftpDir,
                    hostKey : strHostKey,
                    hostKeyType : strHostKeyType
                }); /\*\*
                 \* \[1\] Uploading the file to the external SFTP server. \*/
                var myFileToUpload = file.load({
                    id : 9288 }); // file.create({
                // name : 'originalname.txt',
                // fileType : file.Type.PLAINTEXT,
                // contents : 'I am a test file. Hear me roar.'
                // });

connection.upload({ // directory : ‘Public/‘,
filename : ‘newFileNameOnServer_TEST.js’,
file : myFileToUpload,
replaceExisting : true }); /**
* [2] Download sftp files to NetSuite FileCabinet */
var downloadedFile = connection.download({ // directory : ‘Public’,
filename : ‘tomato(123.114.225.191).ovpn.txt’ });
downloadedFile.folder = -10;
downloadedFile.save();

                context.response
                        .write('1. Uploaded newFileNameOnServer\_TEST.js to SFTP server("Public" folder). \\n\\r2. Downloaded "tomato(123.114.225.191).ovpn.txt" to fileCabinet("Attachments Received" folder).'); return true;
            }
        } return {
            onRequest : onRequest
        };

    });

Open MAC OS Build-in SFTP server

 http://osxdaily.com/2011/09/29/start-an-ftp-or-sftp-server-in-mac-os-x-lion/

Error Messages

Some time error message is cofusing.

I.e. AN_ERROR_OCCURRED_WHILE_DECRYPT_PASSWORDGUID

This error comes when POST of SuiteLet executing

var connection = sftp.createConnection({
username : strSftpUserName,
passwordGuid : passwordToken,
url : strSftpServerUrl,
port : 22,
directory : strSftpDir,
hostKey : strHostKey,
hostKeyType : strHostKeyType
});

But actually the problem is in GET side field definition:

form
.addCredentialField({
id : ‘custpage_sftp_password_token’,
label : ‘SFTP Password’,
restrictToScriptIds : [ ‘customscript_pri_sl_sftptransfer_test’ ],
restrictToDomains : [ strSftpServerUrl ],
});

You might ONLY need restrictToScriptIds and restrictToDomains;  

Adding restrictToCurrentUser: true MIGHT causing error.

个性化需求沟通 扫客服加V加群: