Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SFTP: Failed to transfer file if temporary remote directory doesn't exist #9684

Closed
sottaviani2 opened this issue Nov 26, 2024 · 5 comments
Closed

Comments

@sottaviani2
Copy link

5.5.20

Describe the bug
I use SftpMessageHandler to send files to a remote SFTP server.
I want to transfer files using a temporary remote directory. Transfer is aborted if the temporary remote doesn't exist, otherwise it works fine.

To Reproduce
Transfer file test.txt to /remote setting /remote_temp as temporary directory. /remote_temp doesn't exist on target server

Expected behavior
If /remote_temp doesn't exist on remote server and autoCreateDirectory is true, the file is sent to /remote and /remote_temp is created to store the file before moving to final destination.

Sample
SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory) ;
handler.setRemoteDirectoryExpressionString("headers['remoteDirectory']");
handler.setTemporaryRemoteDirectoryExpressionString("headers['remoteTempDirectory']");
handler.setAutoCreateDirectory(true);

@sottaviani2 sottaviani2 added status: waiting-for-triage The issue need to be evaluated and its future decided type: bug labels Nov 26, 2024
@artembilan
Copy link
Member

That version is out of support for a while. Any chances to check your solution with much newer one: https://spring.io/projects/spring-integration#learn ?

@sottaviani2
Copy link
Author

That version is out of support for a while. Any chances to check your solution with much newer one: https://spring.io/projects/spring-integration#learn ?

I think 6.4.0 has the same problem. I check source code and find out that the bug is in org.springframework.integration.file.remote.RemoteFileTemplate on method sendFileToRemoteDirectory
It tries to create only remoteDirectory. I suppose that you must check for existence of the temporary directory
and create it.

@artembilan
Copy link
Member

Ok. I’m out for vacation until next week. So, if you have some idea how to fix, feel free to open PR and I’ll look shortly when I’m back .
Thank you!

@artembilan
Copy link
Member

Essentially, the RemoteFileTemplate is missing to create that temporary directory here:

		String remoteDirectory = normalizeDirectoryPath(remoteDirectoryArg);
		String temporaryRemoteDirectory = normalizeDirectoryPath(temporaryRemoteDirectoryArg);

		String remoteFilePath = remoteDirectory + fileName;
		String tempRemoteFilePath = temporaryRemoteDirectory + fileName;
		// write remote file first with temporary file extension if enabled

		String tempFilePath = tempRemoteFilePath + (this.useTemporaryFileName ? this.temporaryFileSuffix : "");

		if (this.autoCreateDirectory) {
			try {
				RemoteFileUtils.makeDirectories(remoteDirectory, session, this.remoteFileSeparator, this.logger);
			}
			catch (@SuppressWarnings("unused") IllegalStateException e) {
				// Revert to old FTP behavior if recursive mkdir fails, for backwards compatibility
				session.mkdir(remoteDirectory);
			}
		}

@sottaviani2
Copy link
Author

Correct. I used this code as a workaround for my project
RemoteFileUtils.makeDirectories(temporaryRemoteDirectory, session, this.remoteFileSeparator, this.logger);
only when temporaryRemoteDirectory is specifed

spring-builds pushed a commit that referenced this issue Dec 12, 2024
Fixes: #9684
Issue link: #9684

The `RemoteFileTemplate` is missing to create a temporary remote directory

* Fix `RemoteFileTemplate.sendFileToRemoteDirectory()` to create a `temporaryRemoteDirectory`
as well if it is different from already created `remoteDirectory`
* Add `SftpServerOutboundTests.autoCreateTemporaryDirectory()` to verify that `temporaryRemoteDirectory` is created and in-use
* Some other `SftpServerOutboundTests` refactoring for better code style

(cherry picked from commit a4b193f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants