Sorry for my poor English Grammar. I am working on projects where we need to upload data from SFTP Server to Google Cloud Storage using Spring Boot. SFTP Server is having data(file.txt) coming from many sensors on some particular time let says every 10 minutes.These text file we need to keep sync with Google Cloud Storage. Whenever a new file uploaded in SFTP Server it should be upload in Google Cloud Storage.I am using Spring SFTP integration.I am able to sync data from SFTP Server to Google Cloud Storage buckets.But I am not sure how to delete local temp files when file transferred to GCP Storage is complete for every files.Please find my codes below.(I have very basic knowledge about Spring.I am just using annotation to complete this task.) I found some url in stackoverflow and using.
Spring Integration SFTP Example with Spring Boot
package com.interiorclientapi.sftp;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.File;
import org.springframework.cloud.gcp.storage.integration.GcsSessionFactory;
import org.springframework.cloud.gcp.storage.integration.outbound.GcsMessageHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.expression.ValueExpression;
import org.springframework.integration.file.filters.AcceptOnceFileListFilter;
import org.springframework.integration.file.remote.session.CachingSessionFactory;
import org.springframework.integration.file.remote.session.SessionFactory;
import org.springframework.integration.sftp.inbound.SftpInboundFileSynchronizer;
import org.springframework.integration.sftp.inbound.SftpInboundFileSynchronizingMessageSource;
import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;
import org.springframework.messaging.MessageHandler;
import org.springframework.stereotype.Component;
import com.google.cloud.storage.Storage;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class InteriorClientApiSFTP {
@Autowired
SFTPProperties sftpProperties;
private static final Logger logger = LoggerFactory.getLogger(InteriorClientApiSFTP.class);
@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
String port = sftpProperties.getPort();
factory.setHost(sftpProperties.getHost());
factory.setPort(port!=null?Integer.parseInt(port):22);
factory.setUser(sftpProperties.getsftpUsername());
factory.setPassword(sftpProperties.getsftpPassword());
factory.setAllowUnknownKeys(true);
System.out.println("creating seession to sftp server");
return new CachingSessionFactory<LsEntry>(factory);
}
@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());
fileSynchronizer.setDeleteRemoteFiles(false);
fileSynchronizer.setRemoteDirectory(sftpProperties.getRemotedirectory());
// fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.txt"));
System.out.println("inside sftpInboundFileSynchronizer");
return fileSynchronizer;
}
@Bean
@InboundChannelAdapter(channel = "mineChannel", poller = @Poller(fixedDelay = "5000"))
public MessageSource<File> sftpMessageSource() {
SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(
sftpInboundFileSynchronizer());
source.setLocalDirectory(new File(sftpProperties.getLocaldirectory()));
source.setAutoCreateLocalDirectory(true);
source.setLocalFilter(new AcceptOnceFileListFilter<File>());
System.out.println("inside sftpMessageSource");
return source;
}
@Bean
@ServiceActivator(inputChannel = "mineChannel")
public MessageHandler outboundChannelAdapter(Storage gcs) {
GcsMessageHandler outboundChannelAdapter = new GcsMessageHandler(new GcsSessionFactory(gcs));
outboundChannelAdapter.setRemoteDirectoryExpression(new ValueExpression<>(sftpProperties.getGcpSFTPBucket()));
System.out.println("outboundChannelAdapter");
return outboundChannelAdapter;
}
}