Nav

About the SFTP Connector

The SFTP connector provides access to files and folders on a SFTP 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 four connectors, and they behave almost identically.

Connecting to an SFTP Server

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


         
      
1
2
3
4
5
<sftp:config name="ftp">
  <sftp:connection username="anonymous" password="password"
    host="localhost" port="${ftpPort}"
    workingDir="${workingDir}"/>
</sftp: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 Connections

Many integrations require connecting to different servers depending on a certain condition. Examples of this include:

  • Connecting to different invoice storage servers depending on the branch that emitted the invoice.

  • Connecting to different servers depending on a given integration subject, like in a multi-tenant use case.

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

The next examples shows a dynamic multicast app 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.

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

Example: Using SFTP Write

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<sftp:config name="ftp"> (1)
  <sftp:connection host="#[payload.host]" username="#[payload.user]" password="#[payload.password]" />
</sftp: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)
    <sftp:write path="demo.txt" config-ref="ftp"> (5)
      <sftp:content>#[vars.content]</sftp:content>
    </sftp: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 or 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.

Attributes

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. More information about file attributes are available in the reference for this connector.