Modules

filter

 

Objects/Functions

module (N/sftp)

SuiteScript 2.0

Main Examples

N/sftp Module
Member Type: Name
Search NetSuite - https://system.netsuite.com/app/help/helpcenter.nl?search=N/sftp Module

// Code Example 1
/**
* @NApiVersion 2.x
*/

require(['N/sftp', 'N/file'],
function(sftp, file) {
var myPwdGuid = "B34672495064525E5D65032D63B52301";
var myHostKey = "AAA1234567890Q=";

// establish connection to remote FTP server

var connection = sftp.createConnection({
username: 'myuser',
passwordGuid: myPwdGuid, // references var myPwdGuid
url: 'host.somewhere.com',
directory: 'myuser/wheres/my/file',
hostKey: myHostKey // references var myHostKey
});

// specify the file to upload using the N/file module

var myFileToUpload = file.create({
name: 'originalname.js',
fileType: file.Type.PLAINTEXT,
contents: 'I am a test file.'
});

// upload the file to the remote server

connection.upload({
directory: 'relative/path/to/remote/dir',
filename: 'newFileNameOnServer.js',
file: myFileToUpload,
replaceExisting: true
});

// download the file from the remote server

var downloadedFile = connection.download({
directory: 'relative/path/to/file',
filename: 'downloadMe.js'
});
});

// Code Example 2
/**
* @NApiVersion 2.x
*/

require(['N/sftp', 'N/file'], function(sftp, file){
// Establish connection
log.debug('Establishing SFTP connection...');
var connection = sftp.createConnection({
username: 'sftpuser',
keyId: 'custkeysftp_nft_demo_key',
url: 'myurl',
port: 22,
directory: 'inbound',
hostKey: 'myhostkey'
});
log.debug('Connection established!');
// ------------------------------------------------------------------------
// List the directory and log number of elements there
var list = connection.list({path: 'yyy/test'});
log.debug('Items in directory "test" at the beginning: ' + list.length);
// ------------------------------------------------------------------------
// Generate the test file
log.debug('Generating test file...');
var myFileToUpload = file.create({
name: 'asdf.txt',
fileType: file.Type.PLAINTEXT,
contents: 'I am a test file.'
});
log.debug('Test file generated, uploading to "test" directory...');
// ------------------------------------------------------------------------
// Upload the test file
connection.upload({
directory: 'yyy/test',
filename: 'af.txt',
file: myFileToUpload,
replaceExisting: true
});
log.debug('Upload complete!');
// ------------------------------------------------------------------------
// List the directory to see there is one more file than before
list = connection.list({path: 'yyy/test'});
log.debug('Items in directory "test" after the upload: ' + list.length);
// ------------------------------------------------------------------------
// Create new directory
log.debug('Creating directory "test2"...');
try {
connection.makeDirectory({path: 'yyy/test2'});
log.debug('Directory created.');
} catch (e) {
log.debug('Directory not created.');
log.error(e.message);
}
list = connection.list({path: 'yyy/test2'});
log.debug('Items in directory "test2": ' + list.length);
// ------------------------------------------------------------------------
// Move the test file there
log.debug('Moving the test file from "test" to "test2"...');
connection.move({
from: 'yyy/test/af.txt',
to: 'yyy/test2/af.txt'
})
log.debug('File moved!');
// ------------------------------------------------------------------------
// List the original directory again to see the file is moved out
list = connection.list({path: 'yyy/test'});
log.debug('Items in directory "test" after the upload: ' + list.length);
// ------------------------------------------------------------------------
// List the new directory for the file
list = connection.list({path: 'yyy/test2'});
log.debug('Items in directory "test2" after the upload: ' + list.length);
log.debug(JSON.stringify(list));
// ------------------------------------------------------------------------
// Try to remove the directory
log.debug('Removing directory "test2"...');
try {
connection.removeDirectory({
path: 'yyy/test2'
});
log.debug('Directory removed!');
} catch (e) {
log.debug('Directory not removed!');
log.error(e.message);
}
// ------------------------------------------------------------------------
// It's not empty so let's delete the file first
log.debug('Removing test file from "test2" directory...');
connection.removeFile({
path: 'yyy/test2/af.txt'
});
log.debug('Test file removed!');

list = connection.list({path: 'yyy/test2'});
log.debug('Items in directory "test2": ' + list.length);
// ------------------------------------------------------------------------
// Remove it now again
log.debug('Removing directory "test2"...');
try {
connection.removeDirectory({
path: 'yyy/test2'
});
log.debug('Directory removed!');
} catch (e) {
log.debug('Directory not removed!');
log.error(e.message);
}
// ------------------------------------------------------------------------
// Listing it again
log.debug('Trying to list directory "test2"...');
try {
list = connection.list({path: 'yyy/test2'});
} catch (e) {
log.error(e.message);
}
// ------------------------------------------------------------------------
;
})

// Code Example 3
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/

define(['N/file', 'N/sftp', 'N/error'],
function(file, sftp, error) {
return {
beforeLoad: function(){
var portNumber = -1;
var connectTimeout = -1;
var transferTimeout = -1;
//these variables can be taken as parameters of the script instead

if (portNumber < sftp.MIN_PORT_NUMBER || portNumber > sftp.MAX_PORT_NUMBER)
portNumber = sftp.DEFAULT_PORT_NUMBER;
if (connectTimeout < sftp.MIN_CONNECT_TIMEOUT)
connectTimeout = sftp.MIN_CONNECT_TIMEOUT;
else if (connectTimeout > sftp.MAX_CONNECT_TIMEOUT)
connectTimeout = sftp.MAX_CONNECT_TIMEOUT;

var connection = sftp.createConnection({
username: 'sftpuser',
keyId: 'custkey1',
url: '192.168.0.100',
port: portNumber,
directory: 'inbound',
timeout: connectTimeout,
hostKey: "AAAAB3NzaC1yc2EAAAADAQABAAABAQDMifKH2vTxdiype8nem7+lS3x7dTQR/A67KdsR/5C2WUcDipBzYhHbnG6Am12Nd2tlM01LnaBZA6/8P4Y9x/sGTxtsdE/MzeGDUBn6HBlQvgIrhX62wgoKGQ+P2lEAO1+Vz8y3/MB1NmD7Fc62cJ9Mu88YA6jwJOIPZeHYNVyIm9OrY6VyzYyvSJhH0x7SXyvGnijJQF4G8C4c8u/UVpF/sE16xKZtly2Rx0aDL2FsDRtpyPmM602/R6ISbsmgab3MzzAEIu+zLDMdIBJn3cDhNt1F7Rar6Tu0u18KCkk8GPxbnxDuG4sCNOnXPYkDXSMUbM/ocRjYGtqdZUMmeTf3"
});

// can also be a big file (created for example by async search)
var myFileToUpload = file.create({
name: 'originalname.txt',
fileType: file.Type.PLAINTEXT,
contents: 'I am a test file.'
});

if (myFileToUpload.size > connection.MAX_FILE_SIZE)
throw error.create({name:"FILE_IS_TOO_BIG", message:"The file you are trying to upload is too big"});

if (transferTimeout > connection.MAX_TRANSFER_TIMEOUT)
transferTimeout = connection.MAX_TRANSFER_TIMEOUT;
else if (transferTimeout < connection.MIN_TRANSFER_TIMEOUT)
transferTimeout = connection.MIN_TRANSFER_TIMEOUT;


connection.upload({
directory: 'files',
filename: 'test.txt',
file: myFileToUpload,
replaceExisting: true,
timeout: transferTimeout
});

}
};
});

//SOURCE: https://system.netsuite.com/app/help/helpcenter.nl?fid=section_4617004932.html