Contact Free trial Login

SFTP Connector - Mule 4

Support Category: Select

SFTP Connector v1.3

Anypoint Connector for SFTP (SFTP Connector) provides access to files and folders on a SFTP server. The connector’s main features include:

  • The ability to read files or fully list directory contents on demand.

  • Support for common SFTP 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.

Release Notes: SFTP Connector Release Notes
Exchange: SFTP Connector

POM File Information


Mule converts RELEASE to the latest version. To specify a version, view Anypoint Exchange and click Dependency Snippets.

Connect to an SFTP Server

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

<sftp:config name="sftp">
  <sftp:connection username="anonymous" password="password"
    host="localhost" port="${ftpPort}"

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.

Connect to an SFTP Server Through a Proxy

To connect through a proxy, use a setup such as this:

<sftp:config name="sftp">
  <sftp:connection username="muletest1" password="muletest1"
    host="" port="${SFTP_PORT}" workingDir="${workingDir}">
      <sftp:sftp-proxy-config host="localhost" port="${proxyPort}" protocol="HTTP"/>

The protocol can be HTTP, SOCKS4, or SOCKS5.

Dynamic SFTP 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 emits 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 SFTP 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 SFTP 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.

<sftp:config name="FTP_Config" doc:name="FTP Config" >

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


<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:set-payload ><![CDATA[%dw 2.0
output application/java
payload map using (row = $) {
   host: row.Host,
   user: row.User,
   password: row.Password

  <foreach doc:name="For Each" >
    <sftp:write config-ref="SFTP_Config" doc:name="Write" path="demo.txt">
    <sftp:content >#[content]</sftp:content>
<set-payload doc:name="Set Payload" value="Multicast OK"/>
  • This sample application defines an SFTP 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 SFTP 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 SFTP destinations.

  • On each foreach iteration, each of the expressions in the SFTP config resolves to a different value, establishing different connections to each of the servers.

Example: SFTP Write

The next is example is identical to the previous one, except that it uses an SFTP Write operation.

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

<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)
    <sftp:write path="demo.txt" config-ref="ftp"> (5)

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

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

  • The previous examples 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 SFTP 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.


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, and so on. 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.