MuleSoft — FTP/SFTP API — List, RW, Delete
FTP and SFTP are widely used protocols for a file transfer from one system to another.
Anypoint Connectors are also present for both protocols, having file operations such as read, write, delete, list, etc.
In this article we will focus on 3 points:
- How to implement a system API for handling file transfer using FTP/SFTP protocol for various servers(systems)
- ListFiles operation using Java code.
- API usage
File Transfer System API — dynamic for multiple servers.
In this POC, there are 4 file operations that are supported for FTP and SFTP protocols:
- ListFiles
- Read file
- Write File
- Delete File
For ListFiles, please go through the next section of this article.
For the other 3 operations (Read, Write and Delete) we are using ‘Dynamic Evaluate’ to make the flow dynamic (i.e same flow to be used by all servers).
Step 1. Based on the query parameter in the request, the DW instance file is identified and properties are added to the target variable.
Step 2. Every server will have its own instance file. FTP/SFTP configuration properties are set using the property file values (password is secured using secure props).
Step 3. Configuration Properties are added.
Step 4. Dynamic Evaluate Flow is called before the operations to set the configuration for FTP /SFTP server.
ListFiles Operation
NOTE: the list operation in FTP/SFTP Anypoint Connector lists all the files (name + file data) present on the server as per the file matching rule provided.
Since the file data is also received in the response, there is a high chance that the flow will crash if there are multiple large-sized files on the server.
In our case, only the file names were required in the response.
We created the Java code to list the files present on FTP and SFTP servers.
Similar to other operations we are calling ‘Dynamic Evaluate’ to make the flow usable for all servers.
For both FTP and SFTP file listing, we added the Java package in src/main/java.
Java code is easy to understand. You will be able to easily find the Java method details on the internet.
API Usage:
FTP Operations:
- ListFiles
GET http://localhost:8081/api/v1.0/ftp/listfiles?system=sap&filepath=/test
2. Read File
GET http://localhost:8081/api/v1.0/ftp/file?system=sap&filepath=/test&filename=test.txt
3. Write File
POST http://localhost:8081/api/v1.0/ftp/file?system=sap&filepath=/test&filename=test.txt
4. Delete File
DELETE http://localhost:8081/api/v1.0/ftp/file?system=sap&filepath=/test&filename=test.txt
SFTP Operations:
- ListFiles
GET http://localhost:8081/api/v1.0/sftp/listfiles?system=sap&filepath=/test
2. Read File
GET http://localhost:8081/api/v1.0/sftp/file?system=sap&filepath=/test&filename=test.txt
3. Write File
POST http://localhost:8081/api/v1.0/sftp/file?system=sap&filepath=/test&filename=test.txt
4. Delete File
DELETE http://localhost:8081/api/v1.0/sftp/file?system=sap&filepath=/test&filename=test.txt
Link for the Jar file for reference.
https://drive.google.com/file/d/1Y2TtHb5GdHL4n3s9MYcrOCKzE6It4bTB/view?usp=sharing