Camel SFTP
Last updated
Was this helpful?
Last updated
Was this helpful?
Apache Camel’s SFTP component allows to transfer files to and from SFTP (SSH File Transfer Protocol) servers. It supports both the sending (uploading) and receiving (downloading) of files, making it useful in various integration scenarios where secure file transfer is required.
For more details, refer to official documentation page -
The basic URI format for the Camel SFTP component looks like this:
username@hostname
: Specifies the username and the hostname of the SFTP server.
:port
: Optional; the port number. The default SFTP port is 22.
directoryname
: The remote directory on the SFTP server.
options
: Additional configuration options, like connection timeouts, key-based authentication, or polling intervals.
Example:
In this example, Camel downloads files from the /data
directory of the SFTP server myhost.com
and stores them in the local /local/storage
directory.
The Camel SFTP component provides numerous options to fine-tune the behavior of file transfers. Here are some commonly used options:
fileName: Specifies a specific file to download or upload.
binary: Set to true
to treat the files as binary files. The default is false
, meaning files are transferred in ASCII mode.
disconnect: Whether or not to disconnect from the SFTP server after each operation. The default is false
.
delay: Sets the polling interval (in milliseconds) for the SFTP server. By default, this is 500 milliseconds.
delete: Whether to delete files after they have been successfully transferred. The default is false
.
passiveMode: Whether to use passive mode. The default is false
.
Strict Host Key Checking: By default, SFTP follows strict host key checking. We can configure this using knownHostsFile
and strictHostKeyChecking
options.
Private Key Authentication: We can specify the private key file and its passphrase for secure authentication.
There are two main ways to authenticate when using SFTP in Camel: password-based and key-based.
Password Authentication: If the SFTP server uses password authentication, we can provide the password as an option in the URI.
Key-based Authentication: Key-based authentication is more secure and widely used for automated processes.
privateKeyFile
: Specifies the path to the private key file.
knownHostsFile
: Specifies the known hosts file, ensuring the server is trusted.
If the private key is encrypted, we can use the privateKeyPassphrase
option to provide the passphrase.
We can filter the files that are picked up by the SFTP component by using Camel's file filter options:
Ant-style file filtering:
This filter only includes files that match the *.txt
pattern.
Filtering by Last Modified Time: We can filter files by their last modified time using the noop
option (which prevents files from being deleted or moved) and a custom file filter.
To avoid processing the same file multiple times, we can use Camel’s idempotent consumer feature, which ensures that each file is processed only once.
The idempotent=true
option ensures the file is only picked up once, and we can specify a custom idempotent repository (e.g., #myRepo
) to manage file state.
In Apache Camel, message headers are key-value pairs that provide additional metadata for a message. These headers do not form part of the message body itself but are used to control routing, transformations, and component-specific behaviors.
When using the Camel SFTP component, we can set or retrieve certain message headers to influence how the component behaves during file transfers. The SFTP component supports a set of message headers that can control various aspects of the SFTP operations such as file handling, directory management, and more.
The SFTP component in Apache Camel automatically sets some of these message headers when it processes files. For instance, headers like CamelFileName
, CamelFileAbsolutePath
, and CamelFileLastModified
are typically set by the component during file read or write operations.
The Camel SFTP component supports the following 10 key message headers:
CamelFileName
Description: Specifies the name of the file being written or read.
Example Usage: We can use this header to set the target filename when writing to a remote SFTP server.
Example:
CamelFileNameOnly
Description: Contains only the name of the file (without the path).
Use Case: This can be useful when we need to extract just the filename from a full file path.
CamelFileAbsolutePath
Description: Contains the absolute file path of the file.
Use Case: Useful for logging or debugging to know the exact location of the file on the SFTP server.
CamelFileAbsolute
Description: A boolean flag indicating whether the file path is absolute or relative.
Use Case: Can help determine if the file is referenced with a full path or a relative one.
CamelFileRelativePath
Description: The relative file path, which is relative to the starting directory of the SFTP connection.
Use Case: This can help when working with files in a specific directory relative to the root SFTP folder.
CamelFileLastModified
Description: The timestamp (in milliseconds) of the last time the file was modified.
Use Case: We can use this to compare file modification times or to handle files based on their age.
CamelFileLength
Description: The size of the file in bytes.
Use Case: Helpful when we need to verify file sizes or log file size information during transfers.
CamelFtpReplyCode
Description: The FTP reply code received from the FTP/SFTP server.
Use Case: Useful for debugging and handling specific reply codes for error handling.
CamelFtpReplyString
Description: The full FTP reply message string from the FTP/SFTP server.
Use Case: Similar to CamelFtpReplyCode
, this is useful for troubleshooting and debugging.
CamelFtpUserDir
Description: The home directory of the user on the SFTP server.
Use Case: Can be helpful when needing to determine the base directory for the SFTP session.
To send (upload) files to an SFTP server, the following configuration can be used:
The local files in /local/files
will be uploaded to the /upload
directory on the SFTP server.
The noop=true
option ensures that files are not moved or deleted after processing locally.
To download files from an SFTP server, we can define a route like this:
Files are downloaded from the /download
directory on the SFTP server.
delete=true
: Deletes the file from the remote directory after a successful download.
delay=60000
: Polls the remote server every 60 seconds to check for new files.
Camel provides ways to handle errors during file transfer, such as retrying or logging errors. For instance:
We can download large files from an SFTP server using streaming, which reduces memory consumption by not loading the entire file into memory at once.
For more details, refer to the official documentation -