Contact Us 1-800-596-4880

Connect to an SFTP Server Example - Mule 4

The following examples show how to configure Anypoint Connector for SFTP (SFTP Connector) global configuration element to:

Connect to an SFTP Server

To configure the global element to connect to an SFTP Server, follow these steps:

  1. Select the name of the connector in the Studio canvas.

  2. Select the operation in the Studio canvas.

  3. In the configuration screen for the operation, click the plus sign (+) next to the Connector configuration field to access the global element configuration fields.

  4. On the General tab, specify the connection information for the connector.

  5. Set the Working Directory field to ${workingDir}.
    This is root of every relative path used with this connector. If it’s not provided, the value defaults to the remote server.

  6. Set the Host field to specify the SFTP server host, for example localhost or 192.168.0.1.

  7. Set the Port field to ${sftpPort}.

  8. Set the Username field to anonymous and the Password field to password.

  9. On the Advanced tab, optionally specify timeout configuration and reconnection strategy.

  10. Click Test Connection to confirm that Mule can connect with the specified server.

  11. Click OK.

SFTP Connector Global Element Configuration

In the Configuration XML editor, the configuration sftp:config looks like this:

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

Connect to an SFTP Server Through a Proxy

To configure the global element to connect to an SFTP server through a proxy, follow these steps:

  1. Select the name of the connector in the Studio canvas.

  2. Select the operation in the Studio canvas.

  3. In the configuration screen for the operation, click the plus sign (+) next to the Connector configuration field to access the global element configuration fields.

  4. On the General tab, specify the connection information for the connector.

  5. Set the Working Directory field to ${workingDir}.
    This is root of every relative path used with this connector. If it’s not provided, the value defaults to the remote server.

  6. Set the Host field to 127.0.0.100.

  7. Set the Port field to ${SFTP_PORT}.

  8. Set the Username field to muletest1 and the Password field to password.

  9. For the Sftp proxy config field, select Edit inline, and set the following fields:

    • Host: localhost

    • Port: ${proxyPort}

    • Protocol: HTTP
      The protocol can be either HTTP or SOCKS5.

  10. On the Advanced tab, optionally specify a timeout configuration and reconnection strategy.

  11. Click Test Connection to confirm that Mule can connect with the specified server.

  12. Click OK.

SFTP Connector Global Element Configuration

In the Configuration XML editor, the configuration sftp:config looks like this:

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

Configure a Public Key to Connect to an SFTP Server

The following example shows how to configure a public key as the preferred authentication method in your SFTP configuration. The example uses a PEM (Privacy Enhanced Mail) format certificate.

  1. In Studio, navigate to the Global Elements tab.

  2. Click Create.

  3. Search for sftp.

  4. Select SFTP Config.

  5. Click OK.

  6. In the Global Element Properties window, set Preferred authentication methods to Edit inline.

  7. Click the plus sign to add a new value.

  8. In the Preferred authentication method window, set Value to PUBLIC_KEY.

  9. Click the plus sign to add a new value.

  10. Set Value to PASSWORD.

  11. Set Host to sftp.host.com.

  12. Set Port to 22.

  13. Set Username to sftp username.

  14. Set Password to sftp password.

  15. Set Identity file to the PEM location folder, for example Sftp-test-connection/src-main/resources/private.pem.

  16. Set Passphrase to the password of the identity file.

  17. Click OK.

SFTP Connector Global Element Configuration for public key

In the Configuration XML editor, the configuration looks like this:

<sftp:config name="SFTP_Config" doc:name="SFTP Config" >
		<sftp:connection host="sftp.host.com" username="sftp username" password="sftp password" identityFile="Sftp-test-connection/src-main/resources/private.pem" passphrase="passwordfile" >
			<sftp:preferred-authentication-methods >
				<sftp:preferred-authentication-method value="PUBLIC_KEY" />
				<sftp:preferred-authentication-method value="PASSWORD" />
			</sftp:preferred-authentication-methods>
		</sftp:connection>
	</sftp:config>
View on GitHub