SFTP in MuleSoft

SFTP in MuleSoft

SFTP (2)

SFTP stands for SSH File Transfer Protocol which, unlike the FTP protocol, is secure. SFTP in MuleSoft is a secure protocol for transferring files that work more or less the same as FTP but safe. Since the files are transmitted over a secure channel, it prevents files from unauthorized access and any content or password sniffing. SFTP requires a username and password to connect as well as using an SSH key.

Creating a local SFTP server

Download Rebex Tiny SFTP Server for creating an SFTP server on a local machine. Once downloaded, unzip the contents and Browse to the folder and start the server by clicking on the RebexTinySftpServer application file.

sftp-rebex

User and password are displayed on the Rebex Server screen. Create a new folder for reading and writing files under the User root directory displayed on the Rebex start screen.

SFTP Configuration

Create a global SFTP Configuration with details shown on Rebex screen.

<sftp:config name="SFTP_Config" doc:name="SFTP Config">
	<sftp:connection host="localhost" username="tester" password="password" />
</sftp:config>

Operations supported 

  1. List: Returns list of all files under a directory.
  2. Read: Read a specific file from a specified location.
  3. Write: Write to the specified directory.
  4. On New or Updated: Triggers the flow when a new file is created or updated which matches the criteria.
  5. Copy: Copy a given file.
  6. Move: Move a given file
  7. Delete: Deletes the specified file.
  8. Create Directory: Create a directory at the given path.
  9. Rename: Renames a file

List

This operation lists all the files in a directory. Returns an iterable object which is an array of payloads from each file.

sftp-list-flow

The directory whose files are listed is specified in the directory path

sftp-list-prop

The payload for each file can be accessed using payload[index].payload. The file metadata like name, creation time, size, path etc is saved in typedAttributes and can be accessed like payload[index].typedAttributes

sftp-list

The value of payloads in the returned array depends on whether it’s a subfolder or file. For subfolders the payload is null, and for the file it’s the content of the file. By default, List doesn’t look inside subfolders. This can be achieved by setting recursive to true. The operation can be customized to list files depending on the last updated time, name pattern, size etc, using File Matching Rules.

Read

Read operation returns the file content as payload and metadata in the attributes.

The MIME type of file is determined by file format but can be done using outputMimeType parameter is required to set explicitly.

Write

Writes the content to a file either by creating a new file or overwriting the existing one.

sftp-write

Parameters required by the Write operation

  1. Path: path where the file is written to
  2. Content: The content to write to file. The default value is
  3. Create Parent Directories: When checked, the target Path directory will be created when writing otherwise, the directory should be created beforehand.
  4. Write Mode: An Enumeration value for file write mode. OVERWRITE: overwrite existing file, default Value. APPEND: append the data to any existing file. Creates one if no file exists. CREATE_NEW: Create a new file every time, if any existing file, SFTP:FILE_ALREADY_EXISTS error is thrown.

On New or Updated File

This operation acts as a listener and polls the configured directory for any new or updated file, and triggers the flow when there is one.

sftp-listen

Parameters required by this operation

  1. Directory: Directory to poll for listening.
  2. Matcher: Matcher used to specify the criteria to pick up the file.
  3. Scheduling Strategy: Configures the scheduler that triggers the polling.
  4. Recursive: Whether to poll files in the subdirectories also.
  5. Auto Delete: Whether to delete the file after reading. False by default.
  6. Move To Directory: Each processed file be moved to the directory specified takes absolute path. Also, if the file is already in the target directory, it will not be moved.
  7. Rename To: Rename the file when moving.

Copy and Move

SFTP connector supports operations to copy or move a directory/file.

sftp-copy

<sftp:copy doc:name="Copy" config-ref="SFTP_Config" sourcePath="/poc/test.txt" targetPath="/poc/New Folder/" />
<sftp:move doc:name="Move" config-ref="SFTP_Config" sourcePath="/poc/test.txt" targetPath="/poc/New Folder"/>

Delete

The function Deletes the file or directory specified in the path.

<sftp:delete doc:name="Delete" config-ref="SFTP_Config" path="/poc/Data"/>

Throws FILE:ILLEGAL_PATH error if specified file is not present.

 

Create Directory

Creates a new directory on Directory Path. Directory Path should be an absolute path. Throws SFTP:FILE_ALREADY_EXISTS error if directory already exists.

<sftp:create-directory doc:name="Create directory" config-ref="SFTP_Config" directoryPath="/poc/Data"/>

Rename

Renames the file

<sftp:rename doc:name="Rename" config-ref="SFTP_Config" path="/poc/test.txt" to="test-3.txt"/>

 

This is all about STFP in MuleSoft. Feel free to leave comments on this article.

FInd more tips and tricks on MuleSoft at our Resource Centre.

 

 

 

SFTP in MuleSoft

Leave a Reply

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