Contact Free trial Login

About the 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.

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 config for 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 <foreach> component to iterate over each of the lines in the CSV file.

  5. On each <foreach> iteration, 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>
  • 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.

Example: FTP Write

<ftp:config name="ftp"> (1)
  <ftp:connection host="#[payload.host]" username="#[payload.user]" password="#[payload.password]" />
</ftp:config>

<flow name="ftpMultitenant" >
  <http:listener config-ref="HTTP_Listener_config" path="/multitenant"/> (2)
  <set-variable variableName="content" value="#[payload]" />
  <file:read path="recipients.csv" outputMimeType="application/csv" /> (3)

  <foreach> (4)
    <ftp:write path="demo.txt" config-ref="ftp"> (5)
      <ftp:content >#[vars.content]</ftp:content>
    </ftp:write>
  </foreach>

  <set-payload value="Multicast OK"/>
</flow>

Notes on Mule 4 behavior (for Mule 3 users):

  • The examples above use the File connector to read a file in the middle of the flow. The information posted through the <http:listener> component is written to each FTP site multiple times. Because the component makes use of the repeatable streams feature, you do not need to worry about consuming the stream multiple times.

  • The <foreach> component automatically goes through each line of the CSV file. In Mule 3, you first needed to transform the CSV file into a Java structure, but because Mule 4 is now Java agnostic, this works out-of-the-box.

When reading or listing files, you might be interested in the file’s metadata (for example, the file name, full path, size, timestamp, and so on). The connector uses the Mule Message Attributes to access this information.

Representation

Mule 4 provides a message structure utilizing a pair of payload and attributes. While the payload is the same as Mule 3, in Mule 4 the data moves around. This means that the payload is a file’s content. The attributes provide an object that contains metadata on the payload and information such as the file’s name, path, size, timestamps, etc. For the FTP and SFTP connectors these attributes are known as the file attributes.

The Mule message contains the concepts of encoding and MIME type. These are used to describe the format in which a payload is represented. The file attributes describe a file’s encoding and MIME type.