Retrieve the timestamp of a file downloaded from SFTP server

To retrieve the timestamp of a file downloaded from an SFTP server using SuiteScript, you’ll primarily be interacting with the N/sftp module. NetSuite’s N/sftp module allows you to connect to an SFTP server and perform operations such as downloading files. However, to specifically get the timestamp of a file, you will need to use the list method provided by the N/sftp module, which can give you details about the files in the directory, including their timestamps.

Here’s a step-by-step guide on how you can do this:

Step 1: Set Up SFTP Connection

First, you’ll need to set up and configure an SFTP connection in NetSuite. This includes setting up the SFTP server details and generating the necessary credentials (e.g., username, password, host key, etc.).

Step 2: Write the SuiteScript to Access the SFTP and Retrieve File Timestamp

Here’s an example of how you could write a script using SuiteScript 2.x to connect to an SFTP server, list files, and retrieve their timestamps.

/**
 * @NApiVersion 2.x
 * @NScriptType ScheduledScript
 */
define(['N/sftp', 'N/search', 'N/file', 'N/runtime'], function(sftp, search, file, runtime) {


    function execute(context) {
        var connection = sftp.createConnection({
            username: 'your_username',
            passwordGuid: 'your_password_guid',
            url: 'sftp.yourserver.com',
            directory: 'path/to/directory',
            hostKey: 'your_host_key'
        });


        // List files in the directory
        var fileList = connection.list({
            path: '/path/to/your/directory/'
        });


        fileList.forEach(function (fileDetail) {
            log.debug({
                title: 'File Details',
                details: 'Name: ' + fileDetail.name + ', Timestamp: ' + fileDetail.lastModified
            });
            // fileDetail.lastModified gives you the timestamp of the file
        });
    }


    return {
        execute: execute
    };
});

Key Components of the Script:

  • SFTP Connection: The createConnection method sets up the connection to the SFTP server using your credentials.
  • Listing Files: The list method retrieves a list of files in a specified directory on the SFTP server.
  • Accessing File Timestamp: Each object in the list returned from list includes properties such as name, size, and lastModified. The lastModified property contains the timestamp of when the file was last modified.

Additional Notes:

  • Ensure that the directory paths and SFTP credentials are correctly configured.
  • The above script assumes you have the necessary permissions and roles set up in NetSuite to use the N/sftp module.
  • Depending on your specific use case and where this script is executed (e.g., as a scheduled script, user event, etc.), you may need to handle errors and connectivity issues appropriately.

This script should help you retrieve and log the timestamps of files in an SFTP directory directly from a NetSuite script.

Leave a comment

Your email address will not be published. Required fields are marked *