MuleSoft Technical Guides
SFTP in MuleSoft
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.
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.
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>
- List: Returns list of all files under a directory.
- Read: Read a specific file from a specified location.
- Write: Write to the specified directory.
- On New or Updated: Triggers the flow when a new file is created or updated which matches the criteria.
- Copy: Copy a given file.
- Move: Move a given file
- Delete: Deletes the specified file.
- Create Directory: Create a directory at the given path.
- Rename: Renames a file
This operation lists all the files in a directory. Returns an iterable object which is an array of payloads from each file.
The directory whose files are listed is specified in the directory path
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
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 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.
Writes the content to a file either by creating a new file or overwriting the existing one.
Parameters required by the Write operation
- Path: path where the file is written to
- Content: The content to write to file. The default value is
- Create Parent Directories: When checked, the target Path directory will be created when writing otherwise, the directory should be created beforehand.
- 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.
Parameters required by this operation
- Directory: Directory to poll for listening.
- Matcher: Matcher used to specify the criteria to pick up the file.
- Scheduling Strategy: Configures the scheduler that triggers the polling.
- Recursive: Whether to poll files in the subdirectories also.
- Auto Delete: Whether to delete the file after reading. False by default.
- 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.
- Rename To: Rename the file when moving.
Copy and Move
SFTP connector supports operations to copy or move a directory/file.
<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"/>
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.
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"/>
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.