Connect to an FTP Server using MuleSoft

Comments · 182 Views

This connector provides access to files and folders on an FTP server. Its main features include:o A design that is fully consistent with the File and SFTP connectors. The same set of operations is available on all these connectors, and they behave similarly.

FTP Connector

This connector provides access to files and folders on an FTP server. Its main features include:

  • The ability to read files or fully list directory contents on demand.
  • Support for common FTP operations such as creating directories and copying, moving, renaming, and deleting files.
  • Support for locking files.
  • File matching functionality.
  • A design that is fully consistent with the File and SFTP connectors. The same set of operations is available on all these connectors, and they behave similarly.

Connect to an FTP Server

You set up a connection through an ftp:config element, for example:

ftp:config name="ftp"
ftp:connection username="anonymous" password="password" host="localhost" port="${ftpPort}" workingDir="${workingDir}"/
/ftp:config

The workingDir provides the path to a directory that is treated as the root of every relative path used with this connector. If the directory is not provided, the remote server’s default serves as the working directory. The username, password, host, and port attributes are self explanatory. For more additional info Mulesoft Online Training

Dynamic FTP Connections

Many integrations require connecting to different servers depending on a certain condition.

Examples of this include:

  • Connect to different invoice storage servers depending on the branch that emit an invoice.
  • Connect to different servers depending on an integration subject, such as in a multi-tenant use case.

To accommodate these use cases, the config element supports expressions, which makes it possible for connection parameters to evaluate these conditions and connect to the correct server.

Dynamic FTP Connection Example

The following example shows a dynamic multicast application that:

  1. Defines a configfor the connector in which the host, username, and password are expressions.
  2. Describes a flow in which content is posted through HTTP.
  3. Uses the File connector to read a CSV file that contains a random set of FTP destinations with columns such as host, user, and port.
  4. Uses a foreachcomponent to iterate over each of the lines in the CSV file.
  5. On each foreachiteration, resolves each expression in the connector config to a different value, establishing different connections to each of the servers.

ftp:config name="FTP_Config" doc:name="FTP Config"

ftp:sftp-connection host="#[payload.host]" username="#[payload.user]" password="#[payload.password]" /

/ftp:config

flow name="streaming-multitenantFlow"
http:listener config-ref="HTTP_Listener_config" path="/multitenant"
doc:name="Listener" /
set-variable variableName="content" value="#[payload]" doc:name="Variable" /
file:read config-ref="File_Config" path="recipients.csv" doc:name="Read"
outputMimeType="application/csv" /
ee:transform doc:name="Transform Message"
ee:message

ee:set-payload ![CDATA[%dw 2.0
output application/java

payload map using (row = $) {
host: row.Host,
user: row.User,
password: row.Password
}]]

/ee:set-payload
/ee:message
/ee:transform
foreach doc:name="For Each"
ftp:write config-ref="FTP_Config" doc:name="Write" path="demo.txt"
ftp:content #[content]/ftp:content
/ftp:write
/foreach
set-payload doc:name="Set Payload" value="Multicast OK"/
/flow

For more Mulesoft Training

  • This sample application defines an FTP config in which the host, username, and password are expressions.
  • It uses a flow in which a random content is posted.
  • It uses the file connector to load a recipients file, which is a CSV file containing a random set of FTP destinations.
  • There’s a DataWeave transformation that splits a CSV file.
  • The application uses a foreach element to write the contents into each of the FTP destinations.
  • On each foreach iteration, each of the expressions in the FTP config resolves to a different value, establishing different connections to each of the servers.

 

Comments
meritstep technologies 7 months ago

Thanks for sharing this information. I really Like Very Much.
<a href="https://www.meritstep.com/courses/mulesoft">mulesoft online training</a>
<a href="https://www.meritstep.com/courses/mulesoft">best mulesoft online training</a>
<a href="https://www.meritstep.com/courses/mulesoft">top mulesoft online training</a>