To Read a File Using the SFTP Connector
The SFTP connector can read a file at any point in the flow, unlike in Mule 3, where the transport can only read files as a result of inbound endpoint polling.
Here is the syntax for reading a file:
1 2 3 4 <sftp:read path="#[path]" lock="true|false" outputEncoding="UTF-8" outputMimeType="application/xml" />
The operation above reads the file in the given path. It returns a Mule message (
MuleMessage) with the following attributes:
The file’s content as the payload.
The file’s metadata in the message attributes.
If the file does not exist, you receive a
SFTP:ILLEGAL_PATHerror. Note that the operation does not read directories.
This connector tries to determine a file’s mime type (
mimeType) based on its extension. However, when first-hand knowledge of the file’s content is necessary, you can force that
mimeType to a particular value by using the
The same process works for encoding. By default, the connector assumes that Mule runtime’s default encoding matches the one in the file. However, you can set the encoding through the
DataWeave expressions are embeddable inside operations that generate payloads and other values. Having the correct
mimeType set helps DataWeave auto-assign types and, in turn, also generate the correct outputs. It also improving the user experience by maximizing the use of DataSense’s functionality.
File system level locks are not possible in FTP or SFTP, so the the operation uses a Mule lock that only protects the file from other flows in the same Mule app. It does not protect the file from external processes. However, if the Mule app is running on a cluster, the lock will be distributed.
The lock is automatically released when one of the following occurs:
The Mule flow that locked the file ends.
The file content has been fully read.
Note that if the file is already locked by someone else, the connector will not be able to lock it, and you will receive a
The List operation makes use of the repeatable streams functionality introduced in Mule 4. The operation returns a List of Messages, where each message represents a file in the list and holds a stream to the file. That stream is repeatable by default.
For more information on this topic, see About Streaming in Mule 4.0.