NiFi跨FTP站点传输SFTP子文件夹文件失败求助
Hey Aadil, let's get this SFTP-to-FTP workflow working properly—your current setup is hitting a path mismatch issue, and we can fix it with a couple of adjustments.
First, let's diagnose the problem with your original config
When you set ListSFTP to target /rootfolder (with recursion enabled, I assume?), it generates FlowFiles with filenames that include the full relative path from /rootfolder—like subfolder1/file.txt, subfolder2/report.pdf, etc. But when you point each FetchSFTP to a fixed subfolder (e.g., /rootfolder/subfolder1), NiFi tries to combine that fixed path with the FlowFile's filename, resulting in invalid paths like /rootfolder/subfolder1/subfolder1/file.txt—which doesn't exist on your SFTP server. That's why the workflow fails.
Solution 1: Simplified Workflow (Recommended)
You don't need three separate FetchSFTP components. We can handle all subfolders with a single pipeline that preserves the directory structure:
Configure ListSFTP
- Remote Path:
/rootfolder - Recurse Subdirectories: Set to
true(this tells NiFi to scan all subfolders under/rootfolder) - Verify the generated FlowFiles have attributes like:
filename:subfolder1/file.txt(relative path from/rootfolder)absolute.path:/rootfolder/
- Remote Path:
Configure a Single FetchSFTP
- Remote Path: Leave this blank, or set it to
${absolute.path}(NiFi will automatically combine this with thefilenameattribute to locate the correct remote file—e.g.,/rootfolder/ + subfolder1/file.txt = /rootfolder/subfolder1/file.txt) - Fill in your SFTP server credentials and connection details as usual
- Remote Path: Leave this blank, or set it to
Configure PutFTP to Preserve Structure
- Remote Directory: Your target FTP root directory (e.g.,
/ftp_target_root) - Create Directory: Set to
true(NiFi will automatically createsubfolder1,subfolder2, etc., in the FTP target) - Remote Filename:
${filename}(this ensures files land in the matching subfolder—e.g.,/ftp_target_root/subfolder1/file.txt)
- Remote Directory: Your target FTP root directory (e.g.,
Solution 2: Split Workflow for Per-Subfolder Processing
If you need separate logic for each subfolder (e.g., different renaming, filtering), use a routing step to split the FlowFiles first:
- ListSFTP (same as Solution 1:
/rootfolderwith recursion enabled) - Add RouteOnAttribute
- Create three routing rules to split FlowFiles by their subfolder:
- Rule 1: Route to
subfolder1whenfilename starts with 'subfolder1/' - Rule 2: Route to
subfolder2whenfilename starts with 'subfolder2/' - Rule 3: Route to
subfolder3whenfilename starts with 'subfolder3/'
- Rule 1: Route to
- Create three routing rules to split FlowFiles by their subfolder:
- Configure FetchSFTP for Each Subfolder
- For the
subfolder1branch:- Remote Path:
/rootfolder/subfolder1 - Remote Filename:
${filename:substringAfter('subfolder1/')}(this strips thesubfolder1/prefix from the filename, so NiFi looks forfile.txtinstead ofsubfolder1/file.txtin/rootfolder/subfolder1)
- Remote Path:
- Repeat this pattern for the other two subfolders
- For the
- Configure PutFTP for Each Subfolder
- For the
subfolder1branch:- Remote Directory:
/ftp_target_root/subfolder1 - Create Directory:
true - Remote Filename:
${filename:substringAfter('subfolder1/')}
- Remote Directory:
- For the
Quick Troubleshooting Tips
- Use the
LogAttributecomponent afterListSFTPto inspect FlowFile attributes—this helps confirm thatfilenameandabsolute.pathare correctly set - Ensure your NiFi service account has read permissions on all
/rootfoldersubdirectories, and write permissions on the target FTP directories - Test with a small set of files first to validate the directory structure is preserved correctly
内容的提问来源于stack exchange,提问作者Aadil M

