Интеграция Spring Boot SFTP и Google Cloud Storage

Извините за мою плохую английскую грамматику. Я работаю над проектами, в которых нам нужно загружать данные с SFTP-сервера в облачное хранилище Google с помощью Spring Boot. На SFTP-сервере есть данные (file.txt), поступающие от многих датчиков в определенное время, скажем, каждые 10 минут. Этот текстовый файл нам нужен для синхронизации с Google Cloud Storage. Всякий раз, когда новый файл загружается на SFTP-сервер, его следует загружать в Google Cloud Storage. Я использую Spring SFTP-интеграцию. Я могу синхронизировать данные с SFTP-сервера в сегменты Google Cloud Storage. Но я не знаю, как удалить локальные временные файлы, когда файл, переданный в хранилище GCP, завершен для всех файлов. Пожалуйста, найдите мои коды ниже. (У меня очень базовые знания о Spring. Я просто использую аннотацию для выполнения этой задачи.) Я нашел некоторый URL-адрес в переполнение стека и использование.

Пример интеграции SFTP с 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;
 } 

}

person akash kumar    schedule 11.02.2019    source источник
comment
Где именно функция, где вы загружаете файл? можешь поделиться?   -  person Itai Soudry    schedule 11.02.2019
comment
Это функция @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); вернуть исходящий адаптер канала; }   -  person akash kumar    schedule 12.02.2019