Guide To MuleSoft’s SFTP Connector
Overview: –
- In the modern digital landscape, secure and efficient file transfer is a critical component for businesses, particularly when dealing with sensitive data.
- MuleSoft offers a robust solution for secure file transfers through its SFTP (Secure File Transfer Protocol) connector.
- In this blog, you will learn:
- What is SFTP and How to set-up an SFTP server.
- Instructions to set up the SFTP connector in MuleSoft.
- An example showing how to upload a file through an SFTP server.
What is SFTP: –
- SFTP stands for Secure File Transfer Protocol. It is a secure version of the File Transfer Protocol (FTP), which is used to transfer files between a client and a server over a network.
- SFTP provides encryption and authentication mechanisms to ensure that data is transferred securely
- It is essential for businesses that need to transfer files containing sensitive information, ensuring data privacy and security.
Key features of SFTP: –
- Security: SFTP encrypts both the data being transmitted and the authentication credentials used to access the server. This ensures that sensitive information remains protected from unauthorized access or interception
- Authentication: SFTP supports various authentication methods such as password authentication, public key authentication
Note: – For SFTP integration, there are several providers available, and for this instance, I am using Files.com..
Steps to create a Files.com account: –
- Click on the below link to register a Files.com account.
Get Instant Access · Files.com
2. Provide all the details such as the Company name, My name, Phone number, work mail, and Password.
3. Click on the check box and Click on Start my free 7-day trial. Then account will be created and it will dashboard
4. Click on New Floder
5. A new window will pop up and there provide folder name and click on create.
6. A new folder will be created and the Interface will look like this
7. Open the folder we created and you can check that there are no files in that folder
Make sure that you have noted down the points like: –
Host: – tgh.files.com (Hostname from the URL)
Port: – 22 (By default, SFTP uses port 22)
Username: – ********** (mail ID which is used for creating the files.com account)
Password: – ******** (which you have created while activating the account)
Directory: – /mule (folder name that was created before)
Setting up the SFTP Configuration: –
- Create a new project in Anypoint Studio. To create click on the “File” menu at the top left corner of the window. From the dropdown menu, select “New” and then choose “Mule Project”.
2. A dialog box will appear where you can enter details about your new Mule project. Provide a name for your project in the “Project name” field. And click on finish.
3. Add the SFTP module to the project by clicking on the Add modules in the mule palette
4. Drag and drop the SFTP module into the project and the SFTP module will be added to the project.
5. Now click on the global elements in the project file And click on create.
6. Search for sftp config and click on it. It will open the sftp configuration.
7. Provide the Host, Port, Username, and password as we noted before. Click on Test Connection and Check whether the connection is successful or not. If successful then Click on OK.
Operations that we can Perform using the SFTP Module: –
- There are many operations we can perform using the SFTP Module and They are: –
- Uploading Files: Use the Write component to upload files to the SFTP server.
- Downloading Files: Use the Read component to download files from the SFTP server.
- Listing Files: Use the List component to retrieve a list of files in a specific directory on the SFTP server.
- Deleting Files: Use the Delete component to remove files from the SFTP server.
- Creating New Folder: Use Create directory component to create a new directory in a SFTP server.
- Copying a file to another Directory: Use Copy component to copy files from one directory to another.
- Moving a file to another Directory: Use Move component to copy files from one directory to another.
- Rename a file: Use Rename component to rename a file.
- checking a directory for new or updated files: Use On New or Updated File component to Initiate a flow by polling a directory for files that have been created or updated and generates a message for each file found.
- Lets create a simple usecase to upload a file using SFTP connector.
Usecase: –
- Steps: –
- Drag and drop the HTTP Listener Connector into the project from the HTTP module. Configure the listener, and set the host to All interface (0.0.0.0), port number as 8081 by clicking on the (+) in Connector configuration. Set the path as /upload.
2. Drag a Read component from the file module and drop it on the canvas
3. Configure the Read component by providing a file path from a local directory by clicking on “…” (here I am reading a CSV file)
4. Drag and drop the logger from the core module and configure the message as #[payload] to log the payload from the Read component.
5. Drag and drop transform message component to the canvas and write a dataweave code to convert CSV data to JSON
6. Drag a Write component from the SFTP module and drop it on the canvas
7. Give path as /mule/Employees.json (/directoryName ++ /Filename) and Select SFTP config from drop down for connector configuration
8. Drag and drop the logger from the core module and configure the message as #[payload] to log the payload
9. Deploy the application, to deploy the application we have right click on the project, go to Run As-> Mule Application
10. After the application is deployed, Open Postman and hit our application, and the endpoint will be http://localhost:8081/upload and Once you hit the request you can check that JSON response(transformation of CSV to JSON).
11. Open the browser where you set up SFTP server and Open that folder we have created. There you can check that a new JSON file is created.
In this way, we can configure the SFTP Connection and integrate with the SFTP-enabled server from Mulesoft.