Skip to content

SFTPService Examples

Dmytro Katashev edited this page Aug 13, 2024 · 2 revisions

SFTP Service Examples


Download a File from SFTP

This example demonstrates how to download a file from an SFTP server using the SFTPService class.

var SFTPService = require('*/cartridge/scripts/webservice/SFTPService');

// Define a custom SFTP service
var FileDownloadService = SFTPService.extend({
    SERVICE_CONFIGURATIONS: {
        default: 'sftp.download.service',
    },

    /**
     * Download a file from the SFTP server.
     *
     * @param {string} remoteFilePath - The path of the file to download.
     * @param {string} localFilePath - The path where the file should be saved locally.
     * @returns {dw.svc.Result} - The SFTP service result.
     */
    downloadFile: function(remoteFilePath, localFilePath) {
        var File = require('dw/io/File');
        var localFile = new File(localFilePath);

        return this.fetch({
            operation: 'getBinary',
            args: [remoteFilePath, localFile]
        });
    }
});

// Perform the SFTP download
var result = FileDownloadService.downloadFile('/remote/path/file.txt', 'IMPEX/path/file.txt');

Upload a File to SFTP

This example demonstrates how to upload a file to an SFTP server.

var SFTPService = require('*/cartridge/scripts/webservice/SFTPService');

// Define a custom SFTP service
var FileUploadService = SFTPService.extend({
    SERVICE_CONFIGURATIONS: {
        default: 'sftp.upload.service',
    },

    /**
     * Upload a file to the SFTP server.
     *
     * @param {string} localFilePath - The path of the local file to upload.
     * @param {string} remoteFilePath - The destination path on the SFTP server.
     * @returns {dw.svc.Result} - The SFTP service result.
     */
    uploadFile: function(localFilePath, remoteFilePath) {
        var File = require('dw/io/File');
        var localFile = new File(localFilePath);

        return this.fetch({
            operation: 'putBinary',
            args: [localFile, remoteFilePath]
        });
    }
});

// Perform the SFTP upload
var result = FileUploadService.uploadFile('IMPEX/local/path/file.txt', '/remote/path/file.txt');

List Directory Contents

This example shows how to list the contents of a directory on the SFTP server.

var SFTPService = require('*/cartridge/scripts/webservice/SFTPService');

// Define a custom SFTP service
var DirectoryListingService = SFTPService.extend({
    SERVICE_CONFIGURATIONS: {
        default: 'sftp.list.service',
    },

    /**
     * List the contents of a directory on the SFTP server.
     *
     * @param {string} remoteDirectoryPath - The path of the directory to list.
     * @returns {dw.svc.Result} - The SFTP service result.
     */
    listDirectory: function(remoteDirectoryPath) {
        return this.fetch({
            operation: 'list',
            args: [remoteDirectoryPath]
        });
    }
});

// Perform the directory listing
var result = DirectoryListingService.listDirectory('/remote/path/directory');

Create a Directory on SFTP

This example demonstrates how to create a directory on an SFTP server.

var SFTPService = require('*/cartridge/scripts/webservice/SFTPService');

// Define a custom SFTP service
var DirectoryCreationService = SFTPService.extend({
    SERVICE_CONFIGURATIONS: {
        default: 'sftp.mkdir.service',
    },

    /**
     * Create a directory on the SFTP server.
     *
     * @param {string} remoteDirectoryPath - The path of the directory to create.
     * @returns {dw.svc.Result} - The SFTP service result.
     */
    createDirectory: function(remoteDirectoryPath) {
        return this.fetch({
            operation: 'mkdir',
            args: [remoteDirectoryPath]
        });
    }
});

// Create the directory on SFTP
var result = DirectoryCreationService.createDirectory('/remote/path/newdirectory');

Delete a File from SFTP

This example demonstrates how to delete a file from an SFTP server.

var SFTPService = require('*/cartridge/scripts/webservice/SFTPService');

// Define a custom SFTP service
var FileDeletionService = SFTPService.extend({
    SERVICE_CONFIGURATIONS: {
        default: 'sftp.delete.service',
    },

    /**
     * Delete a file from the SFTP server.
     *
     * @param {string} remoteFilePath - The path of the file to delete.
     * @returns {dw.svc.Result} - The SFTP service result.
     */
    deleteFile: function(remoteFilePath) {
        return this.fetch({
            operation: 'del',
            args: [remoteFilePath]
        });
    }
});

// Delete the file from SFTP
var result = FileDeletionService.deleteFile('/remote/path/file.txt');

Execute Multiple Operations on SFTP

This example demonstrates how to upload a file to an SFTP server, rename the file, and then list the contents of the directory, all within a single service call.

var SFTPService = require('*/cartridge/scripts/webservice/SFTPService');

// Define a custom SFTP service
var MultiOperationService = SFTPService.extend({
    SERVICE_CONFIGURATIONS: {
        default: 'sftp.multioperation.service',
    },

    /**
     * Manage files on the SFTP server by performing multiple operations.
     *
     * @param {string} localFilePath - The local path of the file to upload.
     * @param {string} remoteDirectoryPath - The path of the directory on the SFTP server.
     * @param {string} newFileName - The new name for the uploaded file.
     * @returns {dw.svc.Result} - The SFTP service result.
     */
    manageFiles: function(localFilePath, remoteDirectoryPath, newFileName) {
        return this.fetch({
            onExecute: function(svc) {
                var client = svc.client;

                // Define remote file paths
                var remoteFilePath = remoteDirectoryPath + '/' + newFileName;
                var tempRemoteFilePath = remoteDirectoryPath + '/temp_' + newFileName;

                // Upload the file to the remote directory
                if (client.put(localFilePath, tempRemoteFilePath)) {
                    // Rename the uploaded file
                    client.rename(tempRemoteFilePath, remoteFilePath);
                }

                // List the contents of the directory after the operations
                return client.list(remoteDirectoryPath);
            }
        });
    }
});

// Perform the SFTP operations
var result = MultiOperationService.manageFiles('IMPEX/local/path/file.txt', '/remote/path/directory', 'finalfile.txt');

Clone this wiki locally