Contact Us 1-800-596-4880

Write a File Using the SFTP Connector Example - Mule 4

Anypoint Connector for SFTP (SFTP Connector) Write operation writes content into the given path on demand.

Configure the Write Operation in Studio

To add and configure the Write operation in Studio, follow these steps:

  1. In the Mule Palette view, search for sftp and select the Write operation.

  2. Drag the Write operation onto the Studio canvas.

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

  4. Specify the connection information and click OK.

  5. In the General tab, set the Path field to output.csv to set the path of the file to write.
    By default, the connector writes whatever is in the message payload.

The following screenshot shows the Write operation configuration:

Write operation configuration in Studio
Figure 1. Write operation configuration

In the XML editor, the <sftp:write> configuration looks like this:

<sftp:write path="output.csv" />

Use Embedded DataWeave Transformations

If you place a Transform component before the Write operation, the message payload changes, and that impacts the operation that is placed after the Write operation. Therefore, if the payload is in a different format and you need to transform it before writing it, you can place the transformation inside the Content parameter of the Write operation to generate content to be written without affecting the message in transit.

The following example places the DataWeave transformation in the Content parameter:

Write operation configuration in Studio with Content parameter
Figure 2. Write operation configuration with Content parameter

In the XML editor, the configuration looks like this:

<sftp:write path="output.csv">
  <sftp:content>#[%dw 2.0
                   output application/csv
                   ---
                   payload.customers.email
                  ]
  </sftp:content>
</sftp:write>

Write into Directories

Because the Write operation fails by default if you set nonexistent directories in the Path parameter, set the Crete parent directories parameter to True (Default) to automatically create any missing directories.

The following example shows that if any of the a, b, or c directories set in the Path field do not exist, the connector creates them:

.Write operation configuration with Create parent directories
Figure 3. Write operation configuration with Create parent directories

In the XML editor, the configuration looks like this:

<sftp:write path="a/b/c/myFile.txt" />

Write to Existing Files

When writing content to a file, set the Write Mode parameter to any of the following modes:

  • OVERWRITE (Default)
    If the file exists, overwrite it completely.

  • APPEND
    If the file exists, add its content to the end of an existing file.

  • CREATE_NEW
    Create a new file. If the file already exists, then you receive an error.

Locks

The Write operation supports locking similar to the Read operation. The main difference is that the lock releases automatically after the Write operation finishes. Set the Lock parameter to any of the following modes:

  • TRUE

  • FALSE (Default)

View on GitHub