2

I have implemented SFTP upload to a remote server using the example here.

My requirement is that I will have to upload the same file to multiple directories on the same server. The exact number or location of the directories will be known post-production.

Currently, my implementation allows for upload to a single directory on a single server, by setting remoteDirectoryExpression on the message handler. The remoteDirectoryExpression comes from a property file. It is expected that the remaining directories will be configured in a comma-separated way on the same property. I would like my implementation to extract each of the these comma-separated directories from the property and upload the file to each of them.

Is this even possible? I came across publish-subscribe channels but am currently struggling to understand how to include them in my implementation. Even then, pub-sub channels seem to require pre-configuring in the code where one channel = one directory. So am I even on the right track?

user1825770
  • 359
  • 1
  • 6
  • 17
  • 1
    This seems like a very inefficient way to do things. Can you not just upload it once and then copy it locally to every directory? – RaminS Jan 21 '19 at 19:33
  • @Gedarme Yes! That makes sense. Let me give that a try. – user1825770 Jan 21 '19 at 20:11
  • That would also save you a lot of bandwidth if the files are large. – RaminS Jan 21 '19 at 20:11
  • @Gendarme Apparently it is not supported :) https://stackoverflow.com/questions/37664612/copying-files-internally-on-an-sftp-server-using-spring-integration-jcraft-jsch?rq=1 – user1825770 Jan 21 '19 at 20:35

2 Answers2

2

There is nothing built-in to do that.

The simplest way would be to create a custom splitter upstream and emit n messages with the directory in a header and then use the header value in the remote directory expression.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
1

try with publishSubscribeChannel

.publishSubscribeChannel(s -> s
                        .subscribe(f -> f
                                .handle(Sftp.outboundAdapter(sftpSessionFactory())
                                        .remoteDirectory(getRemoteRootDir() + remoteDirectory1)

                                        .temporaryFileSuffix(".tmp")))
                        .subscribe(f -> f
                                .handle(Sftp.outboundAdapter(sftpSessionFactory())
                                        .remoteDirectory(getRemoteRootDir() + remoteDirectory2)
                                        .fileNameExpression(fileRenameExpression)
                                        .temporaryFileSuffix(".tmp")))
Hari
  • 35
  • 6
  • 2
    That will work too, but only if you know the number of destinations when writing the code; the splitter solution will work with a variable number of directories (either via properties or at runtime for a particular file or file type). But I gave you a vote up. – Gary Russell Jan 23 '19 at 15:52
  • Thanks Gary , Please help for this query [https://stackoverflow.com/questions/54329898/give-priority-to-sftp-remote-directories] – Hari Jan 23 '19 at 16:34