Write a File Using the SFTP Connector Example - Mule 4
Anypoint Connector for SFTP (SFTP Connector) provides a Write operation that 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:
-
In the Mule Palette view, search for
sftp
and select the Write operation. -
Drag the Write operation onto the Studio canvas.
-
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.
-
Specify the connection information and click OK.
-
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.
In the Configuration XML editor, the <sftp:write>
configuration looks like this:
<sftp:write path="output.csv" />
Use Embedded DataWeave Transformations
If you place a Transform Message 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 field 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 field:
In the Configuration 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 field, set the Create parent directories field 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:
In the Configuration XML editor, the configuration looks like this:
<sftp:write path="a/b/c/myFile.txt" />
Use Write Strategy
Use the Write strategy field to configure the strategy to write the file. You can configure the field to select either of these strategies:
-
STANDARD
This is the default strategy that you can use in theAPPEND
,OVERWRITE
, andCREATE_NEW
modes. -
CUSTOM
This strategy enables you to manually specify the offset for the server to write the file inAPPEND
mode, particularly only for older SFTP servers that do not directly support write modes.
Additionally, you can configure the buffer size for writing into the file via the Buffer size for write strategy field.
Use the CUSTOM write strategy only in APPEND mode as it is not applicable to the OVERWRITE or CREATE_NEW modes. Use this strategy only if the STANDARD strategy results in errors during file writing in APPEND mode. Concurrency is not maintained for servers that do not follow SFTP guidelines.
|
The following example shows the configuration for both fields:
In the Configuration XML editor, the configuration looks like this:
<sftp:write path="testFile.csv" mode="APPEND" writeStrategy="CUSTOM" bufferSizeForWriteStrategy="BUFFER_SIZE_16KB" config-ref="SFTP_Config"/>
Write to Existing Files
When writing content to a file, set the Write Mode field 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.