Repository navigation
Fix S3ToSFTPOperator and S3ToFTPOperator failing on S3 folder-marker keys - #77514
rokadepiyush49-rgb wants to merge 2 commits into
Conversation
…keys
With s3_filenames="*", both operators transferred every key that
list_keys returned, including the folder markers (keys ending in "/")
that the S3 console creates for each folder. Storing such a key as a
file at a directory path fails on the server ("550 Is a directory." on
FTP), so the task failed before any real file was moved.
Skip every key ending in "/" right after listing, and log the skipped
keys. Unlike the object-store transfers fixed in apache#76408, an SFTP or FTP
server cannot hold a file whose path ends in "/", so a marker that no
other key overlaps is skipped too.
closes: apache#77513
Generated-by: Claude Code
yuseok89
left a comment
There was a problem hiding this comment.
Left one inline comment. If it's feasible, could you check whether creating the directories would work here?
| # A key ending in "/" is a folder marker, such as the S3 console creates for each | ||
| # folder. An SFTP server cannot store a file at a path ending in "/", so skip every | ||
| # marker, not only those that overlap other keys as the object-store transfers do. | ||
| folder_markers = [k for k in all_keys if k.endswith("/")] |
There was a problem hiding this comment.
With this change, S3ToSFTPOperator would skip every folder marker, while GCSToSFTPOperator, which writes to the same kind of destination, creates each marker as a directory by default since #73880. So the same empty folder would end up on the SFTP server or not, depending on the source bucket.
What do you think about creating the directory here too, instead of skipping it? The same could apply to S3ToFTPOperator, since FTP has directories as well.
There was a problem hiding this comment.
Thanks, good point. Since #73880 makes GCSToSFTPOperator create markers as directories, I've changed both operators to do the same with "*": each folder marker is created as a directory, with any missing parents, before any file is copied, and an existing directory is accepted, so a rerun works. A string prefix still doesn't select markers, since they have no file name.
S3ToSFTPOperator uses paramiko through SSHHook (amazon doesn't depend on the sftp provider), so it has a small mkdir -p helper. On FTP, MKD answers 550 for an existing directory, so that helper checks with CWD first. Directory paths go through validate_destination_path, like files.
I tried it against real servers (pyftpdlib and asyncssh) with reports/, reports/sub/, an empty reports/empty/ and reports/deep/nested/ with no marker for deep/: every directory was created, the files landed in them, and a second run passed too.
I didn't add a switch like create_intermediate_dirs, since markers used to fail the task, so there's no old behavior to keep. Unlike GCSToSFTPOperator, this doesn't create the parent directory of a file that has no marker; I left that out to keep the PR focused, but I can add it if you'd prefer full parity.
There was a problem hiding this comment.
Thanks for taking this into account! I ran it on my side with a real S3 bucket and local SFTP and FTP servers, and it works well.
…fers Instead of skipping folder markers with s3_filenames="*", create each one as a directory on the server, with any missing parents, before any file is transferred. This matches GCSToSFTPOperator since apache#73880, so the same empty folder reaches the server whichever bucket it comes from. An existing directory is accepted, so a rerun works, and directory paths go through validate_destination_path like file paths. S3ToSFTPOperator uses paramiko through SSHHook, since the amazon provider does not depend on the sftp provider, so it gets a small mkdir -p helper. On FTP, MKD answers 550 for an existing directory, so the helper checks with CWD first. A string prefix still does not select folder markers. Generated-by: Claude Code
closes: #77513
With
s3_filenames="*",S3ToSFTPOperatorandS3ToFTPOperatortransfer every key thatlist_keysreturns. Folders created in the S3 console are stored as empty objects whose key ends in/. The operators tried to store those as files at a directory path, and the server rejected them (550 Is a directory.on FTP). S3 lists keys in sorted order, so the marker ofs3_keyitself comes first, and the task failed before any file was moved.Both operators now create each folder marker as a directory, with any missing parents, before any file is copied, as
GCSToSFTPOperatordoes since #73880. An existing directory is accepted, so a rerun works. A string prefix still doesn't select markers, since they have no file name, and they no longer appear in the warning about files the prefix did not select. Thes3_filenamesdocstring describes this.S3ToSFTPOperatoruses paramiko throughSSHHook(amazon doesn't depend on the sftp provider), so it has a smallmkdir -phelper. On FTP,MKDanswers 550 for an existing directory, so that helper checks withCWDfirst. Directory paths go throughvalidate_destination_path, like files.Not changed: an explicit list (
s3_filenames=[...]) and single-file mode, where nothing is listed. UnlikeGCSToSFTPOperator'screate_intermediate_dirs, the parent directory of a file that has no marker is not created, so such nested keys still need the destination subdirectory to exist; that is existing behavior, noted in the issue.Testing
test_execute_creates_folder_markers_as_directories(missing parents created, an empty folder kept, an existing directory accepted, every directory before any file),test_execute_fails_on_folder_marker_that_cannot_be_a_directory(a file at the marker path, and a marker escaping the destination path), and a prefix case where a marker is neither selected, created nor reported.test_s3_to_ftp.pyandTestS3ToSFTPOperatorInit: 33 passed. The SSH-server tests inTestS3ToSFTPOperatorneed a local sshd and were left to CI.reports/,reports/jan.csv,reports/sub/,reports/sub/two.txt, an emptyreports/empty/andreports/deep/nested/with no marker fordeep/: every directory was created, the files landed in them, and a second run passed too.prek runon the changed files: all hooks passed.Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code following the guidelines