![](/img/trans.png)
[英]Spring Integration: delete (sftp) remote file after it has been persisted to database
[英]Spring Integration SFTP Delete Remote File On Demand
当我的应用程序中发生某些事情时,我试图从远程 SFTP 存储桶中删除单个文件(我们将文件保存到 blob 存储)。 我无法做到这一点。 我所能看到的只是在处理完成后始终通过出站通道适配器删除文件。 我正在使用 Java 配置。
这是我的代码:
@Bean
@ServiceActivator(inputChannel = "sftpDeleteChannel")
public MessageHandler deleteHandler(SftpInboundProperties properties) {
SftpOutboundGateway SFTP = new SftpOutboundGateway(sftpSessionFactory(properties), "rm", "'" + properties.getRemoteDirectory() + "'");
return SFTP;
}
@Gateway(requestChannel = "sftpDeleteChannel")
Boolean delete(Message<File> file);
这是便于输入文件的服务激活器:
@Bean
@ServiceActivator(inputChannel = "sftpChannel")
public MessageHandler handler(FileProcessingService fileProcessingService) {
return message -> {
fileProcessingService.processFile((Message<File> message);
};
}
我收到此错误消息:
收到回复消息,但发送请求消息时由于异常而退出接收线程:ErrorMessage [payload = org.springframework.messaging.MessageHandlingException:消息处理程序中发生错误[bean'deleteHandler'; 定义在:'类路径资源 [com/.../SftpInboundConfiguration.class]'; 来源:'org.springframework.core.type.classreading.SimpleMethodMetadata@6ff37443']; 嵌套异常是 org.springframework.messaging.MessagingException:无法在 session 上执行; 嵌套异常是 org.springframework.core.NestedIOException: Failed to remove file.; 嵌套异常是 3:Permission denied, failedMessage=GenericMessage [payload=/REMOTE_FILE_PATH/COMPLETE_FILE_NAME, headers={file_remoteHostPort=127.0.0.1:2222, replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@34d7c35d, errorChannel=org. springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@34d7c35d, file_name=COMPLETE_FILE_NAME, file_remoteDirectory=//sftp_remote, file_originalFile=/REMOTE_FILE_PATH/COMPLETE_FILE_NAME, id=2252eee1-6086-c9d9-c421-403f8a0bfc28, file_relativePath=COMPLETE_FILE_NAME, file_remoteFile=COMPLETE_FILE_NAME, timestamp =1588177024991}],headers={id=0d2b19ca-0e7c-7ba8-58cf-f225c126048c,timestamp=1588177025001}] 用于原始 GenericMessage [payload=/REMOTE_FILE_PATH/COMPLETE_FILE_NAME,headers={file_remoteHostPort=127.0.0.1:222,file_remoteHostPort=127.0.0.1:222 , file_remoteDirectory=//sftp_remote, file_originalFile=/REMOTE_FILE_PATH/COMPLETE_FILE_NAME, id=9f29dd04-362a-ae93-df97-b392572d8864, 文件 _relativePath=COMPLETE_FILE_NAME,file_remoteFile=COMPLETE_FILE_NAME,时间戳=1588177023713}]
我不确定我是否做错了事情,或者出站网关是否不用于特定、按需删除或两者兼而有之。 希望得到一些帮助。
谢谢!
编辑:
我可以使用 SftpInboundFileSynchronizer.setDeleteRemoteFiles(true) 删除服务器上的文件,因此就 sftp 存储桶而言,我可以删除文件。 我是否需要更改 InboundChannelAdapter 中的某些内容? 下面是:
@Bean
@InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = "${com.esrx.dhf.listener.sftp.inbound.poll-interval:5000}"))
public MessageSource<File> sftpMessageSource(SftpInboundProperties properties, JdbcMetadataStore metadataStore) {
SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(
sftpInboundFileSynchronizer(properties, metadataStore));
source.setLocalDirectory(new File(System.getProperty("user.dir") + "/" + "sftp_local"));
source.setAutoCreateLocalDirectory(true);
source.setMaxFetchSize(1);
source.setLocalFilter(new FileSystemPersistentAcceptOnceFileListFilter(metadataStore, SFTP_LOCAL_PERSISTENT_PREFIX));
return source;
}
编辑:已解决
所以可敬的 Gary Russel 的真正答案是我应该在 SftpOutboundGateway 中指定文件的完整路径。 所以现在工作解决方案意味着新的 OutboundGateway 看起来像这样:
@Bean
@ServiceActivator(inputChannel = "sftpDeleteChannel")
public MessageHandler deleteHandler(SftpInboundProperties properties) {
return new SftpOutboundGateway(
sftpSessionFactory(properties),
"rm",
"'" + properties.getRemoteDirectory() + "/'" + " + " + "headers['file_remoteFile']");
}
你所拥有的是正确的。
查看原因:
嵌套异常是 3:权限被拒绝,
您无权删除该文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.