FTPPage edited by Claus IbsenFTP/SFTP Component - Camel 1.x onlyThis component provides access to remote file systems over the FTP and SFTP protocols.
URI format
ftp://[usern...@]hostname[:port]/filename[?options]
sftp://[usern...@]hostname[:port]/filename[?options]
Where filename represents the underlying file name or directory. Can contain nested folders. If no username is provided then anonymous login is attempted using no password. You can append query options to the URI in the following format, ?option=value&option=value&... URI Options
Examplesftp://some...@someftpserver.com/public/upload/images/holiday2008?password=secret&binary=true
New default behavior for FTP/SFTP-Consumers in Camel 1.5The consumer will always skip any file which name starts with a dot, such as ".", ".camel", ".m2" or ".groovy". Only files (not directories) is matched for valid filename if options such as: consumer.regexPattern, consumer.excludeNamePrefix, consumer.excludeNamePostfix is used. The consumer recursive option will be changed from true to false as the default value. We don't feel that Camel out-of-the-box should recursive poll. The consumer will not use timestamp algorithm for determine if a remote file is a new file - see warning section above. To use the old behavior of Camel 1.4 or older you can use the option consumer.timestamp=true. Exclusive Read LockThe option readLock can be used to force Camel not to consume files that is currently in the progress of being written. However this option is default turned off, as it requires that the user has write access. There are other solutions to avoid consuming files that are currently being written over FTP, for instance you can write the a temporary destination and move the file after it has been written. Message HeadersThe following message headers can be used to affect the behavior of the component
Consumer propertiesWhen using FTPConsumer (downloading files from a FTP Server) the consumer specific properties from the File component should be prefixed with "consumer.". For example the delay option from File Component should be specified as "consumer.delay=30000" in the URI. See the samples or some of the unit tests of this component. Filename _expression_In Camel 1.5 we have support for setting the filename using an _expression_. This can be set either using the _expression_ option or as a string based File Language _expression_ in the org.apache.camel.file.name header. See the File Language for some samples. Camel 1.x Known issuesSee the timestamp warning. When consuming files (downloading) you must use type conversation to either String or to InputStream for ASCII and BINARY file types. In Camel 1.4 or below Camel FTPConsumer will poll files regardless if the file is currently being written. See the consumer.exclusiveReadLock option. Also in Camel 1.3 since setNames is default false then you must explicitly set the filename using the setHeader _expression_ when consuming from FTP directly to File.
private String ftpUrl = "ftp://camelri...@localhost:21/public/downloads?password=admin&binary=false";
private String fileUrl = "file:myfolder/?append=false&noop=true";
return new RouteBuilder() {
public void configure() throws Exception {
from(ftpUrl).setHeader(FileComponent.HEADER_FILE_NAME, constant("downloaded.txt")).convertBodyTo(String.class).to(fileUrl);
}
};
Or you can set the option to true as illustrated below:
private String ftpUrl = "ftp://camelri...@localhost:21/public/downloads?password=admin&binary=false&consumer.setNames=true";
private String fileUrl = "file:myfolder/?append=false&noop=true";
return new RouteBuilder() {
public void configure() throws Exception {
from(ftpUrl).convertBodyTo(String.class).to(fileUrl);
}
};
SampleIn the sample below we setup Camel to download all the reports from the FTP server once every hour (60 min) as BINARY content and store it as files on the local file system.
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
public void configure() throws Exception {
// we use a delay of 60 minutes (eg. once pr. hour we poll the FTP server
long delay = 60 * 60 * 1000L;
// from the given FTP server we poll (= download) all the files
// from the public/reports folder as BINARY types and store this as files
// in a local directory. Camel will use the filenames from the FTPServer
// notice that the FTPConsumer properties must be prefixed with "consumer." in the URL
// the delay parameter is from the FileConsumer component so we should use consumer.delay as
// the URI parameter name. The FTP Component is an extension of the File Component.
from("ftp://sc...@localhost/public/reports?password=tiger&binary=true&consumer.delay=" + delay).
to("file://target/test-reports");
}
};
}
And the route using Spring DSL:
<route>
<from uri="ftp://sc...@localhost/public/reports?password=tiger&binary=true&consumer.delay=60000"/>
<to uri="file://target/test-reports"/>
</route>
Using _expression_ for filenamesIn this sample we want to move consumed files to a backup folder using today's date as a sub foldername. Notice that the move happens on the remote FTP server. If you want to store the downloaded file on your local disk then route it to the File component as the sample above illustrates.
from(ftpUrl + "&_expression_=backup/${date:now:yyyyMMdd}/${file:name}").to("...");
See File Language for more samples. Consuming a remote FTP server triggered by a routeThe FTP consumer is build as a scheduled consumer to be used in the from route. However if you want to start consuming from a FTP server triggered within a route it's a bit cumbersome to do this in Camel 1.x (we plan to improve this in Camel 2.x). However it's possible as this code below demonstrates. In the sample we have a SEDA queue where a message arrives that holds a message containing a filename to poll from a remote FTP server. So we setup a basic FTP url as:
// we use delay=5000 to use 5 sec delay between pools to avoid polling a second time before we stop the consumer
// this is because we only want to run a single poll and get the file
private String getFtpUrl() {
return "ftp://ad...@localhost:" + getPort() + "/getme?password=admin&binary=false&delay=5000";
}
And then we have the route where we use Processor within the route so we can use Java code. In this Java code we create the ftp consumer that downloads the file we want. And after the download we can get the content of the file and put it in the original exchange that continues being routed. As this is based on an unit test it routes to a Mock endpoint.
from("seda:start").process(new Processor() {
public void process(final Exchange exchange) throws Exception {
// get the filename from our custome header we want to get from a remote server
String filename = exchange.getIn().getHeader("myfile", String.class);
// construct the total url for the ftp consumer
// add the fileName option with the file we want to consume
String url = "" + "&fileName=" + filename;
// create a ftp endpoint
Endpoint ftp = context.getEndpoint(url);
// create a polling consumer where we can poll the myfile from the ftp server
PollingConsumer consumer = ftp.createPollingConsumer();
// must start the consumer before we can receive
consumer.start();
// poll the file from the ftp server
Exchange result = consumer.receive();
// the result is the response from the FTP consumer (the downloaded file)
// replace the outher exchange with the content from the downloaded file
exchange.getIn().setBody(result.getIn().getBody());
// stop the consumer
consumer.stop();
}
}).to("mock:result");
Debug loggingThis component has log level TRACE that can be helpful if you have problems. See Also
Change Notification Preferences
View Online
|
View Change
|
Add Comment
|
- [CONF] Apache Camel > FTP confluence
- [CONF] Apache Camel > FTP confluence
- [CONF] Apache Camel > FTP confluence
- [CONF] Apache Camel > FTP confluence
- [CONF] Apache Camel > FTP confluence
- [CONF] Apache Camel > FTP confluence