Upload and Download a File
The following sample shows how to upload and download a file.
To obtain a real host key, use ssh-keyscan <domain>.
To create a real password GUID, obtain a password value from a credential field on a form. For more information, see Form.addCredentialField(options). Also see N/https Module Script Samples for a Suitelet sample that shows creating a form field that generates a GUID.
This sample script uses the require
function so that you can copy it into the SuiteScript Debugger and test it. You must use the define
function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.
This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.
/**
* @NApiVersion 2.1
*/
require(['N/sftp', 'N/file'], (sftp, file) => {
const myPwdGuid = "B34672495064525E5D65032D63B52301";
const myHostKey = "AAA1234567890Q=";
// Establish a connection to a remote FTP server
let connection = sftp.createConnection({
username: 'myuser',
passwordGuid: myPwdGuid,
url: 'host.somewhere.com',
directory: 'myuser/wheres/my/file',
hostKey: myHostKey
});
// Create a file to upload using the N/file module
let 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
let downloadedFile = connection.download({
directory: 'relative/path/to/file',
filename: 'downloadMe.js'
});
});