NetSuite SuiteScript 2.0 SFTP API Usage Sample

发布时间:2017-7-1 11:34:38编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"NetSuite SuiteScript 2.0 SFTP API Usage Sample ",主要涉及到NetSuite SuiteScript 2.0 SFTP API Usage Sample 方面的内容,对于NetSuite SuiteScript 2.0 SFTP API Usage Sample 感兴趣的同学可以参考一下。

NetSuite SuiteScript 2.0 SFTP API Usage Sample

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: <br>
 *        1. Upload/Sent specific NS File Cabinet file to SFTP server.(NS ->
 *        SFTP)<br>
 *        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.


上一篇:levmar ndk 编译
下一篇:react native遇到的坑

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

最近更新

好贷网好贷款