camel-component-file
File component for rust-camel
Overview
The File component provides file system integration for rust-camel. It can read files from directories (consumer) and write files to directories (producer), supporting various file handling strategies.
Features
- Consumer: Poll directories for new files
- Producer: Write files with various strategies, streaming directly to disk (zero-copy)
- File filtering: Include/exclude patterns (regex)
- Post-processing: Delete, move, or no-op after processing
- Recursive directory scanning
- Atomic writes: Override strategy writes to a temp file then renames (panic-safe cleanup)
- Streaming: Zero-copy reads (consumer) and writes (producer) via
Body::into_async_read() - Security: Path traversal protection
Installation
Add to your Cargo.toml:
[]
= "0.2"
URI Format
file:directoryPath[?options]
Consumer Options
| Option | Default | Description |
|---|---|---|
delay |
500 |
Poll interval in milliseconds |
initialDelay |
1000 |
Initial delay before first poll |
noop |
false |
Don't delete or move files after processing |
delete |
false |
Delete files after processing |
move |
.camel |
Directory to move processed files to |
include |
- | Regex pattern for files to include |
exclude |
- | Regex pattern for files to exclude |
recursive |
false |
Scan subdirectories |
fileName |
- | Only process files matching this name |
readTimeout |
30000 |
Timeout for reading files (ms) |
Producer Options
| Option | Default | Description |
|---|---|---|
fileName |
- | Output file name (or use CamelFileName header) |
fileExist |
Override |
Strategy: Override, Append, Fail |
tempPrefix |
- | Temp file prefix for atomic writes |
autoCreate |
true |
Create directories automatically |
writeTimeout |
30000 |
Timeout for writing files (ms) |
Usage
Consumer: Read Files
use RouteBuilder;
use FileComponent;
let route = from
.log
.to
.build?;
Consumer: Delete After Processing
let route = from
.process
.build?;
Consumer: Move After Processing
let route = from
.to
.build?;
Consumer: Filter by Pattern
// Only process .csv files
let route = from
.to
.build?;
Producer: Write Files
let route = from
.set_body
.set_header
.to
.build?;
Producer: Append Mode
let route = from
.set_header
.to
.build?;
Producer: Atomic Writes
let route = from
.set_header
.to
.build?;
Exchange Headers
Consumer Headers
| Header | Description |
|---|---|
CamelFileName |
Relative file path |
CamelFileNameOnly |
File name only |
CamelFileAbsolutePath |
Absolute file path |
CamelFileLength |
File size in bytes |
CamelFileLastModified |
Last modified timestamp |
Producer Headers
| Header | Description |
|---|---|
CamelFileName |
Output file name (required if no fileName option) |
CamelFileNameProduced |
Absolute path of written file |
Example: File Pipeline
use RouteBuilder;
use FileComponent;
use CamelContext;
async
Streaming & Memory Management
The File component streams data directly between the body and disk using tokio::io::copy — no intermediate buffers. Both consumer (reading) and producer (writing) operate without materializing the full payload in RAM, making it suitable for arbitrarily large files.
Global Configuration
Configure default file polling and timeout settings in Camel.toml that apply to all File endpoints:
[]
= 500 # Poll interval (default: 500)
= 1000 # Initial delay before first poll (default: 1000)
= 30000 # Read timeout (default: 30000)
= 30000 # Write timeout (default: 30000)
URI parameters always override global defaults:
// Uses global delay (500ms)
.from
// Overrides delay from global config
.from
Profile-Specific Configuration
[]
= 500
[]
= 1000 # Less frequent polling in production
= 60000 # Longer timeout for large files
Known Limitation
Due to how the #[derive(UriConfig)] macro bakes defaults into the generated code, the global configuration uses duration comparison: if a URI-specified duration equals the macro's default, it will be overridden by the global config value. Explicitly setting a URI parameter to its default value (e.g., ?delay=500) is indistinguishable from "not set" and will use the global config.
Security
The File component includes protection against path traversal attacks. Attempts to write files outside the configured directory (e.g., using ../ in the filename) will be rejected with an error.
Documentation
License
Apache-2.0
Contributing
Contributions are welcome! Please see the main repository for details.